SDSL 3.0.1
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
23#include <sdsl/coder.hpp>
24#include <sdsl/int_vector.hpp>
25#include <sdsl/util.hpp>
26
27namespace sdsl
28{
29
30template <uint8_t fixedIntWidth = 0>
32{
33 public:
37
38 private:
39 const int_vector_type & m_vec;
40
41 public:
43 : m_vec(vec)
44 {}
45
46 size_type serialize(std::ostream & out, structure_tree_node * v = nullptr, std::string name = "") const
47 {
48 structure_tree_node * child = structure_tree::add_child(v, name, util::class_name(*this));
49 size_type written_bytes = 0;
50 // (1) write size and int_width
51 written_bytes += _sdsl_serialize_size_and_int_width(out, fixedIntWidth, m_vec.width(), m_vec.bit_size());
52 // (2) write entries in vbyte coding
53 for (size_type i = 0; i < m_vec.size(); ++i)
54 {
55 value_type ww = m_vec[i];
56 uint8_t w = ww & 0x7F;
57 ww >>= 7;
58 while (ww > 0)
59 {
60 w |= 0x80; // mark overflow bit
61 out.write((const char *)&w, sizeof(uint8_t)); // write byte
62 w = ww & 0x7F;
63 ww >>= 7;
64 ++written_bytes;
65 }
66 out.write((const char *)&w, sizeof(uint8_t)); // write without overflow bit
67 ++written_bytes;
68 }
69 structure_tree::add_size(child, written_bytes);
70 return written_bytes;
71 }
72};
73
74template <uint8_t fixedIntWidth = 0>
76{
77 public:
81
82 private:
83 int_vector_type & m_vec;
84
85 public:
87 : m_vec(vec)
88 {}
89
90 void load(std::istream & in)
91 {
93 typename int_vector_type::int_width_type int_width;
94 // (1) read size and int_width
96 // (2) resize the vector
97 m_vec.width(int_width);
98 m_vec.bit_resize(size);
99 // (3) read vbyte encoded entries an put them into the vector
100 size_type i = 0;
101 while (i < m_vec.size())
102 {
103 value_type ww = 0;
104 uint8_t w = 0;
105 value_type shift = 0;
106 do {
107 in.read((char *)&w, sizeof(uint8_t));
108 ww |= (((value_type)(w & 0x7F)) << shift);
109 shift += 7;
110 } while ((w & 0x80) > 0);
111 m_vec[i++] = ww;
112 }
113 }
114};
115
116template <class coder_type = coder::elias_delta<>>
118{
119 public:
123
124 private:
125 const int_vector_type & m_vec;
126
127 public:
129 : m_vec(vec)
130 {}
131
132 size_type serialize(std::ostream & out, structure_tree_node * v = nullptr, std::string name = "") const
133 {
134 structure_tree_node * child = structure_tree::add_child(v, name, util::class_name(*this));
135 size_type written_bytes = 0;
136 int_vector_type enc_vec;
137 coder_type::encode(m_vec, enc_vec);
138 written_bytes += enc_vec.serialize(out, child, "enc_vector");
139 structure_tree::add_size(child, written_bytes);
140 return written_bytes;
141 }
142};
143
144template <class coder_type = coder::elias_delta<>>
146{
147 public:
151
152 private:
153 int_vector_type & m_vec;
154
155 public:
157 : m_vec(vec)
158 {}
159
160 void load(std::istream & in)
161 {
162 int_vector_type enc_vec;
163 enc_vec.load(in);
164 coder_type::decode(enc_vec, m_vec);
165 }
166};
167
168template <class int_vector_type = int_vector<>>
170{
171 public:
172 typedef typename int_vector_type::size_type size_type;
173
174 private:
175 const int_vector_type & m_vec;
176
177 public:
178 int_vector_serialize_wrapper(const int_vector_type & vec)
179 : 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{
191 public:
192 typedef typename int_vector_type::size_type size_type;
193
194 private:
195 int_vector_type & m_vec;
196
197 public:
198 int_vector_load_wrapper(int_vector_type & vec)
199 : m_vec(vec)
200 {}
201 void load(std::istream & in) { m_vec.load(in); }
202};
203
204template <class int_vector_serialize_wrapper_type = int_vector_serialize_wrapper<>>
206{};
207
208} // namespace sdsl
209
210#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)
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
int_vector_serialize_vbyte_wrapper(const int_vector_type &vec)
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
int_vector_serialize_vlen_wrapper(const int_vector_type &vec)
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
int_vector_serialize_wrapper(const int_vector_type &vec)
A generic vector class for integers of width .
Definition: int_vector.hpp:216
int_vector_size_type size_type
Definition: int_vector.hpp:229
int_vector_trait< t_width >::int_width_type int_width_type
Definition: int_vector.hpp:230
size_type bit_size() const noexcept
The number of bits in the int_vector.
Definition: int_vector.hpp:547
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.
Definition: int_vector.hpp:595
size_type size() const noexcept
The number of elements in the int_vector.
int_vector_trait< t_width >::value_type value_type
Definition: int_vector.hpp:221
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, const std::string &name, const std::string &type)
static void add_size(structure_tree_node *v, uint64_t value)
coder.hpp contains the coder namespace and includes the header files of sdsl::coder::fibonacci,...
int_vector.hpp contains the sdsl::int_vector class.
Namespace for the succinct data structure library.
int_vector ::size_type size(const range_type &r)
Size of a range.
Definition: wt_helper.hpp:787
util.hpp contains some helper methods for int_vector and other stuff like demangle class names.