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() {
/** * 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; } /**
/** * 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
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