Skip to content

Commit 16caafa

Browse files
committed
Adding an example.
1 parent ce4bde3 commit 16caafa

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Really simple usage:
5656
int[] recov = iic.uncompress(compressed); // equals to data
5757
```
5858

59-
For more examples, see example.java.
59+
For more examples, see example.java or the examples folder.
6060

6161
Some CODECs ("integrated codecs") assume that the integers are
6262
in sorted orders and use differential coding (they compress deltas).
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import java.io.*;
2+
import java.nio.file.*;
3+
import java.util.*;
4+
import java.util.zip.*;
5+
6+
import me.lemire.integercompression.differential.*;
7+
import me.lemire.integercompression.*;
8+
9+
10+
public class CompressBitmap {
11+
12+
public static void main(String[] args) throws IOException {
13+
if(args.length == 0) {
14+
System.out.println("usage: please provide the file name of a bitmap binary file.");
15+
return;
16+
}
17+
System.out.println("loading file "+args[0]+" as a bitmap");
18+
int[] data = fromBitsetFileToArray(args[0]);
19+
System.out.println("Compressing "+data.length+" integers");
20+
int[] compressed = iic.compress(data);
21+
int[] recov = iic.uncompress(compressed);
22+
System.out.println("compressed from "+data.length*4/1024+"KB to "+compressed.length*4/1024+"KB");
23+
System.out.println("ratio: "+Math.round(data.length*1.0/compressed.length));
24+
25+
if(!Arrays.equals(recov,data)) throw new RuntimeException("bug");
26+
27+
long bef,aft;
28+
bef = System.nanoTime();
29+
recov = iic.uncompress(compressed);
30+
aft = System.nanoTime();
31+
32+
System.out.println("decoding speed:"+Math.round(data.length*1000.0/(aft-bef))+" millions of integers per second");
33+
34+
35+
bef = System.nanoTime();
36+
compressed = iic.compress(data);
37+
aft = System.nanoTime();
38+
39+
System.out.println("encoding speed:"+Math.round(data.length*1000.0/(aft-bef))+" millions of integers per second");
40+
41+
System.out.println("note: with a bit of effort, speed can be much higher.");
42+
43+
44+
System.out.println();
45+
zipStats(args[0]);
46+
47+
48+
}
49+
50+
static IntegratedIntCompressor iic = new IntegratedIntCompressor(
51+
new SkippableIntegratedComposition(
52+
new IntegratedBinaryPacking(),
53+
new IntegratedVariableByte()));
54+
55+
public static int[] fromBitsetFileToArray(String filename) throws IOException {
56+
Path path = Paths.get(filename);
57+
byte[] data = Files.readAllBytes(path);
58+
// we determine cardinality
59+
int card = 0;
60+
for(int k = 0 ; k < data.length; ++k) {
61+
int bv = data[k] & 0xFF;
62+
card += Integer.bitCount(bv);
63+
}
64+
int[] answer = new int[card];
65+
int pos = 0;
66+
for(int k = 0 ; k < data.length; ++k) {
67+
int bv = data[k] & 0xFF;
68+
for(int b = 0 ; b < 8; ++b)
69+
if ( ( (bv >> b) & 1 ) == 1) {
70+
answer[pos++] = b + k * 8;
71+
}
72+
}
73+
if(pos != card) throw new RuntimeException("bug");
74+
return answer;
75+
}
76+
77+
public static void zipStats(String filename) throws IOException {
78+
Path path = Paths.get(filename);
79+
byte[] input = Files.readAllBytes(path);
80+
System.out.println("I will try to compress the original bitmap using zip.");
81+
82+
long bef = System.nanoTime();
83+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
84+
ZipOutputStream zos = new ZipOutputStream(baos);
85+
zos.setLevel(9);
86+
ZipEntry entry = new ZipEntry(filename);
87+
entry.setSize(input.length);
88+
zos.putNextEntry(entry);
89+
zos.write(input);
90+
zos.closeEntry();
91+
zos.close();
92+
byte[] result = baos.toByteArray();
93+
long aft = System.nanoTime();
94+
System.out.println("zip encoding speed:"+input.length*1000.0/(aft-bef)+" million of bytes per second");
95+
System.out.println("zip compression ratio at best level : "+input.length * 1.0 / result.length);
96+
}
97+
}

examples/Axelbrooke/README.md

Whitespace-only changes.
2.82 MB
Binary file not shown.

examples/Axelbrooke/run.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
echo "please be patient as I build the library and the example"
3+
mvn -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -f ../../pom.xml package > /dev/null && javac -cp "../../target/*" CompressBitmap.java && java -cp "../../target/*":. CompressBitmap example_bitmap.bin

0 commit comments

Comments
 (0)