// 
// Decompiled by Procyon v0.6.0
// 

package it.unimi.dsi.fastutil.booleans;

import java.util.function.Consumer;
import java.util.NoSuchElementException;
import java.io.Serializable;
import java.util.Objects;
import java.util.ListIterator;
import java.util.Iterator;
import it.unimi.dsi.fastutil.BigArrays;

public final class BooleanIterators
{
    public static final EmptyIterator EMPTY_ITERATOR;
    
    private BooleanIterators() {
    }
    
    public static BooleanListIterator singleton(final boolean element) {
        return new SingletonIterator(element);
    }
    
    public static BooleanListIterator wrap(final boolean[] array, final int offset, final int length) {
        BooleanArrays.ensureOffsetLength(array, offset, length);
        return new ArrayIterator(array, offset, length);
    }
    
    public static BooleanListIterator wrap(final boolean[] array) {
        return new ArrayIterator(array, 0, array.length);
    }
    
    public static int unwrap(final BooleanIterator i, final boolean[] array, int offset, final int max) {
        if (max < 0) {
            throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
        }
        if (offset < 0 || offset + max > array.length) {
            throw new IllegalArgumentException();
        }
        int j = max;
        while (j-- != 0 && i.hasNext()) {
            array[offset++] = i.nextBoolean();
        }
        return max - j - 1;
    }
    
    public static int unwrap(final BooleanIterator i, final boolean[] array) {
        return unwrap(i, array, 0, array.length);
    }
    
    public static boolean[] unwrap(final BooleanIterator i, int max) {
        if (max < 0) {
            throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
        }
        boolean[] array = new boolean[16];
        int j = 0;
        while (max-- != 0 && i.hasNext()) {
            if (j == array.length) {
                array = BooleanArrays.grow(array, j + 1);
            }
            array[j++] = i.nextBoolean();
        }
        return BooleanArrays.trim(array, j);
    }
    
    public static boolean[] unwrap(final BooleanIterator i) {
        return unwrap(i, Integer.MAX_VALUE);
    }
    
    public static long unwrap(final BooleanIterator i, final boolean[][] array, long offset, final long max) {
        if (max < 0L) {
            throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
        }
        if (offset < 0L || offset + max > BigArrays.length(array)) {
            throw new IllegalArgumentException();
        }
        long j = max;
        while (j-- != 0L && i.hasNext()) {
            BigArrays.set(array, offset++, i.nextBoolean());
        }
        return max - j - 1L;
    }
    
    public static long unwrap(final BooleanIterator i, final boolean[][] array) {
        return unwrap(i, array, 0L, BigArrays.length(array));
    }
    
    public static int unwrap(final BooleanIterator i, final BooleanCollection c, final int max) {
        if (max < 0) {
            throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
        }
        int j = max;
        while (j-- != 0 && i.hasNext()) {
            c.add(i.nextBoolean());
        }
        return max - j - 1;
    }
    
    public static boolean[][] unwrapBig(final BooleanIterator i, long max) {
        if (max < 0L) {
            throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
        }
        boolean[][] array = BooleanBigArrays.newBigArray(16L);
        long j = 0L;
        while (max-- != 0L && i.hasNext()) {
            if (j == BigArrays.length(array)) {
                array = BigArrays.grow(array, j + 1L);
            }
            BigArrays.set(array, j++, i.nextBoolean());
        }
        return BigArrays.trim(array, j);
    }
    
    public static boolean[][] unwrapBig(final BooleanIterator i) {
        return unwrapBig(i, Long.MAX_VALUE);
    }
    
    public static long unwrap(final BooleanIterator i, final BooleanCollection c) {
        long n = 0L;
        while (i.hasNext()) {
            c.add(i.nextBoolean());
            ++n;
        }
        return n;
    }
    
    public static int pour(final BooleanIterator i, final BooleanCollection s, final int max) {
        if (max < 0) {
            throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
        }
        int j = max;
        while (j-- != 0 && i.hasNext()) {
            s.add(i.nextBoolean());
        }
        return max - j - 1;
    }
    
    public static int pour(final BooleanIterator i, final BooleanCollection s) {
        return pour(i, s, Integer.MAX_VALUE);
    }
    
    public static BooleanList pour(final BooleanIterator i, final int max) {
        final BooleanArrayList l = new BooleanArrayList();
        pour(i, l, max);
        l.trim();
        return l;
    }
    
