SDSL: Succinct Data Structure Library
A C++ template library for succinct data structures
 All Classes Namespaces Files Functions Variables Typedefs Friends
sdsl/include/sdsl/int_vector_io_wrappers.hpp
Go to the documentation of this file.
00001 
00014 #ifndef INCLUDE_SDSL_INT_VECTOR_IO_WRAPPERS
00015 #define INCLUDE_SDSL_INT_VECTOR_IO_WRAPPERS
00016 
00017 #include <sdsl/int_vector.hpp>
00018 #include <sdsl/util.hpp>
00019 #include <sdsl/coder.hpp>
00020 
00021 #include <iostream>
00022 
00023 namespace sdsl
00024 {
00025 
00026 template<uint8_t fixedIntWidth=0, class size_type_class=int_vector<0>::size_type>
00027 class int_vector_serialize_vbyte_wrapper
00028 {
00029     public:
00030         typedef int_vector<fixedIntWidth, size_type_class> int_vector_type;
00031         typedef typename int_vector_type::size_type size_type;
00032         typedef typename int_vector_type::value_type value_type;
00033 
00034     private:
00035         const int_vector_type& m_vec;
00036 
00037     public:
00038         int_vector_serialize_vbyte_wrapper(const int_vector_type& vec):m_vec(vec) {}
00039 
00040         size_type serialize(std::ostream& out, structure_tree_node* v=NULL, std::string name="")const {
00041             structure_tree_node* child = structure_tree::add_child(v, name, util::class_name(*this));
00042             size_type written_bytes = 0;
00043             // (1) write size and int_width
00044             written_bytes += _sdsl_serialize_size_and_int_width(out, fixedIntWidth, m_vec.get_int_width(), m_vec.bit_size());
00045             // (2) write entries in vbyte coding
00046             for (size_type i=0; i < m_vec.size(); ++i) {
00047                 value_type ww = m_vec[i];
00048                 uint8_t w = ww & 0x7F;
00049                 ww >>= 7;
00050                 while (ww > 0) {
00051                     w |= 0x80; // mark overflow bit
00052                     out.write((const char*)&w, sizeof(uint8_t));  // write byte
00053                     w = ww & 0x7F;
00054                     ww >>= 7;
00055                     ++written_bytes;
00056                 }
00057                 out.write((const char*)&w, sizeof(uint8_t));  // write without overflow bit
00058                 ++written_bytes;
00059             }
00060             structure_tree::add_size(child, written_bytes);
00061             return written_bytes;
00062         }
00063 };
00064 
00065 template<uint8_t fixedIntWidth=0, class size_type_class=int_vector<0>::size_type>
00066 class int_vector_load_vbyte_wrapper
00067 {
00068     public:
00069         typedef int_vector<fixedIntWidth, size_type_class> int_vector_type;
00070         typedef typename int_vector_type::size_type size_type;
00071         typedef typename int_vector_type::value_type value_type;
00072 
00073     private:
00074         int_vector_type& m_vec;
00075 
00076     public:
00077         int_vector_load_vbyte_wrapper(int_vector_type& vec):m_vec(vec) {}
00078 
00079         void load(std::istream& in) {
00080             size_type size;
00081             typename int_vector_type::int_width_type int_width;
00082             // (1) read size and int_width
00083             int_vector_trait<fixedIntWidth, size_type_class>::read_header(size, int_width, in);
00084             // (2) resize the vector
00085             m_vec.set_int_width(int_width);
00086             m_vec.bit_resize(size);
00087             // (3) read vbyte encoded entries an put them into the vector
00088             size_type i = 0;
00089             while (i < m_vec.size()) {
00090                 value_type ww=0;
00091                 uint8_t w=0;
00092                 value_type shift=0;
00093                 do {
00094                     in.read((char*)&w, sizeof(uint8_t));
00095                     ww |= (((value_type)(w&0x7F))<<shift);
00096                     shift += 7;
00097                 } while ((w&0x80) > 0);
00098                 m_vec[ i++ ] = ww;
00099             }
00100         }
00101 };
00102 
00103 template<class coder_type=coder::elias_delta>
00104 class int_vector_serialize_vlen_wrapper
00105 {
00106     public:
00107         typedef int_vector<> int_vector_type;
00108         typedef typename int_vector_type::size_type size_type;
00109         typedef typename int_vector_type::value_type value_type;
00110 
00111     private:
00112         const int_vector_type& m_vec;
00113 
00114     public:
00115         int_vector_serialize_vlen_wrapper(const int_vector_type& vec):m_vec(vec) {}
00116 
00117         size_type serialize(std::ostream& out, structure_tree_node* v=NULL, std::string name="")const {
00118             structure_tree_node* child = structure_tree::add_child(v, name, util::class_name(*this));
00119             size_type written_bytes = 0;
00120             int_vector_type enc_vec;
00121             coder_type::encode(m_vec, enc_vec);
00122             written_bytes += enc_vec.serialize(out, child, "enc_vector");
00123             structure_tree::add_size(child, written_bytes);
00124             return written_bytes;
00125         }
00126 };
00127 
00128 template<class coder_type=coder::elias_delta>
00129 class int_vector_load_vlen_wrapper
00130 {
00131     public:
00132         typedef int_vector<> int_vector_type;
00133         typedef typename int_vector_type::size_type size_type;
00134         typedef typename int_vector_type::value_type value_type;
00135 
00136     private:
00137         int_vector_type& m_vec;
00138 
00139     public:
00140         int_vector_load_vlen_wrapper(int_vector_type& vec):m_vec(vec) {}
00141 
00142         void load(std::istream& in) {
00143             int_vector_type enc_vec;
00144             enc_vec.load(in);
00145             coder_type::decode(enc_vec, m_vec);
00146         }
00147 };
00148 
00149 
00150 template<class int_vector_type=int_vector<> >
00151 class int_vector_serialize_wrapper
00152 {
00153     public:
00154         typedef typename int_vector_type::size_type size_type;
00155 
00156     private:
00157         const int_vector_type& m_vec;
00158 
00159     public:
00160         int_vector_serialize_wrapper(const int_vector_type& vec):m_vec(vec) {}
00161 
00162         size_type serialize(std::ostream& out, structure_tree_node* v=NULL, std::string name="")const {
00163             return m_vec.serialize(out, v, name);
00164         }
00165 };
00166 
00167 template<class int_vector_type=int_vector<> >
00168 class int_vector_load_wrapper
00169 {
00170     public:
00171         typedef typename int_vector_type::size_type size_type;
00172     private:
00173         int_vector_type& m_vec;
00174 
00175     public:
00176         int_vector_load_wrapper(int_vector_type& vec):m_vec(vec) {}
00177         void load(std::istream& in) {
00178             m_vec.load(in);
00179         }
00180 };
00181 
00182 template<class int_vector_serialize_wrapper_type=int_vector_serialize_wrapper<> >
00183 class int_vector_serialize_min_overhead
00184 {
00185 
00186 };
00187 
00188 } // end namespace
00189 
00190 #endif // end include guard