/** * Creates a new submap with given key range. * * @param from * the start of the submap range. * @param bottom * if true, the first parameter is ignored and the range starts from * -∞. * @param to * the end of the submap range. * @param top * if true, the third parameter is ignored and the range goes to * ∞. */ public Submap(final int from, final boolean bottom, final int to, final boolean top) { if (!bottom && !top && Int2IntRBTreeMap.this.compare(from, to) > 0) throw new IllegalArgumentException("Start key (" + from + ") is larger than end key (" + to + ")"); this.from = from; this.bottom = bottom; this.to = to; this.top = top; this.defRetValue = Int2IntRBTreeMap.this.defRetValue; } @Override
@Override void updateNext() { next = next.next(); if (!top && next != null && Int2IntRBTreeMap.this.compare(next.key, to) >= 0) next = null; } }
@Override void updatePrevious() { prev = prev.prev(); if (!bottom && prev != null && Int2IntRBTreeMap.this.compare(prev.key, from) < 0) prev = null; } @Override
@Override public Int2IntSortedMap subMap(int from, int to) { if (top && bottom) return new Submap(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 Submap(from, false, to, false); } /**
/** * Locates the last entry. * * @return the last entry of this submap, or {@code null} if the submap is * empty. */ public Int2IntRBTreeMap.Entry lastEntry() { if (tree == null) return null; // If this submap goes to infinity, we return the main map last entry; // otherwise, we locate the end of the map. Int2IntRBTreeMap.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 submap 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
/** * Locates the first entry. * * @return the first entry of this submap, or {@code null} if the submap is * empty. */ public Int2IntRBTreeMap.Entry firstEntry() { if (tree == null) return null; // If this submap goes to -infinity, we return the main map first entry; // otherwise, we locate the start of the map. Int2IntRBTreeMap.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 submap 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; } /**
SubmapIterator(final int 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
@Override public Int2IntSortedMap headMap(final int to) { if (top) return new Submap(from, bottom, to, false); return compare(to, this.to) < 0 ? new Submap(from, bottom, to, false) : this; } @Override
@Override public Int2IntSortedMap tailMap(final int from) { if (bottom) return new Submap(from, false, to, top); return compare(from, this.from) > 0 ? new Submap(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. */ final Entry findKey(final int 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 int 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; } /**
TreeIterator(final int k) { if ((next = locateKey(k)) != null) { if (compare(next.key, k) <= 0) { prev = next; next = next.next(); } else prev = next.prev(); } } public boolean hasNext() {
int cmp, i = 0; while (true) { if ((cmp = compare(k, p.key)) == 0) {
final int kk = k; while (true) { if ((cmp = compare(kk, p.key)) == 0) break; dirPath[i] = cmp > 0;