    public static BooleanList pour(final BooleanIterator i) {
        return pour(i, Integer.MAX_VALUE);
    }
    
    public static BooleanIterator asBooleanIterator(final Iterator i) {
        if (i instanceof BooleanIterator) {
            return (BooleanIterator)i;
        }
        return new IteratorWrapper(i);
    }
    
    public static BooleanListIterator asBooleanIterator(final ListIterator i) {
        if (i instanceof BooleanListIterator) {
            return (BooleanListIterator)i;
        }
        return new ListIteratorWrapper(i);
    }
    
    public static boolean any(final BooleanIterator iterator, final BooleanPredicate predicate) {
        return indexOf(iterator, predicate) != -1;
    }
    
    public static boolean all(final BooleanIterator iterator, final BooleanPredicate predicate) {
        Objects.requireNonNull(predicate);
        while (iterator.hasNext()) {
            if (!predicate.test(iterator.nextBoolean())) {
                return false;
            }
        }
        return true;
    }
    
    public static int indexOf(final BooleanIterator iterator, final BooleanPredicate predicate) {
        Objects.requireNonNull(predicate);
        int i = 0;
        while (iterator.hasNext()) {
            if (predicate.test(iterator.nextBoolean())) {
                return i;
            }
            ++i;
        }
        return -1;
    }
    
    public static BooleanIterator concat(final BooleanIterator... a) {
        return concat(a, 0, a.length);
    }
    
    public static BooleanIterator concat(final BooleanIterator[] a, final int offset, final int length) {
        return new IteratorConcatenator(a, offset, length);
    }
    
    public static BooleanIterator unmodifiable(final BooleanIterator i) {
        return new UnmodifiableIterator(i);
    }
    
    public static BooleanBidirectionalIterator unmodifiable(final BooleanBidirectionalIterator i) {
        return new UnmodifiableBidirectionalIterator(i);
    }
    
    public static BooleanListIterator unmodifiable(final BooleanListIterator i) {
        return new UnmodifiableListIterator(i);
    }
    
    static {
        EMPTY_ITERATOR = new EmptyIterator();
    }
    
    public static class EmptyIterator implements BooleanListIterator, Serializable, Cloneable
    {
        private static final long serialVersionUID = -7046029254386353129L;
        
        protected EmptyIterator() {
        }
        
        @Override
        public boolean hasNext() {
            return false;
        }
        
        @Override
        public boolean hasPrevious() {
            return false;
        }
        
        @Override
        public boolean nextBoolean() {
            throw new NoSuchElementException();
        }
        
        @Override
        public boolean previousBoolean() {
            throw new NoSuchElementException();
        }
        
        @Override
        public int nextIndex() {
            return 0;
        }
        
        @Override
        public int previousIndex() {
            return -1;
        }
        
        @Override
        public int skip(final int n) {
            return 0;
        }
        
        @Override
        public int back(final int n) {
            return 0;
        }
        
        @Override
        public void forEachRemaining(final BooleanConsumer action) {
        }
        
        @Deprecated
        @Override
        public void forEachRemaining(final Consumer<? super Boolean> action) {
        }
        
        public Object clone() {
            return BooleanIterators.EMPTY_ITERATOR;
        }
        
        private Object readResolve() {
            return BooleanIterators.EMPTY_ITERATOR;
        }
    }
    
    private static class SingletonIterator implements BooleanListIterator
    {
        private final boolean element;
        private byte curr;
        
        public SingletonIterator(final boolean element) {
            this.element = element;
        }
        
        @Override
        public boolean hasNext() {
            return this.curr == 0;
        }
        
        @Override
        public boolean hasPrevious() {
            return this.curr == 1;
        }
        
        @Override
        public boolean nextBoolean() {
            if (!this.hasNext()) {
                throw new NoSuchElementException();
            }
            this.curr = 1;
            return this.element;
        }
        
        @Override
        public boolean previousBoolean() {
            if (!this.hasPrevious()) {
                throw new NoSuchElementException();
            }
            this.curr = 0;
            return this.element;
        }
        
        @Override
        public void forEachRemaining(final BooleanConsumer action) {
            Objects.requireNonNull(action);
            if (this.curr == 0) {
                action.accept(this.element);
                this.curr = 1;
            }
        }
        
