SDSL: Succinct Data Structure Library
A C++ template library for succinct data structures
 All Classes Namespaces Files Functions Variables Typedefs Friends
sdsl/include/sdsl/template_class.hpp
Go to the documentation of this file.
00001 
00005 #ifndef INCLUDED_SDSL_TEMPLATE_CLASS // replace TEMPLATE_CLASS by our class name
00006 #define INCLUDED_SDSL_TEMPLATE_CLASS // in this include guard
00007 
00008 #include "bit_vectors.hpp" // include your required sdsl header here
00009 #include <iostream>               // then include other header, e.g. from the STL
00010 
00012 namespace sdsl
00013 {
00014 
00016 
00028 template<class bit_vector_type=bit_vector>
00029 class template_class
00030 {
00031     public:
00032         typedef bit_vector::size_type size_type; // put public typedefs here
00033         typedef bit_vector_type
00034     private:
00035         size_type               m_size; // member variables are prefixed with ``m_''
00036         bit_vector_type m_data; // use descriptive names for the variables
00037 
00038     public:
00039         // Define a default constructor. Don't forget to initialize all members,
00040         // which are not initialized by default. E.g. m_size in this case.
00041         template_class():m_size(0) {}
00042 
00043         // User defined operator.
00044         explicit template_class(bit_vector b):m_size(b.size()) {
00045             bit_vector data(b.size()/2, 0);
00046             for (size_type i=0; i+1 < b.size(); i+=2) {
00047                 data[i] = b[2*i] & b[2*i+1];
00048             }
00049             util::assign(m_data, data);
00050         }
00051 
00052         // copy constructor
00053         template_class(const template_class& t) {
00054             if (this != &t) { // if it is not the same object
00055                 m_size = t.m_size;
00056                 m_data = t.m_data;
00057             }
00058             return *this;
00059         }
00060 
00061         // You should implement the swap operator. It is used to swap two variables
00062         // of type template_class in constant time.
00063         void swap(const template_class& t) {
00064             std::swap(m_size, t.m_size); // use std::swap to swap variables of basic primitive types
00065             m_data.swap(t.m_data); // use the swap method for composite data types like sdsl or stl classes
00066         }
00067 
00068         // The serialize method writes the data structure into a stream ``out'' and returns
00069         // the number of written bytes.
00070         void serialize(std::ostream& out)const {
00071             size_type written_bytes = 0;
00072             // variables of basic primitive types can be written using util::write_member
00073             written_bytes += util::write_member(m_size, out);
00074             // other sdsl classes can be written by calling their serialize method
00075             written_bytes += m_data.serialize(out);
00076             return written_bytes;
00077         }
00078 
00079         // The load method reads the data structure from a stream ``in''. Note that
00080         // the members have to be read in the same order in which they were written.
00081         void load(std::istream& in) {
00082             // variables of basic primitive types can be read using util::read_member
00083             util::read_member(m_size, in);
00084             // other sdsl classes can be read by calling their load method
00085             m_data.load(in);
00086         }
00087 
00088         // user defined methods or operators
00089         bool operator[](size_type i)const {
00090             return m_data[i/2];
00091         }
00092 };
00093 
00094 }
00095 #endif