SDSL 3.0.2
Succinct Data Structure Library
Loading...
Searching...
No Matches
int_vector_io_wrappers.hpp
Go to the documentation of this file.
1// Copyright (c) 2016, the SDSL Project Authors. All rights reserved.
2// Please see the AUTHORS file for details. Use of this source code is governed
3// by a BSD license that can be found in the LICENSE file.
18#ifndef INCLUDE_SDSL_INT_VECTOR_IO_WRAPPERS
19#define INCLUDE_SDSL_INT_VECTOR_IO_WRAPPERS
20
21#include <iostream>
22#include <stdint.h>
23#include <string>
24
26#include <sdsl/int_vector.hpp>
28#include <sdsl/util.hpp>
29
30namespace sdsl
31{
32
33template <uint8_t fixedIntWidth = 0>
35{
36public:
40
41private:
42 int_vector_type const & m_vec;
43
44public:
46 {}
47
48 size_type serialize(std::ostream & out, structure_tree_node * v = nullptr, std::string name = "") const
49 {
50 structure_tree_node * child = structure_tree::add_child(v, name, util::class_name(*this));
51 size_type written_bytes = 0;
52 // (1) write size and int_width
53 written_bytes += _sdsl_serialize_size_and_int_width(out, fixedIntWidth, m_vec.width(), m_vec.bit_size());
54 // (2) write entries in vbyte coding
55 for (size_type i = 0; i < m_vec.size(); ++i)
56 {
57 value_type ww = m_vec[i];
58 uint8_t w = ww & 0x7F;
59 ww >>= 7;
60 while (ww > 0)
61 {
62 w |= 0x80; // mark overflow bit
63 out.write((char const *)&w, sizeof(uint8_t)); // write byte
64 w = ww & 0x7F;
65 ww >>= 7;
66 ++written_bytes;
67 }
68 out.write((char const *)&w, sizeof(uint8_t)); // write without overflow bit
69 ++written_bytes;
70 }
71 structure_tree::add_size(child, written_bytes);
72 return written_bytes;
73 }
74};
75
76template <uint8_t fixedIntWidth = 0>
78{
79public:
83
84private:
85 int_vector_type & m_vec;
86
87public:
90
91 void load(std::istream & in)
92 {
94 typename int_vector_type::int_width_type int_width;
95 // (1) read size and int_width
97 // (2) resize the vector
98 m_vec.width(int_width);
99 m_vec.bit_resize(size);
100 // (3) read vbyte encoded entries an put them into the vector
101 size_type i = 0;
102 while (i < m_vec.size())
103 {
104 value_type ww = 0;
105 uint8_t w = 0;
106 value_type shift = 0;
107 do
108 {
109 in.read((char *)&w, sizeof(uint8_t));
110 ww |= (((value_type)(w & 0x7F)) << shift);
111 shift += 7;
112 }
113 while ((w & 0x80) > 0);
114 m_vec[i++] = ww;
115 }
116 }
117};
118
119template <class coder_type = coder::elias_delta<>>
121{
122public:
126
127private:
128 int_vector_type const & m_vec;
129
130public:
132 {}
133
134 size_type serialize(std::ostream & out, structure_tree_node * v = nullptr, std::string name = "") const
135 {
136 structure_tree_node * child = structure_tree::add_child(v, name, util::class_name(*this));
137 size_type written_bytes = 0;
138 int_vector_type enc_vec;
139 coder_type::encode(m_vec, enc_vec);
140 written_bytes += enc_vec.serialize(out, child, "enc_vector");
141 structure_tree::add_size(child, written_bytes);
142 return written_bytes;
143 }
144};
145
146template <class coder_type = coder::elias_delta<>>
148{
149public:
153
154private:
155 int_vector_type & m_vec;
156
157public:
160
161 void load(std::istream & in)
162 {
163 int_vector_type enc_vec;
164 enc_vec.load(in);
165 coder_type::decode(enc_vec, m_vec);
166 }
167};
168
169template <class int_vector_type = int_vector<>>
171{
172public:
173 typedef typename int_vector_type::size_type size_type;
174
175private:
176 int_vector_type const & m_vec;
177
178public:
179 int_vector_serialize_wrapper(int_vector_type const & vec) : m_vec(vec)
180 {}
181
182 size_type serialize(std::ostream & out, structure_tree_node * v = nullptr, std::string name = "") const
183 {
184 return m_vec.serialize(out, v, name);
185 }
186};
187
188template <class int_vector_type = int_vector<>>
190{
191public:
192 typedef typename int_vector_type::size_type size_type;
193
194private:
195 int_vector_type & m_vec;
196
197public:
198 int_vector_load_wrapper(int_vector_type & vec) : m_vec(vec)
199 {}
200 void load(std::istream & in)
201 {
202 m_vec.load(in);
203 }
204};
205
206template <class int_vector_serialize_wrapper_type = int_vector_serialize_wrapper<>>
209
210} // namespace sdsl
211
212#endif // end include guard
int_vector< fixedIntWidth > int_vector_type
int_vector_type::size_type size_type
int_vector_load_wrapper(int_vector_type &vec)
int_vector_serialize_vbyte_wrapper(int_vector_type const &vec)
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
int_vector_serialize_vlen_wrapper(int_vector_type const &vec)
int_vector_serialize_wrapper(int_vector_type const &vec)
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
A generic vector class for integers of width .
int_vector_size_type size_type
int_vector_trait< t_width >::int_width_type int_width_type
size_type bit_size() const noexcept
The number of bits in the int_vector.
void load(std::istream &in)
Load the int_vector for a stream.
uint8_t width() const noexcept
Returns the width of the integers which are accessed via the [] operator.
size_type size() const noexcept
The number of elements in the int_vector.
int_vector_trait< t_width >::value_type value_type
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
Serializes the int_vector to a stream.
static structure_tree_node * add_child(structure_tree_node *v, std::string const &name, std::string const &type)
static void add_size(structure_tree_node *v, uint64_t value)
coder_elias_delta.hpp contains the class sdsl::coder::elias_delta
int_vector.hpp contains the sdsl::int_vector class.
Namespace for the succinct data structure library.
int_vector ::size_type size(range_type const &r)
Size of a range.
structure_tree.hpp contains a helper class which can represent the memory structure of a class.
util.hpp contains some helper methods for int_vector and other stuff like demangle class names.