        @Override
        public int nextIndex() {
            return this.curr;
        }
        
        @Override
        public int previousIndex() {
            return this.curr - 1;
        }
        
        @Override
        public int back(final int n) {
            if (n < 0) {
                throw new IllegalArgumentException("Argument must be nonnegative: " + n);
            }
            if (n == 0 || this.curr < 1) {
                return 0;
            }
            this.curr = 1;
            return 1;
        }
        
        @Override
        public int skip(final int n) {
            if (n < 0) {
                throw new IllegalArgumentException("Argument must be nonnegative: " + n);
            }
            if (n == 0 || this.curr > 0) {
                return 0;
            }
            this.curr = 0;
            return 1;
        }
    }
    
    private static class ArrayIterator implements BooleanListIterator
    {
        private final boolean[] array;
        private final int offset;
        private final int length;
        private int curr;
        
        public ArrayIterator(final boolean[] array, final int offset, final int length) {
            this.array = array;
            this.offset = offset;
            this.length = length;
        }
        
        @Override
        public boolean hasNext() {
            return this.curr < this.length;
        }
        
        @Override
        public boolean hasPrevious() {
            return this.curr > 0;
        }
        
        @Override
        public boolean nextBoolean() {
            if (!this.hasNext()) {
                throw new NoSuchElementException();
            }
            return this.array[this.offset + this.curr++];
        }
        
        @Override
        public boolean previousBoolean() {
            if (!this.hasPrevious()) {
                throw new NoSuchElementException();
            }
            final boolean[] array = this.array;
            final int offset = this.offset;
            final int curr = this.curr - 1;
            this.curr = curr;
            return array[offset + curr];
        }
        
        @Override
        public void forEachRemaining(final BooleanConsumer action) {
            Objects.requireNonNull(action);
            while (this.curr < this.length) {
                action.accept(this.array[this.offset + this.curr]);
                ++this.curr;
            }
        }
        
        @Override
        public int skip(int n) {
            if (n < 0) {
                throw new IllegalArgumentException("Argument must be nonnegative: " + n);
            }
            if (n <= this.length - this.curr) {
                this.curr += n;
                return n;
            }
            n = this.length - this.curr;
            this.curr = this.length;
            return n;
        }
        
        @Override
        public int back(int n) {
            if (n < 0) {
                throw new IllegalArgumentException("Argument must be nonnegative: " + n);
            }
            if (n <= this.curr) {
                this.curr -= n;
                return n;
            }
            n = this.curr;
            this.curr = 0;
            return n;
        }
        
        @Override
        public int nextIndex() {
            return this.curr;
        }
        
        @Override
        public int previousIndex() {
            return this.curr - 1;
        }
    }
    
    private static class IteratorWrapper implements BooleanIterator
    {
        final Iterator<Boolean> i;
        
        public IteratorWrapper(final Iterator<Boolean> i) {
            this.i = i;
        }
        
        @Override
        public boolean hasNext() {
            return this.i.hasNext();
        }
        
        @Override
        public void remove() {
            this.i.remove();
        }
        
        @Override
        public boolean nextBoolean() {
            return this.i.next();
        }
        
        @Override
        public void forEachRemaining(final BooleanConsumer action) {
            this.i.forEachRemaining(action);
        }
        
        @Deprecated
        @Override
        public void forEachRemaining(final Consumer<? super Boolean> action) {
            this.i.forEachRemaining(action);
        }
    }
    
    private static class ListIteratorWrapper implements BooleanListIterator
    {
        final ListIterator<Boolean> i;
        
        public ListIteratorWrapper(final ListIterator<Boolean> i) {
            this.i = i;
        }
        
        @Override
        public boolean hasNext() {
            return this.i.hasNext();
        }
        
        @Override
        public boolean hasPrevious() {
            return this.i.hasPrevious();
        }
        
        @Override
        public int nextIndex() {
            return this.i.nextIndex();
        }
        
        @Override
        public int previousIndex() {
            return this.i.previousIndex();
        }
        
        @Override
        public void set(final boolean k) {
            this.i.set(k);
        }
        
        @Override
        public void add(final boolean k) {
            this.i.add(k);
        }
        
        @Override
        public void remove() {
            this.i.remove();
        }
        
