summaryrefslogtreecommitdiff
path: root/scanner/ip_matcher/IntervalIndex/CEI.cpp
blob: 8a2bba68a32c55975afe5ad4b1d4fda983a91b33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 *
 * 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    ([email protected])
 * 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_CEI

#include "CEI.h"

#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <cassert>
using namespace std;

unsigned int RoundToPowerOfTwo(unsigned int n)
{
	unsigned int N=1;
	while(n>N) N<<=1;
	return N;
}

// r must be no greater than 2^31
CEI::CEI(unsigned int r, unsigned int L)
{
	if(r==0 || (r&(r-1))!=0)
	{
		r=RoundToPowerOfTwo(r);
	}

	if(L==0 || (L&(L-1))!=0)
	{
		L=RoundToPowerOfTwo(L);
	}

	m_r=r;
	m_L=L;
	m_pIDList=new unsigned int[(r<<1)+1];
	m_pIDArray=NULL;
}

CEI::~CEI()
{
	if(m_pIDList!=NULL) delete [] m_pIDList;
	if(m_pIDArray!=NULL) delete [] m_pIDArray;
}

long long CEI::PreProcessing(const std::vector<unsigned int>& a, const std::vector<unsigned int>& b)
{
	vector<unsigned int> A=a, B=b;
	std::vector< std::vector<unsigned int> > IDList(m_r<<1);

	for(int i=0; i<(int)A.size(); i++)
	{
		B[i]++;
		if(A[i]>=B[i]) continue;

		unsigned int inf=(A[i]+m_L-1)&-m_L;
		unsigned int sup=B[i]&-m_L;

		if(inf>sup)
		{
			this->BestPartition(i, A[i], B[i], IDList);
		}
		else
		{
			for(unsigned int p=inf; p<sup; p+=m_L)
			{
				IDList[(p<<1)+1].push_back(i);
			}

			if(A[i]<inf) this->BestPartition(i, A[i], inf, IDList);
			if(sup<B[i]) this->BestPartition(i, sup, B[i], IDList);
		}
	}

	m_pIDList[0]=0;
	for(int i=0; i<(int)IDList.size(); i++)
	{
		m_pIDList[i+1]=m_pIDList[i]+IDList[i].size();
	}

	m_pIDArray=new unsigned int[m_pIDList[2*m_r]];
	for(int i=0; i<(int)IDList.size(); i++)
	{
		copy(IDList[i].begin(), IDList[i].end(), m_pIDArray+m_pIDList[i]);
	}

	long long iMemBytes=sizeof(unsigned int)*(2*m_r+1+m_pIDList[2*m_r]);

#ifdef DEBUG_CEI
	printf("CEI membyte=%5.3lf (MB).\n", (double)iMemBytes/(1u<<20));
#endif

	return iMemBytes;
}

void CEI::BestPartition(unsigned int id, unsigned int a, unsigned int b, std::vector< std::vector<unsigned int> >& IDList)
{
	unsigned int g=(a&-m_L)<<1;

	while(a<b)
	{
		unsigned int t=(a&(m_L-1))+m_L;
		unsigned int delta=1;
		while((t&1)==0 && a+(delta<<1)<=b)
		{
			t>>=1;
			delta<<=1;
		}

		IDList[g+t].push_back(id);
		a+=delta;
	}
}

static int compare(const void * a, const void * b)
{
	return(*(unsigned int *)a - *(unsigned int *)b);
}

int CEI::Find(unsigned int key, unsigned int * result, unsigned int size)
{
	unsigned int g=(key&-m_L)<<1;
	unsigned int n = 0;
	for(unsigned int t=(key&(m_L-1))+m_L; t; t>>=1)
	{
		unsigned int c=g+t;
		if(m_pIDList[c]<m_pIDList[c+1])
		{
			int s = m_pIDList[c+1] - m_pIDList[c];
			unsigned int * id_array = m_pIDArray + m_pIDList[c]; 
			for(int i = 0; i < s; i++)
			{
				if(n >= size)
				{
					qsort(result, n, sizeof(unsigned int), compare);
					return n;
				}
				result[n++] = id_array[i];
			}
		}
	}

	qsort(result, n, sizeof(unsigned int), compare);

	return n;
}