SDSL: Succinct Data Structure Library
A C++ template library for succinct data structures
|
00001 /* sdsl - succinct data structures library 00002 Copyright (C) 2008 Simon Gog 00003 00004 This program is free software: you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation, either version 3 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program. If not, see http://www.gnu.org/licenses/ . 00016 */ 00021 #ifndef INCLUDED_SDSL_SELECT_SUPPORT_BS 00022 #define INCLUDED_SDSL_SELECT_SUPPORT_BS 00023 00024 #include "int_vector.hpp" 00025 #include "rank_support.hpp" 00026 #include "select_support.hpp" 00027 00029 namespace sdsl 00030 { 00031 00033 00036 template<class RankSupport=rank_support_v<> > 00037 class select_support_bs : public select_support 00038 { 00039 public: 00040 typedef typename RankSupport::bit_vector_type bit_vector_type; 00041 private: 00042 const RankSupport* m_rs; 00043 void copy(const select_support_bs& ss); 00044 public: 00045 explicit select_support_bs(const int_vector<1>* v=NULL, const RankSupport* m_rs=NULL); 00046 select_support_bs(const select_support_bs& ss); 00047 ~select_support_bs() {} 00048 void init(const int_vector<1>* v=NULL); 00049 00050 inline const size_type select(size_type) const; 00052 inline const size_type operator()(size_type)const; 00053 00054 size_type serialize(std::ostream& out, structure_tree_node* v=NULL, std::string name="")const; 00055 void load(std::istream& in, const int_vector<1>* v=NULL); 00056 void set_vector(const int_vector<1>* v=NULL); 00057 select_support_bs& operator=(const select_support_bs& ss); 00058 void swap(select_support_bs& ss); 00060 00064 bool operator==(const select_support_bs& ss)const; 00066 00070 bool operator!=(const select_support_bs& ss)const; 00071 }; 00072 00073 template<class RankSupport> 00074 select_support_bs<RankSupport>::select_support_bs(const int_vector<1>* v, const RankSupport* rs):select_support(v) 00075 { 00076 m_rs = rs; 00077 } 00078 00079 template<class RankSupport> 00080 select_support_bs<RankSupport>::select_support_bs(const select_support_bs& ss):select_support(ss.m_v) 00081 { 00082 copy(ss); 00083 } 00084 00085 template<class RankSupport> 00086 select_support_bs<RankSupport>& select_support_bs<RankSupport>::operator=(const select_support_bs& ss) 00087 { 00088 copy(ss); 00089 return *this; 00090 } 00091 00092 template<class RankSupport> 00093 void select_support_bs<RankSupport>::swap(select_support_bs& ss) 00094 { 00095 std::swap(m_rs, ss.m_rs); 00096 } 00097 00098 template<class RankSupport> 00099 void select_support_bs<RankSupport>::copy(const select_support_bs& ss) 00100 { 00101 m_v = ss.m_v; 00102 m_rs = ss.m_rs; 00103 } 00104 00105 template<class RankSupport> 00106 void select_support_bs<RankSupport>::init(const int_vector<1>*) 00107 { 00108 } 00109 00110 template<class RankSupport> 00111 inline const typename select_support_bs<RankSupport>::size_type select_support_bs<RankSupport>::select(size_type i)const 00112 { 00113 size_type min = i, max = m_v->bit_size()+1; // min included, max excluded 00114 assert(i <= m_rs->rank(m_v->bit_size()-1) && i>0); 00115 size_type idx = m_v->bit_size()/2, r; 00116 // binary search t 00117 do { 00118 idx = (min+max)>>1; 00119 r = m_rs->rank(idx); // ones in the prefix [0..idx-1] 00120 if (r>i) 00121 max = idx; 00122 else if (r<i) 00123 min = idx+1; 00124 else { // rank(idx)==i 00125 if ((*m_v)[idx-1]) 00126 return idx-1; 00127 else 00128 max = idx; 00129 } 00130 } while (true); 00131 } 00132 00133 00134 00135 template<class RankSupport> 00136 typename select_support_bs<RankSupport>::size_type select_support_bs<RankSupport>::serialize(std::ostream& out, structure_tree_node* v, std::string name)const 00137 { 00138 structure_tree_node* child = structure_tree::add_child(v, name, util::class_name(*this)); 00139 structure_tree::add_size(child, 0); 00140 return 0; 00141 } 00142 00143 template<class RankSupport> 00144 void select_support_bs<RankSupport>::load(std::istream&, const int_vector<1>* v) 00145 { 00146 set_vector(v); 00147 } 00148 00149 template<class RankSupport> 00150 void select_support_bs<RankSupport>::set_vector(const int_vector<1>* v) 00151 { 00152 m_v = v; 00153 } 00154 00155 template<class RankSupport> 00156 bool select_support_bs<RankSupport>::operator==(const select_support_bs& ss)const 00157 { 00158 if (this == &ss) 00159 return true; 00160 return *m_rs == *(ss.m_rs); 00161 } 00162 00163 template<class RankSupport> 00164 bool select_support_bs<RankSupport>::operator!=(const select_support_bs& ss)const 00165 { 00166 return !(*this == ss); 00167 } 00168 00169 template<class RankSupport> 00170 inline const typename select_support_bs<RankSupport>::size_type select_support_bs<RankSupport>::operator()(size_type i)const 00171 { 00172 return select(i); 00173 } 00174 00175 00176 } 00177 00178 #endif