        @Override
        public boolean nextBoolean() {
            return this.i.next();
        }
        
        @Override
        public boolean previousBoolean() {
            return this.i.previous();
        }
        
        @Override
        public void forEachRemaining(final BooleanConsumer action) {
            this.i.forEachRemaining(action);
        }
        
        @Deprecated
        @Override
        public void forEachRemaining(final Consumer<? super Boolean> action) {
            this.i.forEachRemaining(action);
        }
    }
    
    public abstract static class AbstractIndexBasedIterator extends AbstractBooleanIterator
    {
        protected final int minPos;
        protected int pos;
        protected int lastReturned;
        
        protected AbstractIndexBasedIterator(final int minPos, final int initialPos) {
            this.minPos = minPos;
            this.pos = initialPos;
        }
        
        protected abstract boolean get(final int p0);
        
        protected abstract void remove(final int p0);
        
        protected abstract int getMaxPos();
        
        @Override
        public boolean hasNext() {
            return this.pos < this.getMaxPos();
        }
        
        @Override
        public boolean nextBoolean() {
            if (!this.hasNext()) {
                throw new NoSuchElementException();
            }
            final int lastReturned = this.pos++;
            this.lastReturned = lastReturned;
            return this.get(lastReturned);
        }
        
        @Override
        public void remove() {
            if (this.lastReturned == -1) {
                throw new IllegalStateException();
            }
            this.remove(this.lastReturned);
            if (this.lastReturned < this.pos) {
                --this.pos;
            }
            this.lastReturned = -1;
        }
        
        @Override
        public void forEachRemaining(final BooleanConsumer action) {
            while (this.pos < this.getMaxPos()) {
                final int lastReturned = this.pos++;
                this.lastReturned = lastReturned;
                action.accept(this.get(lastReturned));
            }
        }
        
        @Override
        public int skip(int n) {
            if (n < 0) {
                throw new IllegalArgumentException("Argument must be nonnegative: " + n);
            }
            final int max = this.getMaxPos();
            final int remaining = max - this.pos;
            if (n < remaining) {
                this.pos += n;
            }
            else {
                n = remaining;
                this.pos = max;
            }
            this.lastReturned = this.pos - 1;
            return n;
        }
    }
    
    public abstract static class AbstractIndexBasedListIterator extends AbstractIndexBasedIterator implements BooleanListIterator
    {
        protected AbstractIndexBasedListIterator(final int minPos, final int initialPos) {
            super(minPos, initialPos);
        }
        
        protected abstract void add(final int p0, final boolean p1);
        
        protected abstract void set(final int p0, final boolean p1);
        
        @Override
        public boolean hasPrevious() {
            return this.pos > this.minPos;
        }
        
        @Override
        public boolean previousBoolean() {
            if (!this.hasPrevious()) {
                throw new NoSuchElementException();
            }
            final int n = this.pos - 1;
            this.pos = n;
            this.lastReturned = n;
            return this.get(n);
        }
        
        @Override
        public int nextIndex() {
            return this.pos;
        }
        
        @Override
        public int previousIndex() {
            return this.pos - 1;
        }
        
        @Override
        public void add(final boolean k) {
            this.add(this.pos++, k);
            this.lastReturned = -1;
        }
        
        @Override
        public void set(final boolean k) {
            if (this.lastReturned == -1) {
                throw new IllegalStateException();
            }
            this.set(this.lastReturned, k);
        }
        
        @Override
        public int back(int n) {
            if (n < 0) {
                throw new IllegalArgumentException("Argument must be nonnegative: " + n);
            }
            final int remaining = this.pos - this.minPos;
            if (n < remaining) {
                this.pos -= n;
            }
            else {
                n = remaining;
                this.pos = this.minPos;
            }
            this.lastReturned = this.pos;
            return n;
        }
    }
    
    private static class IteratorConcatenator implements BooleanIterator
    {
        final BooleanIterator[] a;
        int offset;
        int length;
        int lastOffset;
        
        public IteratorConcatenator(final BooleanIterator[] a, final int offset, final int length) {
            this.lastOffset = -1;
            this.a = a;
            this.offset = offset;
            this.length = length;
            this.advance();
        }
        
        private void advance() {
            while (this.length != 0 && !this.a[this.offset].hasNext()) {
                --this.length;
                ++this.offset;
            }
        }
        
