2010-02-12 14:05:43 +03:00
|
|
|
#include "DynSuffixArray.h"
|
|
|
|
#include <iostream>
|
2013-06-03 15:22:05 +04:00
|
|
|
#include <boost/foreach.hpp>
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2010-04-12 16:29:37 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
namespace Moses
|
|
|
|
{
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
DynSuffixArray::DynSuffixArray()
|
|
|
|
{
|
2010-04-08 18:52:35 +04:00
|
|
|
m_SA = new vuint_t();
|
|
|
|
m_ISA = new vuint_t();
|
|
|
|
m_F = new vuint_t();
|
|
|
|
m_L = new vuint_t();
|
2010-02-12 14:05:43 +03:00
|
|
|
std::cerr << "DYNAMIC SUFFIX ARRAY CLASS INSTANTIATED" << std::endl;
|
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
DynSuffixArray::~DynSuffixArray()
|
|
|
|
{
|
2010-04-08 18:52:35 +04:00
|
|
|
delete m_SA;
|
|
|
|
delete m_ISA;
|
|
|
|
delete m_F;
|
|
|
|
delete m_L;
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
DynSuffixArray::DynSuffixArray(vuint_t* crp)
|
|
|
|
{
|
2010-02-12 14:05:43 +03:00
|
|
|
// make native int array and pass to SA builder
|
2011-02-24 16:14:42 +03:00
|
|
|
m_corpus = crp;
|
2010-04-08 18:52:35 +04:00
|
|
|
int size = m_corpus->size();
|
2010-02-12 14:05:43 +03:00
|
|
|
int* tmpArr = new int[size];
|
2011-02-24 16:14:42 +03:00
|
|
|
for(int i=0 ; i < size; ++i) tmpArr[i] = i;
|
|
|
|
|
2010-04-20 18:09:53 +04:00
|
|
|
Qsort(tmpArr, 0, size-1);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-08 18:52:35 +04:00
|
|
|
m_SA = new vuint_t(tmpArr, tmpArr + size);
|
2010-02-12 14:05:43 +03:00
|
|
|
//std::cerr << "printing SA " << std::endl;
|
2010-04-08 18:52:35 +04:00
|
|
|
//for(int i=0; i < size; ++i) std::cerr << m_SA->at(i) << std::endl;
|
2010-02-12 14:05:43 +03:00
|
|
|
delete[] tmpArr;
|
|
|
|
std::cerr << "DYNAMIC SUFFIX ARRAY CLASS INSTANTIATED WITH SIZE " << size << std::endl;
|
2010-04-20 18:09:53 +04:00
|
|
|
BuildAuxArrays();
|
2010-02-12 14:05:43 +03:00
|
|
|
//printAuxArrays();
|
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
void DynSuffixArray::BuildAuxArrays()
|
|
|
|
{
|
2010-04-08 18:52:35 +04:00
|
|
|
int size = m_SA->size();
|
|
|
|
m_ISA = new vuint_t(size);
|
|
|
|
m_F = new vuint_t(size);
|
|
|
|
m_L = new vuint_t(size);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-02-12 14:05:43 +03:00
|
|
|
for(int i=0; i < size; ++i) {
|
2010-04-08 18:52:35 +04:00
|
|
|
m_ISA->at(m_SA->at(i)) = i;
|
|
|
|
//(*m_ISA)[(*m_SA)[i]] = i;
|
|
|
|
(*m_F)[i] = (*m_corpus)[m_SA->at(i)];
|
2011-02-24 16:14:42 +03:00
|
|
|
(*m_L)[i] = (*m_corpus)[(m_SA->at(i) == 0 ? size-1 : m_SA->at(i)-1)];
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
int DynSuffixArray::Rank(unsigned word, unsigned idx)
|
|
|
|
{
|
|
|
|
/* use Gerlach's code to make rank faster */
|
2011-09-01 13:57:16 +04:00
|
|
|
// the number of words in L[0..i] (minus 1 which is why 'i < idx', not '<=')
|
2010-02-12 14:05:43 +03:00
|
|
|
int r(0);
|
|
|
|
for(unsigned i=0; i < idx; ++i)
|
2010-04-08 18:52:35 +04:00
|
|
|
if(m_L->at(i) == word) ++r;
|
2010-02-12 14:05:43 +03:00
|
|
|
return r;
|
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2010-02-12 14:05:43 +03:00
|
|
|
/* count function should be implemented
|
|
|
|
* with binary search over suffix array!! */
|
2011-02-24 16:14:42 +03:00
|
|
|
int DynSuffixArray::F_firstIdx(unsigned word)
|
|
|
|
{
|
2010-04-08 18:52:35 +04:00
|
|
|
// return index of first row where word is found in m_F
|
2011-07-06 21:25:54 +04:00
|
|
|
/*for(int i=0; i < m_F->size(); ++i) {
|
|
|
|
if(m_F->at(i) == word) {
|
2013-05-29 21:16:15 +04:00
|
|
|
return i;
|
2011-07-06 21:25:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;*/
|
2013-05-29 21:16:15 +04:00
|
|
|
//NOTE: lower_bound is faster than linear search above but may cause issues
|
|
|
|
// if ordering of vocab is not consecutive (ie..after deletions)
|
2010-04-08 18:52:35 +04:00
|
|
|
int low = std::lower_bound(m_F->begin(), m_F->end(), word) - m_F->begin();
|
2010-05-25 17:11:42 +04:00
|
|
|
//cerr << "in F_firstIdx with word = " << word << " and low = " << low << " and F->size() =" << m_F->size() << endl;
|
2012-05-10 16:48:51 +04:00
|
|
|
if((size_t)low >= m_F->size())
|
2011-02-24 16:14:42 +03:00
|
|
|
return -1;
|
|
|
|
else
|
2010-05-25 17:06:56 +04:00
|
|
|
return low;
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2010-05-07 13:50:19 +04:00
|
|
|
/* uses rank() and c() to obtain the LastFirstFunc function */
|
2011-02-24 16:14:42 +03:00
|
|
|
int DynSuffixArray::LastFirstFunc(unsigned L_idx)
|
|
|
|
{
|
2010-02-12 14:05:43 +03:00
|
|
|
int fIdx(-1);
|
2010-05-25 17:06:56 +04:00
|
|
|
//cerr << "in LastFirstFcn() with L_idx = " << L_idx << endl;
|
2010-04-08 18:52:35 +04:00
|
|
|
unsigned word = m_L->at(L_idx);
|
2011-02-24 16:14:42 +03:00
|
|
|
if((fIdx = F_firstIdx(word)) != -1) {
|
2010-05-25 17:06:56 +04:00
|
|
|
//cerr << "fidx + Rank(" << word << "," << L_idx << ") = " << fIdx << "+" << Rank(word, L_idx) << endl;
|
2010-05-27 22:53:39 +04:00
|
|
|
fIdx += Rank(word, L_idx);
|
2010-05-25 17:06:56 +04:00
|
|
|
}
|
2010-05-27 22:53:39 +04:00
|
|
|
return fIdx;
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
void DynSuffixArray::Insert(vuint_t* newSent, unsigned newIndex)
|
|
|
|
{
|
2010-02-12 14:05:43 +03:00
|
|
|
// for sentences
|
|
|
|
//stages 1, 2, 4 stay same from 1char case
|
|
|
|
//(use last word of new text in step 2 and save Ltmp until last insert?)
|
|
|
|
//stage 3...all words of new sentence are inserted backwards
|
|
|
|
// stage 2: k=ISA[newIndex], tmp= L[k], L[k] = newChar
|
2010-05-25 17:06:56 +04:00
|
|
|
//PrintAuxArrays();
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(newIndex > m_SA->size(), "Error");
|
2010-02-12 14:05:43 +03:00
|
|
|
int k(-1), kprime(-1);
|
2010-04-08 18:52:35 +04:00
|
|
|
k = (newIndex < m_SA->size() ? m_ISA->at(newIndex) : m_ISA->at(0)); // k is now index of the cycle that starts at newindex
|
2010-05-07 13:50:19 +04:00
|
|
|
int true_pos = LastFirstFunc(k); // track cycle shift (newIndex - 1)
|
2010-04-08 18:52:35 +04:00
|
|
|
int Ltmp = m_L->at(k);
|
2010-05-25 17:06:56 +04:00
|
|
|
m_L->at(k) = newSent->at(newSent->size()-1); // cycle k now ends with correct word
|
2010-02-12 14:05:43 +03:00
|
|
|
for(int j = newSent->size()-1; j > -1; --j) {
|
2010-05-07 13:50:19 +04:00
|
|
|
kprime = LastFirstFunc(k); // find cycle that starts with (newindex - 1)
|
2010-04-08 18:52:35 +04:00
|
|
|
//kprime += ((m_L[k] == Ltmp) && (k > isa[k]) ? 1 : 0); // yada yada
|
2010-02-12 14:05:43 +03:00
|
|
|
// only terminal char can be 0 so add new vocab at end
|
2011-02-24 16:14:42 +03:00
|
|
|
kprime = (kprime > 0 ? kprime : m_SA->size());
|
2010-02-12 14:05:43 +03:00
|
|
|
true_pos += (kprime <= true_pos ? 1 : 0); // track changes
|
|
|
|
// insert everything
|
2010-05-25 17:06:56 +04:00
|
|
|
m_F->insert(m_F->begin() + kprime, newSent->at(j));
|
|
|
|
int theLWord = (j == 0 ? Ltmp : newSent->at(j-1));
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-08 18:52:35 +04:00
|
|
|
m_L->insert(m_L->begin() + kprime, theLWord);
|
2010-11-22 13:05:17 +03:00
|
|
|
for (vuint_t::iterator itr = m_SA->begin(); itr != m_SA->end(); ++itr) {
|
2010-02-12 14:05:43 +03:00
|
|
|
if(*itr >= newIndex) ++(*itr);
|
2010-11-22 13:05:17 +03:00
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
m_SA->insert(m_SA->begin() + kprime, newIndex);
|
2010-11-22 13:05:17 +03:00
|
|
|
for (vuint_t::iterator itr = m_ISA->begin(); itr != m_ISA->end(); ++itr) {
|
2010-04-08 18:52:35 +04:00
|
|
|
if((int)*itr >= kprime) ++(*itr);
|
2010-11-22 13:05:17 +03:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-08 18:52:35 +04:00
|
|
|
m_ISA->insert(m_ISA->begin() + newIndex, kprime);
|
2010-02-12 14:05:43 +03:00
|
|
|
k = kprime;
|
2010-05-25 17:06:56 +04:00
|
|
|
//PrintAuxArrays();
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
|
|
|
// Begin stage 4
|
2010-05-07 13:50:19 +04:00
|
|
|
Reorder(true_pos, LastFirstFunc(kprime)); // actual position vs computed position of cycle (newIndex-1)
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
void DynSuffixArray::Reorder(unsigned j, unsigned jprime)
|
|
|
|
{
|
2011-09-01 13:57:16 +04:00
|
|
|
set<pair<unsigned, unsigned> > seen;
|
2010-02-12 14:05:43 +03:00
|
|
|
while(j != jprime) {
|
2013-05-29 21:16:15 +04:00
|
|
|
// this 'seenit' check added for data with many loops. will remove after double
|
|
|
|
// checking.
|
2011-09-01 13:57:16 +04:00
|
|
|
bool seenit = seen.insert(std::make_pair(j, jprime)).second;
|
|
|
|
if(seenit) {
|
2012-05-10 16:48:51 +04:00
|
|
|
for(size_t i=1; i < m_SA->size(); ++i) {
|
2011-09-01 13:57:16 +04:00
|
|
|
if(m_corpus->at(m_SA->at(i)) < m_corpus->at(m_SA->at(i-1))) {
|
|
|
|
cerr << "PROBLEM WITH SUFFIX ARRAY REORDERING. EXITING...\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
//cerr << "j=" << j << "\tj'=" << jprime << endl;
|
2011-09-01 13:57:16 +04:00
|
|
|
int isaIdx(-1);
|
2010-05-07 13:50:19 +04:00
|
|
|
int new_j = LastFirstFunc(j);
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(j > jprime, "Error");
|
2011-09-01 13:57:16 +04:00
|
|
|
// for SA and L, the element at pos j is moved to pos j'
|
2013-05-29 21:16:15 +04:00
|
|
|
m_L->insert(m_L->begin() + jprime + 1, m_L->at(j));
|
2011-09-01 13:57:16 +04:00
|
|
|
m_L->erase(m_L->begin() + j);
|
2013-05-29 21:16:15 +04:00
|
|
|
m_SA->insert(m_SA->begin() + jprime + 1, m_SA->at(j));
|
2011-09-01 13:57:16 +04:00
|
|
|
m_SA->erase(m_SA->begin() + j);
|
2010-02-12 14:05:43 +03:00
|
|
|
// all ISA values between (j...j'] decremented
|
2010-04-08 18:52:35 +04:00
|
|
|
for(size_t i = 0; i < m_ISA->size(); ++i) {
|
2011-02-24 16:14:42 +03:00
|
|
|
if((m_ISA->at(i) == j) && (isaIdx == -1))
|
2010-02-12 14:05:43 +03:00
|
|
|
isaIdx = i; // store index of ISA[i] = j
|
2010-04-08 18:52:35 +04:00
|
|
|
if((m_ISA->at(i) > j) && (m_ISA->at(i) <= jprime)) --(*m_ISA)[i];
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
|
|
|
// replace j with j' in ISA
|
|
|
|
//isa[isaIdx] = jprime;
|
2010-04-08 18:52:35 +04:00
|
|
|
m_ISA->at(isaIdx) = jprime;
|
2010-02-12 14:05:43 +03:00
|
|
|
j = new_j;
|
2010-05-07 13:50:19 +04:00
|
|
|
jprime = LastFirstFunc(jprime);
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2011-07-06 21:25:54 +04:00
|
|
|
//cerr << "j=" << j << "\tj'=" << jprime << endl;
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
void DynSuffixArray::Delete(unsigned index, unsigned num2del)
|
|
|
|
{
|
2010-04-08 18:52:35 +04:00
|
|
|
int ltmp = m_L->at(m_ISA->at(index));
|
2010-05-07 13:50:19 +04:00
|
|
|
int true_pos = LastFirstFunc(m_ISA->at(index)); // track cycle shift (newIndex - 1)
|
2010-04-08 18:52:35 +04:00
|
|
|
for(size_t q = 0; q < num2del; ++q) {
|
|
|
|
int row = m_ISA->at(index); // gives the position of index in SA and m_F
|
2011-09-01 13:57:16 +04:00
|
|
|
//std::cerr << "row = " << row << std::endl;
|
|
|
|
//std::cerr << "SA[r]/index = " << m_SA->at(row) << "/" << index << std::endl;
|
2010-02-12 14:05:43 +03:00
|
|
|
true_pos -= (row <= true_pos ? 1 : 0); // track changes
|
2011-02-24 16:14:42 +03:00
|
|
|
m_L->erase(m_L->begin() + row);
|
|
|
|
m_F->erase(m_F->begin() + row);
|
|
|
|
|
|
|
|
m_ISA->erase(m_ISA->begin() + index); // order is important
|
2010-11-22 13:05:17 +03:00
|
|
|
for (vuint_t::iterator itr = m_ISA->begin(); itr != m_ISA->end(); ++itr) {
|
2010-04-08 18:52:35 +04:00
|
|
|
if((int)*itr > row) --(*itr);
|
2010-11-22 13:05:17 +03:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-08 18:52:35 +04:00
|
|
|
m_SA->erase(m_SA->begin() + row);
|
2010-11-22 13:05:17 +03:00
|
|
|
for (vuint_t::iterator itr = m_SA->begin(); itr != m_SA->end(); ++itr) {
|
2010-02-12 14:05:43 +03:00
|
|
|
if(*itr > index) --(*itr);
|
2010-11-22 13:05:17 +03:00
|
|
|
}
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
m_L->at(m_ISA->at(index))= ltmp;
|
2010-05-07 13:50:19 +04:00
|
|
|
Reorder(LastFirstFunc(m_ISA->at(index)), true_pos);
|
2011-09-01 13:57:16 +04:00
|
|
|
//PrintAuxArrays();
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 19:17:38 +03:00
|
|
|
void DynSuffixArray::Substitute(vuint_t* /* newSents */, unsigned /* newIndex */)
|
2011-02-24 16:14:42 +03:00
|
|
|
{
|
2010-04-08 18:52:35 +04:00
|
|
|
std::cerr << "NEEDS TO IMPLEMENT SUBSITITUTE FACTOR\n";
|
2010-02-12 14:05:43 +03:00
|
|
|
return;
|
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2013-06-26 20:19:09 +04:00
|
|
|
ComparePosition::
|
|
|
|
ComparePosition(vuint_t const& crp, vuint_t const& sfa)
|
|
|
|
: m_crp(crp), m_sfa(sfa) { }
|
2013-06-03 15:22:05 +04:00
|
|
|
|
2013-06-26 20:19:09 +04:00
|
|
|
bool
|
|
|
|
ComparePosition::
|
|
|
|
operator()(unsigned const& i, vector<wordID_t> const& phrase) const
|
|
|
|
{
|
|
|
|
unsigned const* x = &m_crp.at(i);
|
|
|
|
unsigned const* e = &m_crp.back();
|
|
|
|
size_t k = 0;
|
|
|
|
for (; k < phrase.size() && x < e; ++k, ++x)
|
|
|
|
if (*x != phrase[k]) return *x < phrase[k];
|
|
|
|
return (x == e && k < phrase.size());
|
|
|
|
}
|
2013-06-03 15:22:05 +04:00
|
|
|
|
2013-06-26 20:19:09 +04:00
|
|
|
bool
|
|
|
|
ComparePosition::
|
|
|
|
operator()(vector<wordID_t> const& phrase, unsigned const& i) const
|
|
|
|
{
|
|
|
|
unsigned const* x = &m_crp.at(i);
|
|
|
|
unsigned const* e = &m_crp.back();
|
|
|
|
size_t k = 0;
|
|
|
|
for (; k < phrase.size() && x < e; ++k, ++x)
|
|
|
|
if (*x != phrase[k]) return phrase[k] < *x;
|
|
|
|
return false; // (k == phrase.size() && x < e);
|
|
|
|
}
|
2013-06-03 15:22:05 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
bool DynSuffixArray::GetCorpusIndex(const vuint_t* phrase, vuint_t* indices)
|
|
|
|
{
|
2013-06-26 20:19:09 +04:00
|
|
|
// DOES THIS EVEN WORK WHEN A DynSuffixArray has been saved and reloaded????
|
2010-02-12 14:05:43 +03:00
|
|
|
pair<vuint_t::iterator,vuint_t::iterator> bounds;
|
|
|
|
indices->clear();
|
2010-04-08 18:52:35 +04:00
|
|
|
size_t phrasesize = phrase->size();
|
2010-02-12 14:05:43 +03:00
|
|
|
// find lower and upper bounds on phrase[0]
|
2010-04-08 18:52:35 +04:00
|
|
|
bounds = std::equal_range(m_F->begin(), m_F->end(), phrase->at(0));
|
|
|
|
// bounds holds first and (last + 1) index of phrase[0] in m_SA
|
|
|
|
size_t lwrBnd = size_t(bounds.first - m_F->begin());
|
|
|
|
size_t uprBnd = size_t(bounds.second - m_F->begin());
|
2011-06-09 21:27:48 +04:00
|
|
|
//cerr << "phrasesize = " << phrasesize << "\tuprBnd = " << uprBnd << "\tlwrBnd = " << lwrBnd;
|
|
|
|
//cerr << "\tcorpus size = " << m_corpus->size() << endl;
|
2010-02-12 14:05:43 +03:00
|
|
|
if(uprBnd - lwrBnd == 0) return false; // not found
|
|
|
|
if(phrasesize == 1) {
|
2010-04-08 18:52:35 +04:00
|
|
|
for(size_t i=lwrBnd; i < uprBnd; ++i) {
|
|
|
|
indices->push_back(m_SA->at(i));
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
|
|
|
return (indices->size() > 0);
|
|
|
|
}
|
|
|
|
//find longer phrases if they exist
|
2010-04-08 18:52:35 +04:00
|
|
|
for(size_t i = lwrBnd; i < uprBnd; ++i) {
|
|
|
|
size_t crpIdx = m_SA->at(i);
|
2011-05-31 16:04:40 +04:00
|
|
|
if((crpIdx + phrasesize) > m_corpus->size()) continue; // past end of corpus
|
2010-04-08 18:52:35 +04:00
|
|
|
for(size_t pos = 1; pos < phrasesize; ++pos) { // for all following words
|
|
|
|
if(m_corpus->at(crpIdx + pos) != phrase->at(pos)) { // if word doesn't match
|
2010-02-12 14:05:43 +03:00
|
|
|
if(indices->size() > 0) i = uprBnd; // past the phrases since SA is ordered
|
2011-02-24 16:14:42 +03:00
|
|
|
break;
|
|
|
|
} else if(pos == phrasesize-1) { // found phrase
|
|
|
|
indices->push_back(crpIdx + pos); // store rigthmost index of phrase
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
|
|
|
//cerr << "Total count of phrase = " << indices->size() << endl;
|
|
|
|
return (indices->size() > 0);
|
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2013-06-26 20:19:09 +04:00
|
|
|
size_t
|
|
|
|
DynSuffixArray::
|
|
|
|
GetCount(vuint_t const& phrase) const
|
|
|
|
{
|
|
|
|
ComparePosition cmp(*m_corpus, *m_SA);
|
|
|
|
vuint_t::const_iterator lb = lower_bound(m_SA->begin(), m_SA->end(), phrase, cmp);
|
|
|
|
vuint_t::const_iterator ub = upper_bound(m_SA->begin(), m_SA->end(), phrase, cmp);
|
|
|
|
return ub-lb;
|
|
|
|
}
|
2013-06-03 15:22:05 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
void DynSuffixArray::Save(FILE* fout)
|
|
|
|
{
|
2010-04-08 18:52:35 +04:00
|
|
|
fWriteVector(fout, *m_SA);
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
void DynSuffixArray::Load(FILE* fin)
|
|
|
|
{
|
2010-04-08 18:52:35 +04:00
|
|
|
fReadVector(fin, *m_SA);
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
int DynSuffixArray::Compare(int pos1, int pos2, int max)
|
|
|
|
{
|
2010-04-08 18:52:35 +04:00
|
|
|
for (size_t i = 0; i < (unsigned)max; ++i) {
|
|
|
|
if((pos1 + i < m_corpus->size()) && (pos2 + i >= m_corpus->size()))
|
2010-02-12 14:05:43 +03:00
|
|
|
return 1;
|
2010-04-08 18:52:35 +04:00
|
|
|
if((pos2 + i < m_corpus->size()) && (pos1 + i >= m_corpus->size()))
|
2010-02-12 14:05:43 +03:00
|
|
|
return -1;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-08 18:52:35 +04:00
|
|
|
int diff = m_corpus->at(pos1+i) - m_corpus->at(pos2+i);
|
2010-02-12 14:05:43 +03:00
|
|
|
if(diff != 0) return diff;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-04-08 18:52:35 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
void DynSuffixArray::Qsort(int* array, int begin, int end)
|
|
|
|
{
|
|
|
|
if(end > begin) {
|
|
|
|
int index;
|
|
|
|
{
|
2010-02-12 14:05:43 +03:00
|
|
|
index = begin + (rand() % (end - begin + 1));
|
|
|
|
int pivot = array[index];
|
|
|
|
{
|
|
|
|
int tmp = array[index];
|
|
|
|
array[index] = array[end];
|
|
|
|
array[end] = tmp;
|
|
|
|
}
|
|
|
|
for(int i=index=begin; i < end; ++i) {
|
2010-04-20 18:09:53 +04:00
|
|
|
if (Compare(array[i], pivot, 20) <= 0) {
|
2010-02-12 14:05:43 +03:00
|
|
|
{
|
|
|
|
int tmp = array[index];
|
|
|
|
array[index] = array[i];
|
|
|
|
array[i] = tmp;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
int tmp = array[index];
|
|
|
|
array[index] = array[end];
|
|
|
|
array[end] = tmp;
|
|
|
|
}
|
|
|
|
}
|
2010-04-20 18:09:53 +04:00
|
|
|
Qsort(array, begin, index - 1);
|
|
|
|
Qsort(array, index + 1, end);
|
2010-02-12 14:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace
|