public BinaryIndex div() { BinaryIndex tmp = new BinaryIndex(this); tmp.b_.remove(0); tmp.b_.add(tmp.c_); tmp.c_ = false; return tmp; }
public BinaryIndex sub() { BinaryIndex tmp = new BinaryIndex(this); Boolean carry = false; tmp.b_.set(0, this.b_.get(0) ^ true ^ carry); carry = (!this.b_.get(0) /*&& true*/) || (/*true &&*/ carry) || (carry && !this.b_.get(0)); for (int i = 1; i < this.b_.size(); ++i) { tmp.b_.set(i, this.b_.get(i) ^ false ^ carry); carry = /*( !this.b_.get( i ) && false ) || ( false && carry ) ||*/ (carry && !this.b_.get(i)); } tmp.c_ ^= carry; return tmp; }
public BinaryIndex add(BinaryIndex that) { BinaryIndex tmp = new BinaryIndex(this); Boolean carry = false; for (int i = 0; i < this.b_.size(); ++i) { tmp.b_.set(i, this.b_.get(i) ^ that.b_.get(i) ^ carry); carry = (this.b_.get(i) && that.b_.get(i)) || (that.b_.get(i) && carry) || (carry && this.b_.get(i)); } tmp.c_ = carry; return tmp; }
private Pack<T> extract(BinaryIndex index) { ArrayList<T> items = new ArrayList<T>(); ArrayList<Boolean> b = index.getIndex(); for (int i = 0; i < b.size(); ++i) { if (b.get(i)) { items.add(this.map_.get(i)); } } return new Pack<T>(this.eval(index), items); }
private Long eval(BinaryIndex index) { Long sum = 0L; ArrayList<Boolean> b = index.getIndex(); for (int i = 0; i < b.size(); ++i) { if (b.get(i)) { sum += this.items_.get(this.map_.get(i)); } } return sum; }
private BinaryIndex call(BinaryIndex b, BinaryIndex e) { if (b.equals(e.sub())) { return b; } BinaryIndex u = b.add(e.sub(b).div()); BinaryIndex l = u.sub(); Long lv = this.eval(l); Long uv = this.eval(u); if (this.limit_ == lv) { return l; } if (this.limit_ == uv) { return u; } ArrayList<BinaryIndex> tmp = new ArrayList<BinaryIndex>(); if (this.limit_ < lv) { tmp.add(this.call(b, u)); } if (this.limit_ > uv) { tmp.add(this.call(u, e)); } if (tmp.isEmpty()) { return l; } return Collections.max( tmp, new Comparator<BinaryIndex>() { @Override public int compare(BinaryIndex l, BinaryIndex r) { return BinarySearch.this.eval(l).compareTo(BinarySearch.this.eval(r)); } }); }
public static BinaryIndex end(int digits) { BinaryIndex tmp = BinaryIndex.begin(digits); tmp.c_ = true; return tmp; }
public Pack<T> call() { BinaryIndex lower = BinaryIndex.begin(this.items_.size()); BinaryIndex upper = BinaryIndex.end(this.items_.size()); return this.extract(this.call(lower, upper)); }