        @Override
        public boolean hasNext() {
            return this.length > 0;
        }
        
        @Override
        public boolean nextBoolean() {
            if (!this.hasNext()) {
                throw new NoSuchElementException();
            }
            final BooleanIterator[] a = this.a;
            final int offset = this.offset;
            this.lastOffset = offset;
            final boolean next = a[offset].nextBoolean();
            this.advance();
            return next;
        }
        
        @Override
        public void forEachRemaining(final BooleanConsumer action) {
            while (this.length > 0) {
                final BooleanIterator[] a = this.a;
                final int offset = this.offset;
                this.lastOffset = offset;
                a[offset].forEachRemaining(action);
                this.advance();
            }
        }
        
        @Deprecated
        @Override
        public void forEachRemaining(final Consumer<? super Boolean> action) {
            while (this.length > 0) {
                final BooleanIterator[] a = this.a;
                final int offset = this.offset;
                this.lastOffset = offset;
                a[offset].forEachRemaining(action);
                this.advance();
            }
        }
        
        @Override
        public void remove() {
            if (this.lastOffset == -1) {
                throw new IllegalStateException();
            }
            this.a[this.lastOffset].remove();
        }
        
        @Override
        public int skip(final int n) {
            if (n < 0) {
                throw new IllegalArgumentException("Argument must be nonnegative: " + n);
            }
            this.lastOffset = -1;
            int skipped = 0;
            while (skipped < n && this.length != 0) {
                skipped += this.a[this.offset].skip(n - skipped);
                if (this.a[this.offset].hasNext()) {
                    break;
                }
                --this.length;
                ++this.offset;
            }
            return skipped;
        }
    }
    
    public static class UnmodifiableIterator implements BooleanIterator
    {
        protected final BooleanIterator i;
        
        public UnmodifiableIterator(final BooleanIterator i) {
            this.i = i;
        }
        
        @Override
        public boolean hasNext() {
            return this.i.hasNext();
        }
        
        @Override
        public boolean nextBoolean() {
            return this.i.nextBoolean();
        }
        
        @Override
        public void forEachRemaining(final BooleanConsumer action) {
            this.i.forEachRemaining(action);
        }
        
        @Deprecated
        @Override
        public void forEachRemaining(final Consumer<? super Boolean> action) {
            this.i.forEachRemaining(action);
        }
    }
    
    public static class UnmodifiableBidirectionalIterator implements BooleanBidirectionalIterator
    {
        protected final BooleanBidirectionalIterator i;
        
        public UnmodifiableBidirectionalIterator(final BooleanBidirectionalIterator i) {
            this.i = i;
        }
        
        @Override
        public boolean hasNext() {
            return this.i.hasNext();
        }
        
        @Override
        public boolean hasPrevious() {
            return this.i.hasPrevious();
        }
        
        @Override
        public boolean nextBoolean() {
            return this.i.nextBoolean();
        }
        
        @Override
        public boolean previousBoolean() {
            return this.i.previousBoolean();
        }
        
        @Override
        public void forEachRemaining(final BooleanConsumer action) {
            this.i.forEachRemaining(action);
        }
        
        @Deprecated
        @Override
        public void forEachRemaining(final Consumer<? super Boolean> action) {
            this.i.forEachRemaining(action);
        }
    }
    
    public static class UnmodifiableListIterator implements BooleanListIterator
    {
        protected final BooleanListIterator i;
        
        public UnmodifiableListIterator(final BooleanListIterator i) {
            this.i = i;
        }
        
        @Override
        public boolean hasNext() {
            return this.i.hasNext();
        }
        
        @Override
        public boolean hasPrevious() {
            return this.i.hasPrevious();
        }
        
        @Override
        public boolean nextBoolean() {
            return this.i.nextBoolean();
        }
        
        @Override
        public boolean previousBoolean() {
            return this.i.previousBoolean();
        }
        
        @Override
        public int nextIndex() {
            return this.i.nextIndex();
        }
        
        @Override
        public int previousIndex() {
            return this.i.previousIndex();
        }
        
        @Override
        public void forEachRemaining(final BooleanConsumer action) {
            this.i.forEachRemaining(action);
        }
        
        @Deprecated
        @Override
        public void forEachRemaining(final Consumer<? super Boolean> action) {
            this.i.forEachRemaining(action);
        }
    }
}
