SDSL  3.0.0
Succinct Data Structure Library
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 
27 namespace sdsl
28 {
29 
30 template <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 
74 template <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 
116 template <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 
144 template <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 
168 template <class int_vector_type = int_vector<>>
170 {
171  public:
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 
188 template <class int_vector_type = int_vector<>>
190 {
191  public:
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 
204 template <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:253
int_vector_size_type size_type
Definition: int_vector.hpp:266
int_vector_trait< t_width >::int_width_type int_width_type
Definition: int_vector.hpp:267
size_type bit_size() const noexcept
The number of bits in the int_vector.
Definition: int_vector.hpp:571
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:619
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:255
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
Serializes the int_vector to a stream.
static void add_size(structure_tree_node *v, uint64_t value)
static structure_tree_node * add_child(structure_tree_node *v, const std::string &name, const std::string &type)
coder.hpp contains the coder namespace and includes the header files of sdsl::coder::fibonacci,...
int_vector.hpp contains the sdsl::int_vector class.
int_vector ::size_type size_type
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.