File: uint64-array2int_vector.cpp

package info (click to toggle)
libsdsl 2.1.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,992 kB
  • sloc: cpp: 42,286; makefile: 1,171; ansic: 318; sh: 201; python: 27
file content (75 lines) | stat: -rw-r--r-- 2,257 bytes parent folder | download | duplicates (19)
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
/*
 * Transforms a sequence of 64-bit integers into a bit-compressed integer vector.
 * The first command line parameter argv[1] specifies the file, which contains the sequence
 * of integers.
 * The bit-compressed integer vector is stored in a file called `argv[1].int_vector`.
 */
#include <sdsl/util.hpp>
#include <sdsl/int_vector.hpp>
#include <iostream>
#include <string>

using namespace sdsl;
using namespace std;

int main(int argc, char* argv[])
{
    if (argc < 2) {
        cout << "Usage: " << argv[0] << " int_file" << endl;
        return 1;
    }

    size_t x = util::file_size(argv[1]);
    const int BPI=8;
    cout<<"file size in bytes = "<<x<<endl;
    if (x % BPI != 0) {
        cout << "Error: x%"<<BPI<<" != 0"<<endl;
        return 1;
    }
//	(1) scan file and determine the largest value
    int_vector_buffer<64> ivb(string(argv[1]), std::ios::in, 6400000, 64, true);
    if (!ivb.is_open()) {
        cout<<"ERROR: could not open file "<<argv[1]<<endl;
        return 1;
    }
    uint64_t max=0;
    for (size_t i=0; i<ivb.size(); ++i) {
        if (ivb[i] > max)
            max = ivb[i];
    }
    cout<<"Max value: "<<max<<endl;
    uint8_t width = bits::hi(max)+1;
    cout<<"width="<<(int)width<<endl;

//  (2) scan file, bit-compress values and write to outfile
    string ofile = string(argv[1])+".int_vector";
    int_vector_buffer<> ivb2(ofile, std::ios::out, 6400000, width);
    if (!ivb2.is_open()) {
        cout<<"ERROR: could not open output file "<<argv[1]<<endl;
        return 1;
    }
    for (size_t i=0; i<ivb.size(); ++i) {
        ivb2[i] = ivb[i];
    }
    ivb.close();
    ivb2.close();

    int_vector<> v;
    load_from_file(v, ofile);
    cout<<"v.size()="<<v.size()<<endl;
    cout<<"v[0]="<<v[0]<<endl;
    const bool do_check = false;
    if (do_check) {
        int_vector<> check;
        load_vector_from_file(check, argv[1], BPI);
        if (check.size() != v.size()) {
            cout<<"Vectors differ in size: "<<check.size()<<"!="<<v.size()<<endl;
            return 1;
        }
        for (size_t i=0; i<check.size(); ++i) {
            if (check[i] != v[i]) {
                cout<<"vectors differ in position "<<i<<": "<<check[i]<<"!="<<v[i]<<endl;
            }
        }
    }
}