00001
00002 #ifndef _ug_mm_tsa_h
00003 #define _ug_mm_tsa_h
00004
00005
00006
00007 #include <iostream>
00008 #include <stdexcept>
00009 #include <sstream>
00010
00011 #include <boost/iostreams/device/mapped_file.hpp>
00012 #include <boost/shared_ptr.hpp>
00013 #include <boost/dynamic_bitset.hpp>
00014
00015 #include "tpt_tightindex.h"
00016 #include "tpt_tokenindex.h"
00017 #include "tpt_pickler.h"
00018 #include "ug_tsa_base.h"
00019
00020 namespace sapt
00021 {
00022 namespace bio=boost::iostreams;
00023
00024 template<typename TOKEN>
00025 class mmTSA : public TSA<TOKEN>
00026 {
00027 public:
00028 typedef typename TSA<TOKEN>::tree_iterator tree_iterator;
00029 friend class TSA_tree_iterator<TOKEN>;
00030 private:
00031 bio::mapped_file_source file;
00032
00033 public:
00034
00035 filepos_type const* index;
00036
00037 private:
00038
00039 char const* index_jump(char const* a, char const* z, float ratio) const;
00040 char const* getLowerBound(id_type t) const;
00041 char const* getUpperBound(id_type t) const;
00042
00043 public:
00044 mmTSA();
00045 mmTSA(std::string fname, Ttrack<TOKEN> const* c);
00046 void open(std::string fname, typename boost::shared_ptr<Ttrack<TOKEN> const> c);
00047
00048 count_type
00049 sntCnt(char const* p, char const * const q) const;
00050
00051 count_type
00052 rawCnt(char const* p, char const * const q) const;
00053
00054 void
00055 getCounts(char const* p, char const * const q,
00056 count_type& sids, count_type& raw) const;
00057
00058 char const*
00059 readSid(char const* p, char const* q, id_type& sid) const;
00060
00061 char const*
00062 readSid(char const* p, char const* q, ::uint64_t& sid) const;
00063
00064 char const*
00065 readOffset(char const* p, char const* q, uint16_t& offset) const;
00066
00067 char const*
00068 readOffset(char const* p, char const* q, ::uint64_t& offset) const;
00069
00070 void sanityCheck() const;
00071
00072 };
00073
00074
00075
00079 template<typename TOKEN>
00080 char const*
00081 mmTSA<TOKEN>::
00082 index_jump(char const* a, char const* z, float ratio) const
00083 {
00084 assert(ratio >= 0 && ratio < 1);
00085 char const* m = a+int(ratio*(z-a));
00086 if (m > a)
00087 {
00088 while (m > a && *m < 0) --m;
00089 while (m > a && *m >= 0) --m;
00090 if (*m < 0) ++m;
00091 }
00092 assert(*m >= 0);
00093 return m;
00094 }
00095
00096
00097
00098 template<typename TOKEN>
00099 mmTSA<TOKEN>::
00100 mmTSA()
00101 {
00102 this->startArray = NULL;
00103 this->endArray = NULL;
00104 this->BitSetCachingThreshold=4096;
00105 };
00106
00107
00108
00109 template<typename TOKEN>
00110 mmTSA<TOKEN>::
00111 mmTSA(std::string fname, Ttrack<TOKEN> const* c)
00112 {
00113 open(fname,c);
00114 }
00115
00116
00117
00118 template<typename TOKEN>
00119 void
00120 mmTSA<TOKEN>::
00121 open(std::string fname, typename boost::shared_ptr<Ttrack<TOKEN> const> c)
00122 {
00123 this->bsc.reset(new BitSetCache<TSA<TOKEN> >(this));
00124 if (access(fname.c_str(),F_OK))
00125 {
00126 std::ostringstream msg;
00127 msg << "mmTSA<>::open: File '" << fname << "' does not exist.";
00128 throw std::runtime_error(msg.str().c_str());
00129 }
00130 assert(c);
00131 this->corpus = c;
00132 file.open(fname);
00133 Moses::prime(file);
00134 char const* p = file.data();
00135 filepos_type idxOffset;
00136 p = tpt::numread(p,idxOffset);
00137 p = tpt::numread(p,this->indexSize);
00138
00139
00140
00141 this->startArray = p;
00142 this->index = reinterpret_cast<filepos_type const*>(file.data()+idxOffset);
00143 this->endArray = reinterpret_cast<char const*>(index);
00144 this->corpusSize = c->size();
00145 this->numTokens = c->numTokens();
00146 }
00147
00148
00149
00150 template<typename TOKEN>
00151 char const*
00152 mmTSA<TOKEN>::
00153 getLowerBound(id_type id) const
00154 {
00155 if (id >= this->indexSize)
00156 return NULL;
00157 return this->startArray + this->index[id];
00158 }
00159
00160
00161
00162 template<typename TOKEN>
00163 char const*
00164 mmTSA<TOKEN>::
00165 getUpperBound(id_type id) const
00166 {
00167 if (id >= this->indexSize)
00168 return NULL;
00169
00170
00171 else
00172 return this->startArray + this->index[id+1];
00173 }
00174
00175
00176
00177 template<typename TOKEN>
00178 char const*
00179 mmTSA<TOKEN>::
00180 readSid(char const* p, char const* q, id_type& sid) const
00181 {
00182 return tpt::tightread(p,q,sid);
00183 }
00184
00185
00186
00187 template<typename TOKEN>
00188 char const*
00189 mmTSA<TOKEN>::
00190 readSid(char const* p, char const* q, ::uint64_t& sid) const
00191 {
00192 return tpt::tightread(p,q,sid);
00193 }
00194
00195
00196
00197 template<typename TOKEN>
00198 inline
00199 char const*
00200 mmTSA<TOKEN>::
00201 readOffset(char const* p, char const* q, uint16_t& offset) const
00202 {
00203 return tpt::tightread(p,q,offset);
00204 }
00205
00206
00207
00208 template<typename TOKEN>
00209 inline
00210 char const*
00211 mmTSA<TOKEN>::
00212 readOffset(char const* p, char const* q, ::uint64_t& offset) const
00213 {
00214 return tpt::tightread(p,q,offset);
00215 }
00216
00217
00218
00219 template<typename TOKEN>
00220 count_type
00221 mmTSA<TOKEN>::
00222 rawCnt(char const* p, char const* const q) const
00223 {
00224 id_type sid; uint16_t off;
00225 size_t ret=0;
00226 while (p < q)
00227 {
00228 p = tpt::tightread(p,q,sid);
00229 p = tpt::tightread(p,q,off);
00230 ret++;
00231 }
00232 return ret;
00233 }
00234
00235
00236
00237 template<typename TOKEN>
00238 void
00239 mmTSA<TOKEN>::
00240 getCounts(char const* p, char const* const q,
00241 count_type& sids, count_type& raw) const
00242 {
00243 raw = 0;
00244 id_type sid; uint16_t off;
00245 boost::dynamic_bitset<uint64_t> check(this->corpus->size());
00246 while (p < q)
00247 {
00248 p = tpt::tightread(p,q,sid);
00249 p = tpt::tightread(p,q,off);
00250 check.set(sid);
00251 raw++;
00252 }
00253 sids = check.count();
00254 }
00255
00256
00257
00258 }
00259
00260
00261 #endif