File: int-vector-mapper.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 (119 lines) | stat: -rw-r--r-- 3,670 bytes parent folder | download | duplicates (17)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <sdsl/int_vector_mapper.hpp>
#include <string>
#include <iostream>

using namespace sdsl;
using namespace std;

int main(int argc, char* argv[])
{
    if (argc < 1) {
        cout << "Usage: " << argv[0] << endl;
        cout << "(1) Writes an int_vector sequentially to a file" << endl;
        cout << "(2) Streams the content from file" << endl;
        cout << "(3) Remove the file" << endl;
        return 1;
    }
    string tmp_file = "tmp_file.sdsl";
    size_t size = 10000000;
    std::mt19937_64 rng(13);
    uint8_t width = 0;
    int_vector<> iv(size,0,64);
    int_vector<64> ivf(size,0);
    std::vector<uint64_t> stdv(size,0);

    // (1) write an int vector to disk
    {
        // write sequentially random values to disk
        for (uint64_t i=0; i<size; ++i) {
            iv[i] = rng();
            stdv[i] = iv[i];
            ivf[i] = iv[i];
        }

        util::bit_compress(iv);
        width = iv.width();
        store_to_file(iv,tmp_file);
    }

    // (2) open readonly! memory map the content of tmp_file
    {
        const int_vector_mapper<0,std::ios_base::in> ivm(tmp_file);
        if (ivm.size() != size) {
            std::cerr << "ERROR: ivm.size()="<< ivm.size() << " != " << size << std::endl;
            return 1;
        }
        if (ivm.width() != width) {
            std::cerr << "ERROR: ivm.width()="<< ivm.width() << " != " << width << std::endl;
            return 1;
        }
        rng.seed(13); // To get the same values than before use the same seed
        for (uint64_t i=0; i<ivm.size(); ++i) {
            uint64_t expected_value = rng();
            if (ivm[i] != expected_value) {
                std::cerr << "ERROR: ivm["<< i << "]=" << ivm[i] << " != " << expected_value << "= expected_value" << std::endl;
                return 1;
            }
        }

        if (ivm != stdv) {
            std::cerr << "ERROR: std::vector CMP failed.";
        }
        if (ivm != iv) {
            std::cerr << "ERROR: iv CMP failed.";
        }
        if (ivm != ivf) {
            std::cerr << "ERROR: ivf CMP failed.";
        }
    }

    // (2) open read+write! memory map the content of tmp_file
    {
        int_vector_mapper<0> ivm(tmp_file);
        if (ivm.size() != size) {
            std::cerr << "ERROR: ivm.size()="<< ivm.size() << " != " << size << std::endl;
            return 1;
        }
        if (ivm.width() != width) {
            std::cerr << "ERROR: ivm.width()="<< ivm.width() << " != " << width << std::endl;
            return 1;
        }
        rng.seed(13); // To get the same values than before use the same seed
        for (uint64_t i=0; i<ivm.size(); ++i) {
            uint64_t expected_value = rng();
            if (ivm[i] != expected_value) {
                std::cerr << "ERROR: ivm["<< i << "]=" << ivm[i] << " != " << expected_value << "= expected_value" << std::endl;
                return 1;
            }
        }

        if (ivm != stdv) {
            std::cerr << "ERROR: std::vector CMP failed.";
        }
        if (ivm != iv) {
            std::cerr << "ERROR: iv CMP failed.";
        }
        if (ivm != ivf) {
            std::cerr << "ERROR: ivf CMP failed.";
        }
    }

    // (3) remove the file as the mapper does not do that if we don't specify it
    {
        sdsl::remove(tmp_file);
    }

    {
        auto tmp_buf = temp_file_buffer<64>::create();
        for (const auto& val : stdv) {
            tmp_buf.push_back(val);
        }
        if (tmp_buf != stdv) {
            std::cerr << "ERROR: tmp_buf CMP failed." << std::endl;
        }

        // tmp buf file is deleted automatically
    }

    return 0;
}