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

package it.unimi.dsi.fastutil.bytes;

import java.util.Spliterator;
import java.io.Serializable;
import java.util.NoSuchElementException;
import it.unimi.dsi.fastutil.BigListIterator;
import java.util.Objects;
import it.unimi.dsi.fastutil.BigList;
import it.unimi.dsi.fastutil.BigArrays;
import java.util.RandomAccess;
import it.unimi.dsi.fastutil.ints.IntSpliterator;
import java.util.Iterator;
import java.util.Collection;

public abstract class AbstractByteBigList extends AbstractByteCollection implements ByteBigList, ByteStack
{
    protected AbstractByteBigList() {
    }
    
    protected void ensureIndex(final long index) {
        if (index < 0L) {
            throw new IndexOutOfBoundsException("Index (" + index + ") is negative");
        }
        if (index > this.size64()) {
            throw new IndexOutOfBoundsException("Index (" + index + ") is greater than list size (" + this.size64() + ")");
        }
    }
    
    protected void ensureRestrictedIndex(final long index) {
        if (index < 0L) {
            throw new IndexOutOfBoundsException("Index (" + index + ") is negative");
        }
        if (index >= this.size64()) {
            throw new IndexOutOfBoundsException("Index (" + index + ") is greater than or equal to list size (" + this.size64() + ")");
        }
    }
    
    @Override
    public void add(final long index, final byte k) {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public boolean add(final byte k) {
        this.add(this.size64(), k);
        return true;
    }
    
    @Override
    public byte removeByte(final long i) {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public byte set(final long index, final byte k) {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public boolean addAll(long index, final Collection<? extends Byte> c) {
        this.ensureIndex(index);
        final Iterator<? extends Byte> i = c.iterator();
        final boolean retVal = i.hasNext();
        while (i.hasNext()) {
            this.add(index++, (Byte)i.next());
        }
        return retVal;
    }
    
    @Override
    public boolean addAll(final Collection<? extends Byte> c) {
        return this.addAll(this.size64(), c);
    }
    
    @Override
    public ByteBigListIterator iterator() {
        return this.listIterator();
    }
    
    @Override
    public ByteBigListIterator listIterator() {
        return this.listIterator(0L);
    }
    
    @Override
    public ByteBigListIterator listIterator(final long index) {
        this.ensureIndex(index);
        return new ByteBigListIterators.AbstractIndexBasedBigListIterator(0L, index) {
            @Override
            protected final byte get(final long i) {
                return AbstractByteBigList.this.getByte(i);
            }
            
            @Override
            protected final void add(final long i, final byte k) {
                AbstractByteBigList.this.add(i, k);
            }
            
            @Override
            protected final void set(final long i, final byte k) {
                AbstractByteBigList.this.set(i, k);
            }
            
            @Override
            protected final void remove(final long i) {
                AbstractByteBigList.this.removeByte(i);
            }
            
            @Override
            protected final long getMaxPos() {
                return AbstractByteBigList.this.size64();
            }
        };
    }
    
    @Override
    public IntSpliterator intSpliterator() {
        if (this instanceof RandomAccess) {
            return ByteSpliterators.widen(this.spliterator());
        }
        return super.intSpliterator();
    }
    
    @Override
    public boolean contains(final byte k) {
        return this.indexOf(k) >= 0L;
    }
    
    @Override
    public long indexOf(final byte k) {
        final ByteBigListIterator i = this.listIterator();
        while (i.hasNext()) {
            final byte e = i.nextByte();
            if (k == e) {
                return i.previousIndex();
            }
        }
        return -1L;
    }
    
    @Override
    public long lastIndexOf(final byte k) {
        final ByteBigListIterator i = this.listIterator(this.size64());
        while (i.hasPrevious()) {
            final byte e = i.previousByte();
            if (k == e) {
                return i.nextIndex();
            }
        }
        return -1L;
    }
    
    @Override
    public void size(final long size) {
        long i = this.size64();
        if (size > i) {
            while (i++ < size) {
                this.add((byte)0);
            }
        }
        else {
            while (i-- != size) {
                this.remove(i);
            }
        }
    }
    
    @Override
    public ByteBigList subList(final long from, final long to) {
        this.ensureIndex(from);
        this.ensureIndex(to);
        if (from > to) {
            throw new IndexOutOfBoundsException("Start index (" + from + ") is greater than end index (" + to + ")");
        }
        return (this instanceof RandomAccess) ? new ByteRandomAccessSubList(this, from, to) : new ByteSubList(this, from, to);
    }
    
    @Override
    public void forEach(final ByteConsumer action) {
        if (this instanceof RandomAccess) {
            for (long i = 0L, max = this.size64(); i < max; ++i) {
                action.accept(this.getByte(i));
            }
        }
        else {
            super.forEach(action);
        }
    }
    
    @Override
    public void removeElements(final long from, final long to) {
        this.ensureIndex(to);
        final ByteBigListIterator i = this.listIterator(from);
        long n = to - from;
        if (n < 0L) {
            throw new IllegalArgumentException("Start index (" + from + ") is greater than end index (" + to + ")");
        }
        while (n-- != 0L) {
            i.nextByte();
            i.remove();
        }
    }
    
    @Override
    public void addElements(long index, final byte[][] a, long offset, long length) {
        this.ensureIndex(index);
        BigArrays.ensureOffsetLength(a, offset, length);
        if (this instanceof RandomAccess) {
            while (length-- != 0L) {
                this.add(index++, BigArrays.get(a, offset++));
            }
        }
        else {
            final ByteBigListIterator iter = this.listIterator(index);
            while (length-- != 0L) {
                iter.add(BigArrays.get(a, offset++));
            }
        }
    }
    
    @Override
    public void addElements(final long index, final byte[][] a) {
        this.addElements(index, a, 0L, BigArrays.length(a));
    }
    
    @Override
    public void getElements(final long from, final byte[][] a, long offset, long length) {
        this.ensureIndex(from);
        BigArrays.ensureOffsetLength(a, offset, length);
        if (from + length > this.size64()) {
            throw new IndexOutOfBoundsException("End index (" + (from + length) + ") is greater than list size (" + this.size64() + ")");
        }
        if (this instanceof RandomAccess) {
            long current = from;
            while (length-- != 0L) {
                BigArrays.set(a, offset++, this.getByte(current++));
            }
        }
        else {
            final ByteBigListIterator i = this.listIterator(from);
            while (length-- != 0L) {
                BigArrays.set(a, offset++, i.nextByte());
            }
        }
    }
    
    @Override
    public void setElements(final long index, final byte[][] a, final long offset, final long length) {
        this.ensureIndex(index);
        BigArrays.ensureOffsetLength(a, offset, length);
        if (index + length > this.size64()) {
            throw new IndexOutOfBoundsException("End index (" + (index + length) + ") is greater than list size (" + this.size64() + ")");
        }
        if (this instanceof RandomAccess) {
            for (long i = 0L; i < length; ++i) {
                this.set(i + index, BigArrays.get(a, i + offset));
            }
        }
        else {
            final ByteBigListIterator iter = this.listIterator(index);
            long j = 0L;
            while (j < length) {
                iter.nextByte();
                iter.set(BigArrays.get(a, offset + j++));
            }
        }
    }
    
    @Override
    public void clear() {
        this.removeElements(0L, this.size64());
    }
    
    @Deprecated
    @Override
    public int size() {
        return (int)Math.min(2147483647L, this.size64());
    }
    
    @Override
    public int hashCode() {
        final ByteIterator i = this.iterator();
        int h = 1;
        long s = this.size64();
        while (s-- != 0L) {
            final byte k = i.nextByte();
            h = 31 * h + k;
        }
        return h;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof BigList)) {
            return false;
        }
        final BigList<?> l = (BigList<?>)o;
        long s = this.size64();
        if (s != l.size64()) {
            return false;
        }
        if (l instanceof ByteBigList) {
            final ByteBigListIterator i1 = this.listIterator();
            final ByteBigListIterator i2 = ((ByteBigList)l).listIterator();
            while (s-- != 0L) {
                if (i1.nextByte() != i2.nextByte()) {
                    return false;
                }
            }
            return true;
        }
        final BigListIterator<?> i3 = this.listIterator();
        final BigListIterator<?> i4 = l.listIterator();
        while (s-- != 0L) {
            if (!Objects.equals(i3.next(), i4.next())) {
                return false;
            }
        }
        return true;
    }
    
    @Override
    public int compareTo(final BigList<? extends Byte> l) {
        if (l == this) {
            return 0;
        }
        if (l instanceof ByteBigList) {
            final ByteBigListIterator i1 = this.listIterator();
            final ByteBigListIterator i2 = ((ByteBigList)l).listIterator();
            while (i1.hasNext() && i2.hasNext()) {
                final byte e1 = i1.nextByte();
                final byte e2 = i2.nextByte();
                final int r;
                if ((r = Byte.compare(e1, e2)) != 0) {
                    return r;
                }
            }
            return i2.hasNext() ? -1 : (i1.hasNext() ? 1 : 0);
        }
        final BigListIterator<? extends Byte> i3 = this.listIterator();
        final BigListIterator<? extends Byte> i4 = l.listIterator();
        while (i3.hasNext() && i4.hasNext()) {
            final int r;
            if ((r = i3.next().compareTo(i4.next())) != 0) {
                return r;
            }
        }
        return i4.hasNext() ? -1 : (i3.hasNext() ? 1 : 0);
    }
    
    @Override
    public void push(final byte o) {
        this.add(o);
    }
    
    @Override
    public byte popByte() {
        if (this.isEmpty()) {
            throw new NoSuchElementException();
        }
        return this.removeByte(this.size64() - 1L);
    }
    
    @Override
    public byte topByte() {
        if (this.isEmpty()) {
            throw new NoSuchElementException();
        }
        return this.getByte(this.size64() - 1L);
    }
    
    @Override
    public byte peekByte(final int i) {
        return this.getByte(this.size64() - 1L - i);
    }
    
    @Override
    public boolean rem(final byte k) {
        final long index = this.indexOf(k);
        if (index == -1L) {
            return false;
        }
        this.removeByte(index);
        return true;
    }
    
    @Override
    public boolean addAll(final long index, final ByteCollection c) {
        return this.addAll(index, (Collection<? extends Byte>)c);
    }
    
    @Override
    public boolean addAll(final ByteCollection c) {
        return this.addAll(this.size64(), c);
    }
    
    @Deprecated
    @Override
    public void add(final long index, final Byte ok) {
        this.add(index, (byte)ok);
    }
    
    @Deprecated
    @Override
    public Byte set(final long index, final Byte ok) {
        return this.set(index, (byte)ok);
    }
    
    @Deprecated
    @Override
    public Byte get(final long index) {
        return this.getByte(index);
    }
    
    @Deprecated
    @Override
    public long indexOf(final Object ok) {
        return this.indexOf((byte)ok);
    }
    
    @Deprecated
    @Override
    public long lastIndexOf(final Object ok) {
        return this.lastIndexOf((byte)ok);
    }
    
    @Deprecated
    @Override
    public Byte remove(final long index) {
        return this.removeByte(index);
    }
    
    @Deprecated
    @Override
    public void push(final Byte o) {
        this.push((byte)o);
    }
    
    @Deprecated
    @Override
    public Byte pop() {
        return this.popByte();
    }
    
    @Deprecated
    @Override
    public Byte top() {
        return this.topByte();
    }
    
    @Deprecated
    @Override
    public Byte peek(final int i) {
        return this.peekByte(i);
    }
    
    @Override
    public String toString() {
        final StringBuilder s = new StringBuilder();
        final ByteIterator i = this.iterator();
        long n = this.size64();
        boolean first = true;
        s.append("[");
        while (n-- != 0L) {
            if (first) {
                first = false;
            }
            else {
                s.append(", ");
            }
            final byte k = i.nextByte();
            s.append(String.valueOf(k));
        }
        s.append("]");
        return s.toString();
    }
    
    static final class IndexBasedSpliterator extends ByteBigSpliterators.LateBindingSizeIndexBasedSpliterator
    {
        final ByteBigList l;
        
        IndexBasedSpliterator(final ByteBigList l, final long pos) {
            super(pos);
            this.l = l;
        }
        
        IndexBasedSpliterator(final ByteBigList l, final long pos, final long maxPos) {
            super(pos, maxPos);
            this.l = l;
        }
        
        @Override
        protected final long getMaxPosFromBackingStore() {
            return this.l.size64();
        }
        
        @Override
        protected final byte get(final long i) {
            return this.l.getByte(i);
        }
        
        @Override
        protected final IndexBasedSpliterator makeForSplit(final long pos, final long maxPos) {
            return new IndexBasedSpliterator(this.l, pos, maxPos);
        }
    }
    
    public static class ByteSubList extends AbstractByteBigList implements Serializable
    {
        private static final long serialVersionUID = -7046029254386353129L;
        protected final ByteBigList l;
        protected final long from;
        protected long to;
        
        public ByteSubList(final ByteBigList l, final long from, final long to) {
            this.l = l;
            this.from = from;
            this.to = to;
        }
        
        private boolean assertRange() {
            assert this.from <= this.l.size64();
            assert this.to <= this.l.size64();
            assert this.to >= this.from;
            return true;
        }
        
        @Override
        public boolean add(final byte k) {
            this.l.add(this.to, k);
            ++this.to;
            assert this.assertRange();
            return true;
        }
        
        @Override
        public void add(final long index, final byte k) {
            this.ensureIndex(index);
            this.l.add(this.from + index, k);
            ++this.to;
            assert this.assertRange();
        }
        
        @Override
        public boolean addAll(final long index, final Collection<? extends Byte> c) {
            this.ensureIndex(index);
            this.to += c.size();
            return this.l.addAll(this.from + index, c);
        }
        
        @Override
        public byte getByte(final long index) {
            this.ensureRestrictedIndex(index);
            return this.l.getByte(this.from + index);
        }
        
        @Override
        public byte removeByte(final long index) {
            this.ensureRestrictedIndex(index);
            --this.to;
            return this.l.removeByte(this.from + index);
        }
        
        @Override
        public byte set(final long index, final byte k) {
            this.ensureRestrictedIndex(index);
            return this.l.set(this.from + index, k);
        }
        
        @Override
        public long size64() {
            return this.to - this.from;
        }
        
        @Override
        public void getElements(final long from, final byte[][] a, final long offset, final long length) {
            this.ensureIndex(from);
            if (from + length > this.size64()) {
                throw new IndexOutOfBoundsException("End index (" + from + length + ") is greater than list size (" + this.size64() + ")");
            }
            this.l.getElements(this.from + from, a, offset, length);
        }
        
        @Override
        public void removeElements(final long from, final long to) {
            this.ensureIndex(from);
            this.ensureIndex(to);
            this.l.removeElements(this.from + from, this.from + to);
            this.to -= to - from;
            assert this.assertRange();
        }
        
        @Override
        public void addElements(final long index, final byte[][] a, final long offset, final long length) {
            this.ensureIndex(index);
            this.l.addElements(this.from + index, a, offset, length);
            this.to += length;
            assert this.assertRange();
        }
        
        @Override
        public ByteBigListIterator listIterator(final long index) {
            this.ensureIndex(index);
            return (this.l instanceof RandomAccess) ? new RandomAccessIter(index) : new ParentWrappingIter(this.l.listIterator(index + this.from));
        }
        
        @Override
        public ByteSpliterator spliterator() {
            return (this.l instanceof RandomAccess) ? new IndexBasedSpliterator(this.l, this.from, this.to) : super.spliterator();
        }
        
        @Override
        public IntSpliterator intSpliterator() {
            if (this.l instanceof RandomAccess) {
                return ByteSpliterators.widen(this.spliterator());
            }
            return super.intSpliterator();
        }
        
        @Override
        public ByteBigList subList(final long from, final long to) {
            this.ensureIndex(from);
            this.ensureIndex(to);
            if (from > to) {
                throw new IllegalArgumentException("Start index (" + from + ") is greater than end index (" + to + ")");
            }
            return new ByteSubList(this, from, to);
        }
        
        @Override
        public boolean rem(final byte k) {
            final long index = this.indexOf(k);
            if (index == -1L) {
                return false;
            }
            --this.to;
            this.l.removeByte(this.from + index);
            assert this.assertRange();
            return true;
        }
        
        @Override
        public boolean addAll(final long index, final ByteCollection c) {
            return super.addAll(index, c);
        }
        
        @Override
        public boolean addAll(final long index, final ByteBigList l) {
            return super.addAll(index, l);
        }
        
        private final class RandomAccessIter extends ByteBigListIterators.AbstractIndexBasedBigListIterator
        {
            RandomAccessIter(final long pos) {
                super(0L, pos);
            }
            
            @Override
            protected final byte get(final long i) {
                return ByteSubList.this.l.getByte(ByteSubList.this.from + i);
            }
            
            @Override
            protected final void add(final long i, final byte k) {
                ByteSubList.this.add(i, k);
            }
            
            @Override
            protected final void set(final long i, final byte k) {
                ByteSubList.this.set(i, k);
            }
            
            @Override
            protected final void remove(final long i) {
                ByteSubList.this.removeByte(i);
            }
            
            @Override
            protected final long getMaxPos() {
                return ByteSubList.this.to - ByteSubList.this.from;
            }
            
            @Override
            public void add(final byte k) {
                super.add(k);
                assert ByteSubList.this.assertRange();
            }
            
            @Override
            public void remove() {
                super.remove();
                assert ByteSubList.this.assertRange();
            }
        }
        
        private class ParentWrappingIter implements ByteBigListIterator
        {
            private ByteBigListIterator parent;
            
            ParentWrappingIter(final ByteBigListIterator parent) {
                this.parent = parent;
            }
            
            @Override
            public long nextIndex() {
                return this.parent.nextIndex() - ByteSubList.this.from;
            }
            
            @Override
            public long previousIndex() {
                return this.parent.previousIndex() - ByteSubList.this.from;
            }
            
            @Override
            public boolean hasNext() {
                return this.parent.nextIndex() < ByteSubList.this.to;
            }
            
            @Override
            public boolean hasPrevious() {
                return this.parent.previousIndex() >= ByteSubList.this.from;
            }
            
            @Override
            public byte nextByte() {
                if (!this.hasNext()) {
                    throw new NoSuchElementException();
                }
                return this.parent.nextByte();
            }
            
            @Override
            public byte previousByte() {
                if (!this.hasPrevious()) {
                    throw new NoSuchElementException();
                }
                return this.parent.previousByte();
            }
            
            @Override
            public void add(final byte k) {
                this.parent.add(k);
            }
            
            @Override
            public void set(final byte k) {
                this.parent.set(k);
            }
            
            @Override
            public void remove() {
                this.parent.remove();
            }
            
            @Override
            public long back(final long n) {
                if (n < 0L) {
                    throw new IllegalArgumentException("Argument must be nonnegative: " + n);
                }
                final long currentPos = this.parent.previousIndex();
                long parentNewPos = currentPos - n;
                if (parentNewPos < ByteSubList.this.from - 1L) {
                    parentNewPos = ByteSubList.this.from - 1L;
                }
                final long toSkip = parentNewPos - currentPos;
                return this.parent.back(toSkip);
            }
            
            @Override
            public long skip(final long n) {
                if (n < 0L) {
                    throw new IllegalArgumentException("Argument must be nonnegative: " + n);
                }
                final long currentPos = this.parent.nextIndex();
                long parentNewPos = currentPos + n;
                if (parentNewPos > ByteSubList.this.to) {
                    parentNewPos = ByteSubList.this.to;
                }
                final long toSkip = parentNewPos - currentPos;
                return this.parent.skip(toSkip);
            }
        }
    }
    
    public static class ByteRandomAccessSubList extends ByteSubList implements RandomAccess
    {
        private static final long serialVersionUID = -107070782945191929L;
        
        public ByteRandomAccessSubList(final ByteBigList l, final long from, final long to) {
            super(l, from, to);
        }
        
        @Override
        public ByteBigList subList(final long from, final long to) {
            this.ensureIndex(from);
            this.ensureIndex(to);
            if (from > to) {
                throw new IllegalArgumentException("Start index (" + from + ") is greater than end index (" + to + ")");
            }
            return new ByteRandomAccessSubList(this, from, to);
        }
    }
}
