SDSL: Succinct Data Structure Library
A C++ template library for succinct data structures
 All Classes Namespaces Files Functions Variables Typedefs Friends
sdsl/include/sdsl/lcp_support_tree.hpp
00001 #ifndef INCLUDED_SDSL_SUPPORT_LCP_TREE
00002 #define INCLUDED_SDSL_SUPPORT_LCP_TREE
00003 
00004 #include "lcp.hpp"
00005 #include "util.hpp"
00006 #include "algorithms_for_compressed_suffix_trees.hpp"
00007 #include "lcp_wt.hpp"
00008 #include <iostream>
00009 #include <string>
00010 
00011 namespace sdsl
00012 {
00013 
00014 
00021 template<class Lcp, class Cst>
00022 class _lcp_support_tree
00023 {
00024     public:
00025         typedef typename Lcp::value_type                                                value_type;      // STL Container requirement
00026         typedef random_access_const_iterator<_lcp_support_tree> const_iterator;  // STL Container requirement
00027         typedef const_iterator                                                                  iterator;                // STL Container requirement
00028         typedef const value_type                                                                const_reference;
00029         typedef const_reference                                                                 reference;
00030         typedef const_reference*                                                                pointer;
00031         typedef const pointer                                                                   const_pointer;
00032         typedef typename Lcp::size_type                                                 size_type;               // STL Container requirement
00033         typedef typename Lcp::difference_type                                   difference_type; // STL Container requirement
00034 
00035         typedef lcp_tree_compressed_tag                                 lcp_category;
00036 
00037         enum { fast_access = 0,
00038                text_order = Lcp::text_order,
00039                sa_order = Lcp::sa_order
00040              };
00041 
00042         template<class CST>  // template inner class which is used in CSTs to parametrize lcp classes
00043         class type           // with information about the CST. Thanks Stefan Arnold! (2011-03-02)
00044         {
00045             public:
00046                 typedef _lcp_support_tree lcp_type;
00047         };
00048 
00049     private:
00050 
00051         const Cst* m_cst;
00052         Lcp        m_lcp;
00053 
00054         void copy(const _lcp_support_tree& lcp_c) {
00055             m_cst = lcp_c.m_cst;
00056             m_lcp = lcp_c.m_lcp; // works for lcp_bitcompressed and lcp_kurtz
00057         }
00058 
00059     public:
00060 
00062         _lcp_support_tree() {}
00063 
00064         // Destructor
00065         ~_lcp_support_tree() {}
00066 
00068         _lcp_support_tree(const _lcp_support_tree& lcp) {
00069             m_cst = lcp.m_cst;
00070             m_lcp = lcp.m_lcp;
00071         }
00072 
00074         template<class Text, class Sa>
00075         _lcp_support_tree(const Text& text, const Sa& sa, const Cst* cst);
00076         // TODO: implement
00077 
00078 
00079 
00081 
00086         template<uint8_t int_width, class size_type_class>
00087         _lcp_support_tree(int_vector_file_buffer<int_width, size_type_class>& lcp_buf, const Cst* cst = NULL) {
00088             construct(lcp_buf, cst);
00089         }
00090 
00094         template<class Text, class Sa>
00095         void construct(const Text& text, const Sa& sa, const Cst* cst);
00096 
00097 
00099         template<uint8_t int_width, class size_type_class>
00100         void construct(int_vector_file_buffer<int_width, size_type_class>& lcp_buf,
00101                        const Cst* cst=NULL) {
00102             m_cst = cst;
00103             std::string id =  util::to_string(util::get_pid())+"_"+util::to_string(util::get_id()).c_str() + "_fc_lcp";
00104             {
00105                 int_vector<int_width, size_type_class> temp_lcp;
00106                 algorithm::construct_first_child_lcp(lcp_buf, temp_lcp, (size_type_class) 0);
00107                 // TODO: store lcp values directly to disk
00108                 util::store_to_file(temp_lcp, id.c_str());
00109             }
00110             {
00111                 int_vector_file_buffer<int_width, size_type_class> temp_lcp_buf(id.c_str());
00112                 m_lcp = Lcp(temp_lcp_buf); // works for lcp_kurtz, lcp_wt and lcp_bitcompressed
00113             }
00114             std::remove(id.c_str());
00115         }
00116 
00117 
00118         size_type size()const {
00119             return m_cst->size(); // corresponds to the length of the
00120             // original lcp array
00121         }
00122 
00123         void set_cst(const Cst* cst) {
00124             m_cst = cst;
00125         }
00126 
00127         static size_type max_size() {
00128             return Lcp::max_size();
00129         }
00130 
00131         size_type empty()const {
00132             return m_lcp.empty();
00133         }
00134 
00135         void swap(_lcp_support_tree& lcp_c) {
00136             m_lcp.swap(lcp_c.m_lcp);
00137         }
00138 
00140 
00143         const_iterator begin()const {
00144             return const_iterator(this, 0);
00145         }
00146 
00148 
00151         const_iterator end()const {
00152             return const_iterator(this, size());
00153         }
00154 
00156 
00162         inline value_type operator[](size_type i)const {
00163             return m_lcp[ m_cst->tlcp_idx(i) ];
00164         }
00165 
00167 
00170         _lcp_support_tree& operator=(const _lcp_support_tree& lcp_c) {
00171             if (this != &lcp_c) {
00172                 copy(lcp_c);
00173             }
00174             return *this;
00175         }
00176 
00178 
00183         bool operator==(const _lcp_support_tree& lcp_c)const {
00184             if (this == &lcp_c)
00185                 return true;
00186             return m_cst == lcp_c.m_cst and m_lcp == lcp_c.m_lcp;
00187         }
00188 
00190 
00195         bool operator!=(const _lcp_support_tree& lcp_c)const {
00196             return !(*this == lcp_c);
00197         }
00198 
00200 
00203         size_type serialize(std::ostream& out, structure_tree_node* v=NULL, std::string name="")const {
00204             size_type written_bytes = 0;
00205             structure_tree_node* child = structure_tree::add_child(v, name, util::class_name(*this));
00206             written_bytes += m_lcp.serialize(out, child, "lcp");
00207             structure_tree::add_size(child, written_bytes);
00208             return written_bytes;
00209         }
00210 
00212 
00216         void load(std::istream& in, const Cst* cst=NULL) {
00217             m_lcp.load(in); // works for lcp_kurtz and lcp_bitcompressed
00218             m_cst = cst;
00219         }
00220 };
00221 
00223 template<class Lcp = lcp_wt<> >
00224 class lcp_support_tree
00225 {
00226     public:
00227         template<class Cst>  // template inner class which is used in CSTs to parametrize lcp classes
00228         class type           // with information about the CST. Thanks Stefan Arnold! (2011-03-02)
00229         {
00230             public:
00231                 typedef _lcp_support_tree<Lcp, Cst> lcp_type;
00232         };
00233 };
00234 
00235 
00236 
00237 } // end namespace
00238 
00239 #endif