LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
lshsignature.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6//
7// C++ Implementation: lshsignature
8//
9// Description:
10//
11//
12// Author: Claire Mouton, Guillaume Pitel <claire.mouton@cea.fr, guillaume.pitel@gmail.com>, (C) 2008
13//
14// Copyright: See COPYING file that comes with this distribution
15//
16//
17
18#include "lshsignature.h"
19
20LSHSignature::LSHSignature( int signatureSize)
21{
22 precomputedCount = -1;
23 bitLength = signatureSize;
24 if (bitLength > 0)
25 data = new base[(bitLength / 8)/sizeof(base)];
26 else data = NULL;
27}
28
29
31{
32 if (bitLength > 0 && data)
33 delete[] data;
34}
35
37 precomputedCount(s.precomputedCount),
38 data(nullptr),
39 bitLength(s.bitLength)
40{
41 if (bitLength > 0) {
42 data = new base[(bitLength / 8)/sizeof(base)] ;
43 std::copy(s.data, s.data + (bitLength / (sizeof(base)*8)), data);
44 }
45}
46
48{
49 precomputedCount = s.precomputedCount;
50 bitLength = s.bitLength;
51 if (data)
52 {
53 delete[] data;
54 }
55 if (bitLength > 0)
56 {
57 data = new base[(bitLength / 8)/sizeof(base)] ;
58 std::copy(s.data, s.data + (bitLength / (sizeof(base)*8)), data);
59 }
60 else data = nullptr;
61 return *this;
62}
63
65{
66 return data;
67}
68
70{
71 return bitLength;
72}
73
75{
76 (data[b/(sizeof(base)*8)] = data[b/(sizeof(base)*8)] | ((base)1 << (b & ((sizeof(base)*8)-1)))) ;
77}
78
80{
81 (data[b/(sizeof(base)*8)] = data[b/(sizeof(base)*8)] & (~((base)1 << (b & ((sizeof(base)*8)-1))))) ;
82}
83
85{
86 return (data[b/(sizeof(base)*8)] & ((base)1 << (b & ((sizeof(base)*8)-1))));
87}
88
89
90void LSHSignature::merge(std::vector<LSHSignature>& sigs)
91{
92 for(int i = 0; i<bitLength; i++) {
93 int cnt0 = 0;
94 int cnt1 = 1;
95 for (std::vector<LSHSignature>::iterator it = sigs.begin(); it!=sigs.end(); it++) {
96 if(it->getBit(i)==1) {
97 cnt1++;
98 } else {
99 cnt0++;
100 }
101 }
102 if (cnt1>cnt0) {
103 setBit(i);
104 } else if (cnt0>cnt1) {
105 unsetBit(i);
106 } else {
107 // on garde le bit du noyau
108 }
109 }
110}
111
112void LSHSignature::print(double rank, int limit)
113{
114 std::ostringstream os;
115 os << rank << ":sig: ";
116 if (data!=NULL) {
117 for (int i = 0; i < limit; i++) {
118 os << getBit(i) ;
119 }
120 }
121 os << std::endl;
122 std::cerr << os.str();
123}
124
126{
127 if (bitLength > 0 )
128 {
129 if ( signature.bitLength != bitLength )
130 throw 0;
131 long l = bitLength / ( 8 * sizeof ( base ) );
132 base *xdata = signature.getData();
133 while ( l-- )
134 data[l] ^= xdata[l];
135 precomputedCount = -1;
136 }
137}
138
140{
141 if (bitLength > 0) {
142 unsigned char byteCount[256] =
143 { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
144 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
145 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
146 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
147 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
148 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
149 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
150 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
151 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
152 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
153 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
154 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
155 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
156 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
157 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
158 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
159 };
160 if ( precomputedCount >= 0 )
161 return precomputedCount;
162 long l = bitLength / ( 8 * sizeof ( base ) );
163 int sum = 0;
164 unsigned char i;
165 base v;
166 while ( l-- )
167 {
168 v = data[l];
169 i = v & 0xff;
170 sum += byteCount[i];
171 i = ( v >> 8 ) & 0xff;
172 sum += byteCount[i];
173 i = ( v >> 16 ) & 0xff;
174 sum += byteCount[i];
175 i = ( v >> 24 ) & 0xff;
176 sum += byteCount[i];
177 i = ( v >> 32 ) & 0xff;
178 sum += byteCount[i];
179 i = ( v >> 40 ) & 0xff;
180 sum += byteCount[i];
181 i = ( v >> 48 ) & 0xff;
182 sum += byteCount[i];
183 i = ( v >> 56 ) & 0xff;
184 sum += byteCount[i];
185 }
186 precomputedCount = sum;
187 return sum;
188 } else return 0;
189}
190
192{
193 if (bitLength > 0)
194 {
195 unsigned char byteCount[256] =
196 { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
197 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
198 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
199 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
200 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
201 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
202 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
203 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
204 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
205 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
206 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
207 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
208 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
209 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
210 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
211 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
212 };
213
214 int sum = 0;
215 unsigned char i;
216 base v;
217 v = data[where];
218 i = v & 0xff;
219 sum += byteCount[i];
220 i = ( v >> 8 ) & 0xff;
221 sum += byteCount[i];
222 i = ( v >> 16 ) & 0xff;
223 sum += byteCount[i];
224 i = ( v >> 24 ) & 0xff;
225 sum += byteCount[i];
226 i = ( v >> 32 ) & 0xff;
227 sum += byteCount[i];
228 i = ( v >> 40 ) & 0xff;
229 sum += byteCount[i];
230 i = ( v >> 48 ) & 0xff;
231 sum += byteCount[i];
232 i = ( v >> 56 ) & 0xff;
233 sum += byteCount[i];
234 return sum;
235 }
236 else return 0;
237}
238
240 std::vector<int>& permutations)
241{
242 int bca, bcb;
243
244 for (uint p = 0; p < bitLength / ( 8 * sizeof ( base ) ); p++)
245 {
246 bca = bitCount64(permutations[p]);
247 bcb = s.bitCount64(permutations[p]);
248 if (bca != bcb)
249 return bca < bcb;
250 }
251 return false;
252}
LSHSignature(int bitLength)
void print(double rank, int limit)
void merge(std::vector< LSHSignature > &sigs)
void setBit(uint b)
bool getBit(uint b)
base * getData()
void unsetBit(uint b)
LSHSignature & operator=(const LSHSignature &s)
void xorX(LSHSignature &signature)
uint64_t base
int bitCount64(int where)
bool comparePermutedBits(LSHSignature &s, std::vector< int > &permutations)