/* * * Copyright (c) 2008--2012 * String Matching Group, Lab for Intelligent Information Processing Technology, * Institute of Information Engineering, Chinese Academy of Sciences (IIE-CAS). * All rights reserved. * * Written by: LIU YANBING (liuyanbing@iie.ac.cn) * Last modification: 2012-07-10 * * This code is the exclusive and proprietary property of IIE-CAS. Usage for direct * or indirect commercial advantage is not allowed without written permission from * the authors. * */ //#define DEBUG_NAIVE_INTERVAL_INDEX2 #include "NaiveIntervalIndex2.h" #include #include #include #include #include using namespace std; CNaiveIntervalIndex2::CNaiveIntervalIndex2() { this->m_N=1; this->m_pEndPoints=NULL; this->m_pIDList=NULL; } CNaiveIntervalIndex2::~CNaiveIntervalIndex2() { if(this->m_pEndPoints!=NULL) { delete [] this->m_pEndPoints; } if(this->m_pIDList!=NULL) { delete [] this->m_pIDList; } } long long CNaiveIntervalIndex2::PreProcessing(const vector& a, const vector& b) { vector A=a, B=b; long long iMemBytes=0; set s; for(int i=0, n=(int)A.size(); im_IndexForMaxInt.push_back(i); --B[i]; } B[i]++; // now A[i], B[i] is half closed interval. if(A[i]>=B[i]) continue; s.insert(A[i]); s.insert(B[i]); } iMemBytes+=(long long)(sizeof(unsigned int)*this->m_IndexForMaxInt.size()); int M=(int)s.size(); this->m_N=1; while(m_N<=M) m_N<<=1; this->m_pEndPoints=new unsigned int[m_N]; this->m_pIDList=new vector[m_N]; vector v; copy(s.begin(), s.end(), back_inserter(v)); for(int i=M; i>1; d>0; d>>=1) { for(int j=1; d*jm_pEndPoints[k++]=v[d*j-1]; } } assert(k==m_N); iMemBytes+=m_N*(int)(sizeof(unsigned int)+sizeof(vector)); for(int i=0, n=(int)A.size(); i=B[i]) continue; int p =1; while(pm_pIDList[j-m_N].push_back(i); iMemBytes+=(q-p)*(int)sizeof(unsigned int); } #ifdef DEBUG_NAIVE_INTERVAL_INDEX2 printf("Naive Interval Index-2 membyte=%5.3lf (MB).\n", (double)iMemBytes/(1u<<20)); #endif return iMemBytes; } int CNaiveIntervalIndex2::Find(unsigned int key, unsigned int * result, unsigned int size) { unsigned int n = 0; int s = 0; if(key==UINT_MAX) { s = m_IndexForMaxInt.size(); for(int i = 0; i < s; i++) { if(n >= size) { return n; } result[n++] = m_IndexForMaxInt[i]; } } else { int i=1; while(i= size) { return n; } result[n++] = m_pIDList[i - m_N][j]; } } return n; }