|
1 | 1 | import com.googlecode.javaewah.EWAHCompressedBitmap;
|
| 2 | + |
2 | 3 | import java.io.*;
|
| 4 | +import java.nio.ByteBuffer; |
3 | 5 |
|
4 | 6 | /**
|
5 |
| - * @author lemire |
6 |
| - * |
7 |
| - */ |
8 |
| -public class example { |
9 |
| - |
10 |
| - /** |
11 |
| - * @param args arguments from the command line |
12 |
| - * @throws IOException if an IO error occurs |
| 7 | + * Simple illustrative example. |
| 8 | + * |
| 9 | + * @author Daniel Lemire |
| 10 | + * |
13 | 11 | */
|
14 |
| -public static void main(final String[] args) throws Exception { |
15 |
| - EWAHCompressedBitmap ewahBitmap1 = EWAHCompressedBitmap.bitmapOf(0,2,55,64,1<<30); |
16 |
| - EWAHCompressedBitmap ewahBitmap2 = EWAHCompressedBitmap.bitmapOf(1,3,64,1<<30); |
17 |
| - System.out.println("bitmap 1: "+ewahBitmap1); |
18 |
| - System.out.println("bitmap 2: "+ewahBitmap2); |
19 |
| - // or |
20 |
| - EWAHCompressedBitmap orbitmap = ewahBitmap1.or(ewahBitmap2); |
21 |
| - System.out.println("bitmap 1 OR bitmap 2: "+orbitmap); |
22 |
| - System.out.println("memory usage: " + orbitmap.sizeInBytes() + " bytes"); |
23 |
| - // and |
24 |
| - EWAHCompressedBitmap andbitmap = ewahBitmap1.and(ewahBitmap2); |
25 |
| - System.out.println("bitmap 1 AND bitmap 2: "+andbitmap); |
26 |
| - System.out.println("memory usage: " + andbitmap.sizeInBytes() + " bytes"); |
27 |
| - // xor |
28 |
| - EWAHCompressedBitmap xorbitmap = ewahBitmap1.xor(ewahBitmap2); |
29 |
| - System.out.println("bitmap 1 XOR bitmap 2:"+xorbitmap); |
30 |
| - System.out.println("memory usage: " + xorbitmap.sizeInBytes() + " bytes"); |
31 |
| - // fast aggregation over many bitmaps |
32 |
| - EWAHCompressedBitmap ewahBitmap3 = EWAHCompressedBitmap.bitmapOf(5,55,1<<30); |
33 |
| - EWAHCompressedBitmap ewahBitmap4 = EWAHCompressedBitmap.bitmapOf(4,66,1<<30); |
34 |
| - System.out.println("bitmap 3: "+ewahBitmap3); |
35 |
| - System.out.println("bitmap 4: "+ewahBitmap4); |
36 |
| - andbitmap = EWAHCompressedBitmap.and(ewahBitmap1,ewahBitmap2, |
37 |
| - ewahBitmap3,ewahBitmap4); |
38 |
| - System.out.println("b1 AND b2 AND b3 AND b4: "+andbitmap); |
39 |
| - // serialization |
40 |
| - ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
41 |
| - // Note: you could use a file output steam instead of ByteArrayOutputStream |
42 |
| - ObjectOutputStream oo = new ObjectOutputStream(bos); |
43 |
| - ewahBitmap1.writeExternal(oo); |
44 |
| - oo.close(); |
45 |
| - ewahBitmap1 = null; |
46 |
| - ewahBitmap1 = new EWAHCompressedBitmap(); |
47 |
| - ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
48 |
| - ewahBitmap1.readExternal(new ObjectInputStream(bis)); |
49 |
| - System.out.println("bitmap 1 (recovered) : "+ewahBitmap1); |
50 |
| - // |
51 |
| - // support for threshold function (new as of version 0.8.0): |
52 |
| - // mark as true a bit that occurs at least T times in the source |
53 |
| - // bitmaps |
54 |
| - // |
55 |
| - EWAHCompressedBitmap threshold2 = EWAHCompressedBitmap.threshold(2, ewahBitmap1,ewahBitmap2, |
56 |
| - ewahBitmap3,ewahBitmap4); |
57 |
| - System.out.println("threshold 2 : "+threshold2); |
| 12 | +public class example |
| 13 | +{ |
| 14 | + |
| 15 | + /** |
| 16 | + * @param args |
| 17 | + * arguments from the command line |
| 18 | + * @throws IOException |
| 19 | + * if an IO error occurs |
| 20 | + */ |
| 21 | + public static void main(final String[] args) throws Exception { |
| 22 | + EWAHCompressedBitmap ewahBitmap1 = EWAHCompressedBitmap.bitmapOf(0, 2, 55, |
| 23 | + 64, 1 << 30); |
| 24 | + EWAHCompressedBitmap ewahBitmap2 = EWAHCompressedBitmap.bitmapOf(1, 3, 64, |
| 25 | + 1 << 30); |
| 26 | + System.out.println("bitmap 1: " + ewahBitmap1); |
| 27 | + System.out.println("bitmap 2: " + ewahBitmap2); |
| 28 | + // or |
| 29 | + EWAHCompressedBitmap orbitmap = ewahBitmap1.or(ewahBitmap2); |
| 30 | + System.out.println("bitmap 1 OR bitmap 2: " + orbitmap); |
| 31 | + System.out.println("memory usage: " + orbitmap.sizeInBytes() + " bytes"); |
| 32 | + // and |
| 33 | + EWAHCompressedBitmap andbitmap = ewahBitmap1.and(ewahBitmap2); |
| 34 | + System.out.println("bitmap 1 AND bitmap 2: " + andbitmap); |
| 35 | + System.out.println("memory usage: " + andbitmap.sizeInBytes() + " bytes"); |
| 36 | + // xor |
| 37 | + EWAHCompressedBitmap xorbitmap = ewahBitmap1.xor(ewahBitmap2); |
| 38 | + System.out.println("bitmap 1 XOR bitmap 2:" + xorbitmap); |
| 39 | + System.out.println("memory usage: " + xorbitmap.sizeInBytes() + " bytes"); |
| 40 | + // fast aggregation over many bitmaps |
| 41 | + EWAHCompressedBitmap ewahBitmap3 = EWAHCompressedBitmap.bitmapOf(5, 55, |
| 42 | + 1 << 30); |
| 43 | + EWAHCompressedBitmap ewahBitmap4 = EWAHCompressedBitmap.bitmapOf(4, 66, |
| 44 | + 1 << 30); |
| 45 | + System.out.println("bitmap 3: " + ewahBitmap3); |
| 46 | + System.out.println("bitmap 4: " + ewahBitmap4); |
| 47 | + andbitmap = EWAHCompressedBitmap.and(ewahBitmap1, ewahBitmap2, ewahBitmap3, |
| 48 | + ewahBitmap4); |
| 49 | + System.out.println("b1 AND b2 AND b3 AND b4: " + andbitmap); |
| 50 | + // serialization |
| 51 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 52 | + // Note: you could use a file output steam instead of ByteArrayOutputStream |
| 53 | + ewahBitmap1.serialize(new DataOutputStream(bos)); |
| 54 | + EWAHCompressedBitmap ewahBitmap1new = new EWAHCompressedBitmap(); |
| 55 | + byte[] bout = bos.toByteArray(); |
| 56 | + ewahBitmap1new.deserialize(new DataInputStream(new ByteArrayInputStream( |
| 57 | + bout))); |
| 58 | + System.out.println("bitmap 1 (recovered) : " + ewahBitmap1new); |
| 59 | + if (!ewahBitmap1.equals(ewahBitmap1new)) |
| 60 | + throw new RuntimeException("Will not happen"); |
| 61 | + // |
| 62 | + // we can use a ByteBuffer as backend for a bitmap |
| 63 | + // which allows memory-mapped bitmaps |
| 64 | + // |
| 65 | + ByteBuffer bb = ByteBuffer.wrap(bout); |
| 66 | + EWAHCompressedBitmap rmap = new EWAHCompressedBitmap(bb); |
| 67 | + System.out.println("bitmap 1 (mapped) : " + rmap); |
58 | 68 |
|
| 69 | + if (!rmap.equals(ewahBitmap1)) |
| 70 | + throw new RuntimeException("Will not happen"); |
| 71 | + // |
| 72 | + // support for threshold function (new as of version 0.8.0): |
| 73 | + // mark as true a bit that occurs at least T times in the source |
| 74 | + // bitmaps |
| 75 | + // |
| 76 | + EWAHCompressedBitmap threshold2 = EWAHCompressedBitmap.threshold(2, |
| 77 | + ewahBitmap1, ewahBitmap2, ewahBitmap3, ewahBitmap4); |
| 78 | + System.out.println("threshold 2 : " + threshold2); |
59 | 79 |
|
60 |
| - } |
| 80 | + } |
61 | 81 |
|
62 | 82 | }
|
0 commit comments