/** * Creates a new subset with given key range. * * @param from * the start of the subset range. * @param bottom * if true, the first parameter is ignored and the range starts from * -∞. * @param to * the end of the subset range. * @param top * if true, the third parameter is ignored and the range goes to * ∞. */ public Subset(final float from, final boolean bottom, final float to, final boolean top) { if (!bottom && !top && FloatAVLTreeSet.this.compare(from, to) > 0) throw new IllegalArgumentException( "Start element (" + from + ") is larger than end element (" + to + ")"); this.from = from; this.bottom = bottom; this.to = to; this.top = top; } @Override
@Override void updatePrevious() { prev = prev.prev(); if (!bottom && prev != null && FloatAVLTreeSet.this.compare(prev.key, from) < 0) prev = null; } @Override
@Override void updateNext() { next = next.next(); if (!top && next != null && FloatAVLTreeSet.this.compare(next.key, to) >= 0) next = null; } }
@Override public FloatSortedSet subSet(float from, float to) { if (top && bottom) return new Subset(from, false, to, false); if (!top) to = compare(to, this.to) < 0 ? to : this.to; if (!bottom) from = compare(from, this.from) > 0 ? from : this.from; if (!top && !bottom && from == this.from && to == this.to) return this; return new Subset(from, false, to, false); } /**
/** * Locates the first entry. * * @return the first entry of this subset, or {@code null} if the subset is * empty. */ public FloatAVLTreeSet.Entry firstEntry() { if (tree == null) return null; // If this subset goes to -infinity, we return the main set first entry; // otherwise, we locate the start of the set. FloatAVLTreeSet.Entry e; if (bottom) e = firstEntry; else { e = locateKey(from); // If we find either the start or something greater we're OK. if (compare(e.key, from) < 0) e = e.next(); } // Finally, if this subset doesn't go to infinity, we check that the resulting // key isn't greater than the end. if (e == null || !top && compare(e.key, to) >= 0) return null; return e; } /**
SubsetIterator(final float k) { this(); if (next != null) { if (!bottom && compare(k, next.key) < 0) prev = null; else if (!top && compare(k, (prev = lastEntry()).key) >= 0) next = null; else { next = locateKey(k); if (compare(next.key, k) <= 0) { prev = next; next = next.next(); } else prev = next.prev(); } } } @Override
/** * Locates the last entry. * * @return the last entry of this subset, or {@code null} if the subset is * empty. */ public FloatAVLTreeSet.Entry lastEntry() { if (tree == null) return null; // If this subset goes to infinity, we return the main set last entry; // otherwise, we locate the end of the set. FloatAVLTreeSet.Entry e; if (top) e = lastEntry; else { e = locateKey(to); // If we find something smaller than the end we're OK. if (compare(e.key, to) >= 0) e = e.prev(); } // Finally, if this subset doesn't go to -infinity, we check that the resulting // key isn't smaller than the start. if (e == null || !bottom && compare(e.key, from) < 0) return null; return e; } @Override
@Override public FloatSortedSet headSet(final float to) { if (top) return new Subset(from, bottom, to, false); return compare(to, this.to) < 0 ? new Subset(from, bottom, to, false) : this; } @Override
@Override public FloatSortedSet tailSet(final float from) { if (bottom) return new Subset(from, false, to, top); return compare(from, this.from) > 0 ? new Subset(from, false, to, top) : this; } @Override
/** * Returns the entry corresponding to the given key, if it is in the tree; * {@code null}, otherwise. * * @param k * the key to search for. * @return the corresponding entry, or {@code null} if no entry with the given * key exists. */ private Entry findKey(final float k) { Entry e = tree; int cmp; while (e != null && (cmp = compare(k, e.key)) != 0) e = cmp < 0 ? e.left() : e.right(); return e; } /**
/** * Locates a key. * * @param k * a key. * @return the last entry on a search for the given key; this will be the given * key, if it present; otherwise, it will be either the smallest greater * key or the greatest smaller key. */ final Entry locateKey(final float k) { Entry e = tree, last = tree; int cmp = 0; while (e != null && (cmp = compare(k, e.key)) != 0) { last = e; e = cmp < 0 ? e.left() : e.right(); } return cmp == 0 ? e : last; } /**
SetIterator(final float k) { if ((next = locateKey(k)) != null) { if (compare(next.key, k) <= 0) { prev = next; next = next.next(); } else prev = next.prev(); } } @Override
int cmp, i = 0; while (true) { if ((cmp = compare(k, p.key)) == 0) return false; if (p.balance() != 0) {
final float kk = k; while (true) { if ((cmp = compare(kk, p.key)) == 0) break; else if (dir = cmp > 0) {