All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.tum.in.naturals.bitset.BitSetIterator Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2017 Tobias Meggendorfer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package de.tum.in.naturals.bitset;

import it.unimi.dsi.fastutil.ints.IntIterator;
import java.util.BitSet;
import java.util.NoSuchElementException;

final class BitSetIterator implements IntIterator {
    private final BitSet bitSet;
    private int current = -1;
    private int next;

    BitSetIterator(BitSet bitSet) {
        this.bitSet = bitSet;
        next = getNext(0);
    }

    private int getNext(int index) {
        return bitSet.nextSetBit(index);
    }

    @Override
    public boolean hasNext() {
        return next != -1;
    }

    @Override
    public int nextInt() {
        if (next == -1) {
            throw new NoSuchElementException();
        }
        current = next;
        next = getNext(next + 1);
        return current;
    }

    @Override
    public void remove() {
        if (current == -1) {
            throw new IllegalStateException();
        }
        assert bitSet.get(current);
        bitSet.clear(current);
        current = -1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy