use of java.nio.ShortBuffer in project RoaringBitmap by RoaringBitmap.
the class ReverseMappeableArrayContainerShortIterator method inot.
@Override
public // not thread safe! (duh!)
MappeableContainer inot(final int firstOfRange, final int lastOfRange) {
// TODO: may need to convert to a RunContainer
// TODO: this can be optimized for performance
// determine the span of array indices to be affected
int startIndex = BufferUtil.unsignedBinarySearch(content, 0, cardinality, (short) firstOfRange);
if (startIndex < 0) {
startIndex = -startIndex - 1;
}
int lastIndex = BufferUtil.unsignedBinarySearch(content, 0, cardinality, (short) (lastOfRange - 1));
if (lastIndex < 0) {
lastIndex = -lastIndex - 1 - 1;
}
final int currentValuesInRange = lastIndex - startIndex + 1;
final int spanToBeFlipped = lastOfRange - firstOfRange;
final int newValuesInRange = spanToBeFlipped - currentValuesInRange;
final ShortBuffer buffer = ShortBuffer.allocate(newValuesInRange);
final int cardinalityChange = newValuesInRange - currentValuesInRange;
final int newCardinality = cardinality + cardinalityChange;
if (cardinalityChange > 0) {
// expansion, right shifting needed
if (newCardinality > content.limit()) {
// so big we need a bitmap?
if (newCardinality > DEFAULT_MAX_SIZE) {
return toBitmapContainer().inot(firstOfRange, lastOfRange);
}
final ShortBuffer co = ShortBuffer.allocate(newCardinality);
content.rewind();
co.put(content);
content = co;
}
// slide right the contents after the range
for (int pos = cardinality - 1; pos > lastIndex; --pos) {
content.put(pos + cardinalityChange, content.get(pos));
}
negateRange(buffer, startIndex, lastIndex, firstOfRange, lastOfRange);
} else {
// no expansion needed
negateRange(buffer, startIndex, lastIndex, firstOfRange, lastOfRange);
if (cardinalityChange < 0) {
// Leave array oversize
for (int i = startIndex + newValuesInRange; i < newCardinality; ++i) {
content.put(i, content.get(i - cardinalityChange));
}
}
}
cardinality = newCardinality;
return this;
}
use of java.nio.ShortBuffer in project RoaringBitmap by RoaringBitmap.
the class ReverseMappeableRunContainerShortIterator method copyValuesLength.
private void copyValuesLength(ShortBuffer src, int srcIndex, ShortBuffer dst, int dstIndex, int length) {
if (BufferUtil.isBackedBySimpleArray(src) && BufferUtil.isBackedBySimpleArray(dst)) {
// common case.
System.arraycopy(src.array(), 2 * srcIndex, dst.array(), 2 * dstIndex, 2 * length);
return;
}
// source and destination may overlap
// consider specialized code for various cases, rather than using a second buffer
ShortBuffer temp = ShortBuffer.allocate(2 * length);
for (int i = 0; i < 2 * length; ++i) {
temp.put(src.get(2 * srcIndex + i));
}
temp.flip();
for (int i = 0; i < 2 * length; ++i) {
dst.put(2 * dstIndex + i, temp.get());
}
}
use of java.nio.ShortBuffer in project RoaringBitmap by RoaringBitmap.
the class ReverseMappeableRunContainerShortIterator method increaseCapacity.
// not thread safe!
private void increaseCapacity() {
int newCapacity = (valueslength.capacity() == 0) ? DEFAULT_INIT_SIZE : valueslength.capacity() < 64 ? valueslength.capacity() * 2 : valueslength.capacity() < 1024 ? valueslength.capacity() * 3 / 2 : valueslength.capacity() * 5 / 4;
final ShortBuffer nv = ShortBuffer.allocate(newCapacity);
valueslength.rewind();
nv.put(valueslength);
valueslength = nv;
}
use of java.nio.ShortBuffer in project RoaringBitmap by RoaringBitmap.
the class ReverseMappeableRunContainerShortIterator method copyToOffset.
// Push all values length to the end of the array (resize array if needed)
private void copyToOffset(int offset) {
final int minCapacity = 2 * (offset + nbrruns);
if (valueslength.capacity() < minCapacity) {
// expensive case where we need to reallocate
int newCapacity = valueslength.capacity();
while (newCapacity < minCapacity) {
newCapacity = (newCapacity == 0) ? DEFAULT_INIT_SIZE : newCapacity < 64 ? newCapacity * 2 : newCapacity < 1024 ? newCapacity * 3 / 2 : newCapacity * 5 / 4;
}
ShortBuffer newvalueslength = ShortBuffer.allocate(newCapacity);
copyValuesLength(this.valueslength, 0, newvalueslength, offset, nbrruns);
this.valueslength = newvalueslength;
} else {
// efficient case where we just copy
copyValuesLength(this.valueslength, 0, this.valueslength, offset, nbrruns);
}
}
use of java.nio.ShortBuffer in project RoaringBitmap by RoaringBitmap.
the class ReverseMappeableRunContainerShortIterator method trim.
@Override
public void trim() {
if (valueslength.limit() == 2 * nbrruns) {
return;
}
if (BufferUtil.isBackedBySimpleArray(valueslength)) {
this.valueslength = ShortBuffer.wrap(Arrays.copyOf(valueslength.array(), 2 * nbrruns));
} else {
final ShortBuffer co = ShortBuffer.allocate(2 * nbrruns);
short[] a = co.array();
for (int k = 0; k < 2 * nbrruns; ++k) {
a[k] = this.valueslength.get(k);
}
this.valueslength = co;
}
}
Aggregations