📄 booleanbuffer.java
字号:
throw new IllegalArgumentException("Attempt to insert a negative number of objects.");
if(n == 0)
return;
if(capacity() - size >= n)
{
BooleanIterator b = new BooleanIterator(this, size + n);
Algorithms.Copying.copyBackward(pos, new BooleanIterator(this, size), b);
Algorithms.Filling.fill(pos, new BooleanIterator(this, pos.index() + n), object);
size += n;
} else
{
BooleanBuffer tmp = new BooleanBuffer(new ByteBuffer(new byte[size + Math.max(size, n)], false));
Algorithms.Copying.copy(storage.begin(), new ByteIterator(storage, pos.index() >= size ? pos.getInternalIndex() : pos.getInternalIndex() + 1), tmp.storage.begin());
BooleanIterator i = new BooleanIterator(tmp, pos.index());
while(n-- > 0)
{
i.put(object);
i.advance();
}
Algorithms.Copying.copy(pos, new BooleanIterator(this, size), i);
copy(tmp);
}
}
public void insert(BooleanIterator pos, int n, boolean object)
{
insert(pos, n, new Boolean(object));
}
public void insert(int index, int n, Object object)
{
AbstractArrayAdapter.checkIndex(index, size + 1);
insert(new BooleanIterator(this, index), n, object);
}
public synchronized void insert(int index, int n, boolean object)
{
insert(index, n, new Boolean(object));
}
public void insert(BooleanIterator pos, ForwardIterator first, ForwardIterator last)
{
int n = first.distance(last);
if(n == 0)
return;
if(capacity() - size >= n)
{
BooleanIterator b = new BooleanIterator(this, size + n);
Algorithms.Copying.copyBackward(pos, new BooleanIterator(this, size), b);
Algorithms.Copying.copy(first, last, pos);
size += n;
} else
{
BooleanBuffer tmp = new BooleanBuffer(new ByteBuffer(new byte[size + Math.max(size, n)], false));
Algorithms.Copying.copy(storage.begin(), new ByteIterator(storage, pos.index() >= size ? pos.getInternalIndex() : pos.getInternalIndex() + 1), tmp.storage.begin());
BooleanIterator i = new BooleanIterator(tmp, pos.index());
Algorithms.Copying.copy(first, last, i);
i.advance(n);
Algorithms.Copying.copy(pos, new BooleanIterator(this, size), i);
copy(tmp);
}
}
public synchronized void insert(int index, ForwardIterator first, ForwardIterator last)
{
AbstractArrayAdapter.checkIndex(index, size + 1);
insert(new BooleanIterator(this, index), first, last);
}
public synchronized void swap(BooleanBuffer array)
{
synchronized(array)
{
int oldSize = size;
ByteBuffer oldStorage = storage;
size = array.size;
storage = array.storage;
array.size = oldSize;
array.storage = oldStorage;
}
}
public synchronized BidirectionalIterator begin()
{
return new BooleanIterator(this, 0);
}
public synchronized BidirectionalIterator end()
{
return new BooleanIterator(this, size);
}
public synchronized void trimToSize()
{
if(size() < capacity())
copy(new BooleanBuffer(get()));
}
public synchronized void ensureCapacity(int n)
{
if(n < 0)
throw new IllegalArgumentException("Attempt to reserve a negative size.");
if(capacity() < n)
storage.ensureCapacity(n / 8 + 1);
}
public synchronized Object popFront()
{
if(size() == 0)
{
throw new NoSuchElementException("BooleanBuffer is empty");
} else
{
Object result = get(0);
remove(0);
return result;
}
}
public void pushFront(Object object)
{
insert(0, object);
}
public void pushFront(boolean object)
{
insert(0, object);
}
public boolean remove(Object object)
{
return remove(object, 1) > 0;
}
public synchronized int remove(Object object, int count)
{
boolean tmp = asBoolean(object);
int removed = 0;
while(count > 0)
{
int i = indexOf(tmp);
if(i < 0)
break;
count--;
removed++;
remove(i);
}
return removed;
}
public synchronized int remove(int first, int last, Object object)
{
asBoolean(object);
if(last < first)
{
return 0;
} else
{
AbstractArrayAdapter.checkRange(first, last, size);
BooleanIterator firstx = new BooleanIterator(this, first);
BooleanIterator lastx = new BooleanIterator(this, last + 1);
BooleanIterator finish = (BooleanIterator)Algorithms.Removing.remove(firstx, lastx, object);
return remove(finish.index(), last);
}
}
public int replace(Object oldValue, Object newValue)
{
return replace(asBoolean(oldValue), asBoolean(newValue));
}
public int replace(boolean oldValue, boolean newValue)
{
return replace(0, size(), oldValue, newValue);
}
public int replace(int first, int last, Object oldValue, Object newValue)
{
if(last < first)
{
return -1;
} else
{
AbstractArrayAdapter.checkRange(first, last - 1, size);
return Algorithms.Replacing.replace(new BooleanIterator(this, first), new BooleanIterator(this, last), oldValue, newValue);
}
}
public synchronized int replace(int first, int last, boolean oldValue, boolean newValue)
{
return replace(first, last, new Boolean(oldValue), new Boolean(newValue));
}
public int count(Object object)
{
return Algorithms.Counting.count(begin(), end(), object);
}
public int count(boolean object)
{
return count(new Boolean(object));
}
public synchronized int count(int first, int last, Object object)
{
if(last < first)
{
return -1;
} else
{
AbstractArrayAdapter.checkRange(first, last, size);
int index = ((BooleanIterator)Algorithms.Finding.find(new BooleanIterator(this, first), new BooleanIterator(this, last + 1), object)).index();
return index != last + 1 ? index : -1;
}
}
public int count(int first, int last, boolean object)
{
return count(first, last, new Boolean(object));
}
public int indexOf(boolean object)
{
return indexOf(0, size() - 1, object);
}
public int indexOf(int first, int last, Object object)
{
if(last < first)
{
return -1;
} else
{
AbstractArrayAdapter.checkRange(first, last, size);
int index = ((BooleanIterator)Algorithms.Finding.find(new BooleanIterator(this, first), new BooleanIterator(this, last + 1), object)).index();
return index != last + 1 ? index : -1;
}
}
public int indexOf(int first, int last, boolean object)
{
return indexOf(first, last, new Boolean(object));
}
public void setSize(int newSize)
{
if(size() > newSize)
remove(newSize, size() - 1);
else
if(size() < newSize)
insert(size(), newSize - size(), false);
}
public boolean contains(boolean object)
{
return indexOf(object) != -1;
}
private void insertAux(BooleanIterator iter, boolean b)
{
if(atEnd())
{
BooleanBuffer tmp = new BooleanBuffer(new ByteBuffer(new byte[AbstractArrayAdapter.getNextSize(size)], false));
Algorithms.Copying.copy(storage.begin(), new ByteIterator(storage, iter.index() >= size ? iter.getInternalIndex() : iter.getInternalIndex() + 1), tmp.storage.begin());
BooleanIterator i = new BooleanIterator(tmp, iter.index());
i.put(b);
i.advance();
Algorithms.Copying.copy(iter, new BooleanIterator(this, size), i);
copy(tmp);
} else
{
Algorithms.Copying.copyBackward(iter, new BooleanIterator(this, size + 1), new BooleanIterator(this, size));
iter.put(b);
}
}
static final boolean asBoolean(Object b)
{
return ((Boolean)b).booleanValue();
}
private final boolean atEnd()
{
return size / 8 == storage.size();
}
ByteBuffer storage;
int size;
static final long serialVersionUID = 0xf0e9120f5fbd2ba8L;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -