protected LongIterator toIterator()

in spark-load/spark-load-common/src/main/java/org/apache/doris/common/io/Roaring64Map.java [1025:1089]


    protected LongIterator toIterator(final Iterator<Map.Entry<Integer, BitmapDataProvider>> it,
                                      final boolean reversed) {
        return new LongIterator() {

            protected int currentKey;
            protected IntIterator currentIt;

            @Override
            public boolean hasNext() {
                if (currentIt == null) {
                    // Were initially empty
                    if (!moveToNextEntry(it)) {
                        return false;
                    }
                }

                while (true) {
                    if (currentIt.hasNext()) {
                        return true;
                    } else {
                        if (!moveToNextEntry(it)) {
                            return false;
                        }
                    }
                }
            }

            /**
             *
             * @param it the underlying iterator which has to be moved to next long
             * @return true if we MAY have more entries. false if there is definitely nothing more
             */
            private boolean moveToNextEntry(Iterator<Map.Entry<Integer, BitmapDataProvider>> it) {
                if (it.hasNext()) {
                    Map.Entry<Integer, BitmapDataProvider> next = it.next();
                    currentKey = next.getKey();
                    if (reversed) {
                        currentIt = next.getValue().getReverseIntIterator();
                    } else {
                        currentIt = next.getValue().getIntIterator();
                    }

                    // We may have more long
                    return true;
                } else {
                    // We know there is nothing more
                    return false;
                }
            }

            @Override
            public long next() {
                if (hasNext()) {
                    return pack(currentKey, currentIt.next());
                } else {
                    throw new IllegalStateException("empty");
                }
            }

            @Override
            public LongIterator clone() {
                throw new UnsupportedOperationException("TODO");
            }
        };
    }