@Override public MappeableContainer or(final MappeableArrayContainer value2) { final MappeableArrayContainer value1 = this; final int totalCardinality = value1.getCardinality() + value2.getCardinality(); if (totalCardinality > DEFAULT_MAX_SIZE) { // it could be a bitmap! final MappeableBitmapContainer bc = new MappeableBitmapContainer(); if (!BufferUtil.isBackedBySimpleArray(bc.bitmap)) throw new RuntimeException("Should not happen. Internal bug."); long[] bitArray = bc.bitmap.array(); if (BufferUtil.isBackedBySimpleArray(value2.content)) { short[] sarray = value2.content.array(); for (int k = 0; k < value2.cardinality; ++k) { final int i = BufferUtil.toIntUnsigned(sarray[k]) >>> 6; bitArray[i] |= (1l << sarray[k]); } } else for (int k = 0; k < value2.cardinality; ++k) { short v2 = value2.content.get(k); final int i = BufferUtil.toIntUnsigned(v2) >>> 6; bitArray[i] |= (1l << v2); } if (BufferUtil.isBackedBySimpleArray(this.content)) { short[] sarray = this.content.array(); for (int k = 0; k < this.cardinality; ++k) { final int i = BufferUtil.toIntUnsigned(sarray[k]) >>> 6; bitArray[i] |= (1l << sarray[k]); } } else for (int k = 0; k < this.cardinality; ++k) { final int i = BufferUtil.toIntUnsigned(this.content.get(k)) >>> 6; bitArray[i] |= (1l << this.content.get(k)); } bc.cardinality = 0; int len = bc.bitmap.limit(); for (int index = 0; index < len; ++index) { bc.cardinality += Long.bitCount(bitArray[index]); } if (bc.cardinality <= DEFAULT_MAX_SIZE) return bc.toArrayContainer(); return bc; } final MappeableArrayContainer answer = new MappeableArrayContainer(totalCardinality); if (BufferUtil.isBackedBySimpleArray(value1.content) && BufferUtil.isBackedBySimpleArray(value2.content)) answer.cardinality = org.roaringbitmap.Util.unsignedUnion2by2( value1.content.array(), value1.getCardinality(), value2.content.array(), value2.getCardinality(), answer.content.array()); else answer.cardinality = BufferUtil.unsignedUnion2by2( value1.content, value1.getCardinality(), value2.content, value2.getCardinality(), answer.content.array()); return answer; }
@Override public MappeableArrayContainer and(final MappeableArrayContainer value2) { MappeableArrayContainer value1 = this; final int desiredCapacity = Math.min(value1.getCardinality(), value2.getCardinality()); MappeableArrayContainer answer = new MappeableArrayContainer(desiredCapacity); if (BufferUtil.isBackedBySimpleArray(this.content) && BufferUtil.isBackedBySimpleArray(value2.content)) answer.cardinality = org.roaringbitmap.Util.unsignedIntersect2by2( value1.content.array(), value1.getCardinality(), value2.content.array(), value2.getCardinality(), answer.content.array()); else answer.cardinality = BufferUtil.unsignedIntersect2by2( value1.content, value1.getCardinality(), value2.content, value2.getCardinality(), answer.content.array()); return answer; }
@Override public MappeableContainer flip(short x) { if (BufferUtil.isBackedBySimpleArray(this.content)) { short[] sarray = content.array(); int loc = Util.unsignedBinarySearch(sarray, 0, cardinality, x); if (loc < 0) { // Transform the ArrayContainer to a BitmapContainer // when cardinality = DEFAULT_MAX_SIZE if (cardinality >= DEFAULT_MAX_SIZE) { MappeableBitmapContainer a = this.toBitmapContainer(); a.add(x); return a; } if (cardinality >= sarray.length) { increaseCapacity(); sarray = content.array(); } // insertion : shift the elements > x by one position to // the right // and put x in it's appropriate place System.arraycopy(sarray, -loc - 1, sarray, -loc, cardinality + loc + 1); sarray[-loc - 1] = x; ++cardinality; } else { System.arraycopy(sarray, loc + 1, sarray, loc, cardinality - loc - 1); --cardinality; } return this; } else { int loc = BufferUtil.unsignedBinarySearch(content, 0, cardinality, x); if (loc < 0) { // Transform the ArrayContainer to a BitmapContainer // when cardinality = DEFAULT_MAX_SIZE if (cardinality >= DEFAULT_MAX_SIZE) { MappeableBitmapContainer a = this.toBitmapContainer(); a.add(x); return a; } if (cardinality >= content.limit()) { increaseCapacity(); } // insertion : shift the elements > x by one position to // the right // and put x in it's appropriate place for (int k = cardinality; k > -loc - 1; --k) content.put(k, content.get(k - 1)); content.put(-loc - 1, x); ++cardinality; } else { for (int k = loc + 1; k < cardinality; --k) { content.put(k - 1, content.get(k)); } --cardinality; } return this; } }
@Override public MappeableContainer remove(final short x) { if (BufferUtil.isBackedBySimpleArray(this.content)) { final int loc = Util.unsignedBinarySearch(content.array(), 0, cardinality, x); if (loc >= 0) { // insertion System.arraycopy(content.array(), loc + 1, content.array(), loc, cardinality - loc - 1); --cardinality; } return this; } else { final int loc = BufferUtil.unsignedBinarySearch(content, 0, cardinality, x); if (loc >= 0) { // insertion for (int k = loc + 1; k < cardinality; --k) { content.put(k - 1, content.get(k)); } --cardinality; } return this; } }
@Override public MappeableArrayContainer iandNot(final MappeableArrayContainer value2) { if (!BufferUtil.isBackedBySimpleArray(this.content)) throw new RuntimeException("Should not happen. Internal bug."); if (BufferUtil.isBackedBySimpleArray(value2.content)) this.cardinality = org.roaringbitmap.Util.unsignedDifference( this.content.array(), this.getCardinality(), value2.content.array(), value2.getCardinality(), this.content.array()); else this.cardinality = BufferUtil.unsignedDifference( this.content, this.getCardinality(), value2.content, value2.getCardinality(), this.content.array()); return this; }
@Override public void advanceIfNeeded(short minval) { pos = Util.advanceUntil(content, pos - 1, parent.cardinality, minval); }