/** * Create an array container with specified capacity * * @param capacity The capacity of the container */ public MappeableArrayContainer(final int capacity) { content = ShortBuffer.allocate(capacity); }
/** * Create an array container with specified capacity * * @param capacity The capacity of the container */ public MappeableRunContainer(final int capacity) { valueslength = ShortBuffer.allocate(2 * capacity); }
/** * Return the content of this container as a ShortBuffer. This creates a copy and might be * relatively slow. * * @return the ShortBuffer */ public ShortBuffer toShortBuffer() { ShortBuffer sb = ShortBuffer.allocate(this.nbrruns * 2); sb.put(this.valueslength, 0, this.nbrruns * 2); return sb; }
/** * Return the content of this container as a ShortBuffer. This creates a copy and might be * relatively slow. * * @return the ShortBuffer */ public ShortBuffer toShortBuffer() { ShortBuffer sb = ShortBuffer.allocate(this.cardinality); sb.put(this.content, 0, this.cardinality); return sb; }
@Generates private ShortBuffer generateShortBuffer() { return ShortBuffer.allocate(generateInt()); }
/** * Create an array container with a run of ones from firstOfRun to lastOfRun, exclusive. Caller is * responsible for making sure the range is small enough that ArrayContainer is appropriate. * * @param firstOfRun first index * @param lastOfRun last index (range is exclusive) */ public MappeableArrayContainer(final int firstOfRun, final int lastOfRun) { // TODO: this can be optimized for performance final int valuesInRange = lastOfRun - firstOfRun; content = ShortBuffer.allocate(valuesInRange); short[] sarray = content.array(); for (int i = 0; i < valuesInRange; ++i) { sarray[i] = (short) (firstOfRun + i); } cardinality = valuesInRange; }
private MappeableArrayContainer(int newCard, ShortBuffer newContent) { this.cardinality = newCard; ShortBuffer tmp = newContent.duplicate();// for thread-safety this.content = ShortBuffer.allocate(Math.max(newCard, tmp.limit())); tmp.rewind(); this.content.put(tmp); }
private MappeableRunContainer(int nbrruns, final ShortBuffer valueslength) { this.nbrruns = nbrruns; ShortBuffer tmp = valueslength.duplicate();// for thread safety this.valueslength = ShortBuffer.allocate(Math.max(2 * nbrruns, tmp.limit())); tmp.rewind(); this.valueslength.put(tmp); // may copy more than it needs to?? }
protected void ensureCapacity(int minNbRuns) { final int minCapacity = 2 * minNbRuns; if (valueslength.capacity() < minCapacity) { 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; } final ShortBuffer nv = ShortBuffer.allocate(newCapacity); valueslength.rewind(); nv.put(valueslength); valueslength = nv; } }
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()); } }
private void increaseCapacity(boolean allowIllegalSize) { int len = this.content.limit(); int newCapacity = (len == 0) ? DEFAULT_INIT_SIZE : len < 64 ? len * 2 : this.content.limit() < 1067 ? len * 3 / 2 : len * 5 / 4; // do not allocate more than we will ever need if (newCapacity > MappeableArrayContainer.DEFAULT_MAX_SIZE && !allowIllegalSize) { newCapacity = MappeableArrayContainer.DEFAULT_MAX_SIZE; } // if we are within 1/16th of the max., go to max right away to avoid further reallocations if (newCapacity > MappeableArrayContainer.DEFAULT_MAX_SIZE - MappeableArrayContainer.DEFAULT_MAX_SIZE / 16 && !allowIllegalSize) { newCapacity = MappeableArrayContainer.DEFAULT_MAX_SIZE; } final ShortBuffer newContent = ShortBuffer.allocate(newCapacity); this.content.rewind(); newContent.put(this.content); this.content = newContent; }
@Override public void trim() { if (this.content.limit() == this.cardinality) { return; } if (BufferUtil.isBackedBySimpleArray(content)) { this.content = ShortBuffer.wrap(Arrays.copyOf(content.array(), cardinality)); } else { final ShortBuffer co = ShortBuffer.allocate(this.cardinality); // can assume that new co is array backed short[] x = co.array(); for (int k = 0; k < this.cardinality; ++k) { x[k] = this.content.get(k); } this.content = co; } }
@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; } }
@Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { // little endian this.nbrruns = 0xFFFF & Short.reverseBytes(in.readShort()); if (this.valueslength.capacity() < 2 * this.nbrruns) { this.valueslength = ShortBuffer.allocate(2 * this.nbrruns); } for (int k = 0; k < 2 * this.nbrruns; ++k) { this.valueslength.put(k, Short.reverseBytes(in.readShort())); } }
@Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { // little endian this.cardinality = 0xFFFF & Short.reverseBytes(in.readShort()); if (this.content.limit() < this.cardinality) { this.content = ShortBuffer.allocate(this.cardinality); } for (int k = 0; k < this.cardinality; ++k) { this.content.put(k, Short.reverseBytes(in.readShort())); } }
@Test(expected = RuntimeException.class) public void throwsExceptionCopyPixelsToShortBuffer() { Bitmap bitmapOriginal = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888); ShortBuffer buffer = ShortBuffer.allocate(bitmapOriginal.getByteCount()); bitmapOriginal.copyPixelsToBuffer(buffer); }
@Test(expected = RuntimeException.class) public void throwsExceptionCopyPixelsFromShortBuffer() { Bitmap bitmapOriginal = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888); ShortBuffer buffer = ShortBuffer.allocate(bitmapOriginal.getByteCount()); bitmapOriginal.copyPixelsFromBuffer(buffer); }
/** * Creates a new ShortBuffer with the same contents as the given * ShortBuffer. The new ShortBuffer is separate from the old one and changes * are not reflected across. If you want to reflect changes, consider using * Buffer.duplicate(). * * @param buf * the ShortBuffer to copy * @return the copy */ public static ShortBuffer clone(ShortBuffer buf) { if (buf == null) { return null; } buf.rewind(); ShortBuffer copy; if (isDirect(buf)) { copy = createShortBuffer(buf.limit()); } else { copy = ShortBuffer.allocate(buf.limit()); } copy.put(buf); return copy; }
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; }
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); } }