/** * Simple algorithm that computes the OR aggregate. * * @param bitmaps input bitmaps * @return new bitmap containing the aggregate */ public static EWAHCompressedBitmap32 or(final EWAHCompressedBitmap32... bitmaps) { PriorityQueue<EWAHCompressedBitmap32> pq = new PriorityQueue<EWAHCompressedBitmap32>( bitmaps.length, new Comparator<EWAHCompressedBitmap32>() { @Override public int compare(EWAHCompressedBitmap32 a, EWAHCompressedBitmap32 b) { return a.sizeInBytes() - b.sizeInBytes(); } }); Collections.addAll(pq, bitmaps); if (pq.isEmpty()) return new EWAHCompressedBitmap32(); while (pq.size() > 1) { EWAHCompressedBitmap32 x1 = pq.poll(); EWAHCompressedBitmap32 x2 = pq.poll(); pq.add(x1.or(x2)); } return pq.poll(); }
/** * Simple algorithm that computes the OR aggregate. * * @param bitmaps input bitmaps * @return new bitmap containing the aggregate */ public static EWAHCompressedBitmap32 or(final Iterator<EWAHCompressedBitmap32> bitmaps) { PriorityQueue<EWAHCompressedBitmap32> pq = new PriorityQueue<EWAHCompressedBitmap32>( 32, new Comparator<EWAHCompressedBitmap32>() { @Override public int compare(EWAHCompressedBitmap32 a, EWAHCompressedBitmap32 b) { return a.sizeInBytes() - b.sizeInBytes(); } }); while (bitmaps.hasNext()) pq.add(bitmaps.next()); if (pq.isEmpty()) return new EWAHCompressedBitmap32(); while (pq.size() > 1) { EWAHCompressedBitmap32 x1 = pq.poll(); EWAHCompressedBitmap32 x2 = pq.poll(); pq.add(x1.or(x2)); } return pq.poll(); }
/** * Uses a priority queue to compute the or aggregate. * * <p>The content of the container is overwritten. * * <p>This algorithm runs in linearithmic time (O(n log n)) with respect to the number of bitmaps. * * @param container where we write the result * @param bitmaps to be aggregated */ public static void orToContainer( final BitmapStorage32 container, final EWAHCompressedBitmap32... bitmaps) { if (bitmaps.length < 2) throw new IllegalArgumentException("We need at least two bitmaps"); PriorityQueue<EWAHCompressedBitmap32> pq = new PriorityQueue<EWAHCompressedBitmap32>( bitmaps.length, new Comparator<EWAHCompressedBitmap32>() { @Override public int compare(EWAHCompressedBitmap32 a, EWAHCompressedBitmap32 b) { return a.sizeInBytes() - b.sizeInBytes(); } }); Collections.addAll(pq, bitmaps); while (pq.size() > 2) { EWAHCompressedBitmap32 x1 = pq.poll(); EWAHCompressedBitmap32 x2 = pq.poll(); pq.add(x1.or(x2)); } pq.poll().orToContainer(pq.poll(), container); }