-
Notifications
You must be signed in to change notification settings - Fork 124
/
example.cpp
90 lines (85 loc) · 2.62 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// A simple example to get you started with the library.
// You can compile and run this example like so:
//
// make example
// ./example
//
// Warning: If your compiler does not fully support C++11, some of
// this example may require changes.
//
#include "headers/codecfactory.h"
#include "headers/deltautil.h"
int main() {
using namespace FastPForLib;
CODECFactory factory;
// We pick a CODEC
IntegerCODEC &codec = *factory.getFromName("simdfastpfor256");
// could use others, e.g., "simdbinarypacking", "varintg8iu"
////////////
//
// create a container with some integers in it
//
// for this example, we will not assume that the
// integers are in sorted order
//
// (Note: You don't need to use a vector.)
//
size_t N = 10 * 1000;
std::vector<uint32_t> mydata(N);
for (uint32_t i = 0; i < N; i += 150)
mydata[i] = i;
//
// the vector mydata could contain anything, really
//
///////////
//
// You need some "output" container. You are responsible
// for allocating enough memory.
//
std::vector<uint32_t> compressed_output(N + 1024);
// N+1024 should be plenty
//
//
size_t compressedsize = compressed_output.size();
codec.encodeArray(mydata.data(), mydata.size(), compressed_output.data(),
compressedsize);
//
// if desired, shrink back the array:
compressed_output.resize(compressedsize);
compressed_output.shrink_to_fit();
// display compression rate:
std::cout << std::setprecision(3);
std::cout << "You are using "
<< 32.0 * static_cast<double>(compressed_output.size()) /
static_cast<double>(mydata.size())
<< " bits per integer. " << std::endl;
//
// You are done!... with the compression...
//
///
// decompressing is also easy:
//
std::vector<uint32_t> mydataback(N);
size_t recoveredsize = mydataback.size();
//
codec.decodeArray(compressed_output.data(), compressed_output.size(),
mydataback.data(), recoveredsize);
mydataback.resize(recoveredsize);
//
// That's it!
//
if (mydataback != mydata)
throw std::runtime_error("bug!");
// If you need to use differential coding, you can use
// calls like these to get the deltas and recover the original
// data from the deltas:
Delta::deltaSIMD(mydata.data(), mydata.size());
Delta::inverseDeltaSIMD(mydata.data(), mydata.size());
// be mindful of CPU caching issues
// If you do use differential coding a lot, you might want
// to check out these other libraries...
// https://github.com/lemire/FastDifferentialCoding
// and
// https://github.com/lemire/SIMDCompressionAndIntersection
}