-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathccLsh.cpp
More file actions
1500 lines (1320 loc) · 41.7 KB
/
ccLsh.cpp
File metadata and controls
1500 lines (1320 loc) · 41.7 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Author: Mohamed Aly <malaa at vision d0t caltech d0t edu>
// Date: October 6, 2010
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <iterator>
#include <boost/random.hpp>
#include "ccCommon.hpp"
#include "ccLsh.hpp"
//random number generator
//boost::lagged_fibonacci23209 randEngine(0x123456);
boost::mt19937 randEngine(0x123456);
//boost::uniform_01<boost::mt19937> randUniReal(randEngine);
//boost::uniform_int<uint32_t> randUniInt;
//boost::normal_distribution<double> randNorm;
//a random number between 0 -> 1
//#define URAND (((double)randEngine() - randEngine().min()) / (double)randEngine().max_value)
//#define URAND (randEngine())
//#define URAND ((double)randUniInt(randEngine) / randUniInt.max())
//#define URAND (randUniReal())
#define URAND ((double)rand() / (double)RAND_MAX)
//get a random number between M1->M2-1
#define RAND(M1, M2) (URAND * ((M2)-(M1)) + (M1))
//#define RAND(M1, M2) ((double)randUniInt(randEngine) / randUniInt.max() * ((M2)-(M1)) + (M1))
//#define RAND(M1, M2) (float)rand() / (float)RAND_MAX * ((M2)-(M1)) + (M1)
//#define RAND(M2) RAND(0,M2)
#define IRAND(M1, M2) floor(RAND(M1,M2))
//#define IRAND(M2) IRAND(0,M2)
//normal random
//#define NRAND randNorm(randEngine)
#define NRAND randn()
//PI
#define PI 3.141592653589793f
//------------------------------------------------------------------------
// // Template function definitions
// #define TEMPLATE(F) \
// F(float) \
// F(double) \
// F(char) \
// F(int) \
// F(unsigned int) \
// F(unsigned char)
#define SEARCHPOINTS(T) \
template void searchPoints(Lsh&, Data<T>&, LshPointList*);
#define INSERTPOINTS(T) \
template void insertPoints(Lsh&, Data<T>&, uint);
#define BUCKETID(T) \
template void getBucketId(Lsh&, Data<T>&, BucketIdList*);
#define GETFUNCVAL(T) \
template void getFuncVal(Lsh&, Data<T>&, LshFuncValList*);
#define GETKNN_FL(T) \
template void getKnn(Lsh&, Data<T>&, Data<T>&, uint k, DistanceType, uint*, float* );
#define GETKNN_DB(T) \
template void getKnn(Lsh&, Data<T>&, Data<T>&, uint k, DistanceType, uint*, double* );
TEMPLATE(SEARCHPOINTS)
TEMPLATE(INSERTPOINTS)
TEMPLATE(BUCKETID)
TEMPLATE(GETFUNCVAL)
TEMPLATE(GETKNN_FL)
TEMPLATE(GETKNN_DB)
//-----------------------------------------------------------------------
/// stream overloads for LshOptions
ostream& operator<<(ostream& s, LshOptions& o)
{
//write
s.write((char*) &o, sizeof(LshOptions));
// s.write((char*) &o.ntables, sizeof(o.ntables));
// s.write((char*) &o.nfuncs, sizeof(o.nfuncs));
// s.write((char*) &o.htype, sizeof(o.htype));
// s.write((char*) &o.dist, sizeof(o.dist));
// s.write((char*) &o.norm, sizeof(o.norm));
// s.write((char*) &o.ndims, sizeof(o.ndims));
// s.write((char*) &o.nbits, sizeof(o.nbits));
// s.write((char*) &o.hwidth, sizeof(o.hwidth));
// s.write((char*) &o.opt1, sizeof(o.opt1));
// s.write((char*) &o.opt2, sizeof(o.opt2));
// s.write((char*) &o.tablesize, sizeof(o.tablesize));
// s.write((char*) &o.str, sizeof(o.str));
// s.write((char*) &o.forest, sizeof(o.forest));
// s.write((char*) &o.verbose, sizeof(o.verbose));
}
istream& operator>>(istream& s, LshOptions& o)
{
//read
s.read((char*) &o, sizeof(LshOptions));
// s.read((char*) &o.ntables, sizeof(o.ntables));
// s.read((char*) &o.nfuncs, sizeof(o.nfuncs));
// s.read((char*) &o.htype, sizeof(o.htype));
// s.read((char*) &o.dist, sizeof(o.dist));
// s.read((char*) &o.norm, sizeof(o.norm));
// s.read((char*) &o.ndims, sizeof(o.ndims));
// s.read((char*) &o.nbits, sizeof(o.nbits));
// s.read((char*) &o.hwidth, sizeof(o.hwidth));
// s.read((char*) &o.opt1, sizeof(o.opt1));
// s.read((char*) &o.opt2, sizeof(o.opt2));
// s.read((char*) &o.tablesize, sizeof(o.tablesize));
// s.read((char*) &o.str, sizeof(o.str));
// s.read((char*) &o.forest, sizeof(o.forest));
// s.read((char*) &o.verbose, sizeof(o.verbose));
}
//-----------------------------------------------------------------------
// a simple function to compute standard gaussian random numbers
// using Box-Muller polar transformations
inline float randn()
{
float x1, x2, w;
do
{
x1 = 2.0 * URAND - 1.0;
x2 = 2.0 * URAND - 1.0;
w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 || w == 0);
return x1 * sqrt(-2.0 * log( w ) / w);
}
//-----------------------------------------------------------------------
// a simple hash function that returns the remainder modulo a prime number
// using Rabin's algorithm
inline uint64_t hash(uint64_t v) { return (v % HASH_POLY); }
inline uint64_t hash(uint32_t v) { return ((uint64_t)v % HASH_POLY); }
inline uint64_t hash(int64_t v) { return ((uint64_t)v % HASH_POLY); }
inline uint64_t hash(int32_t v) { return ((uint64_t)v % HASH_POLY); }
inline uint64_t hash(float v) { return ((uint64_t)v % HASH_POLY); }
inline uint64_t hash(double v) { return ((uint64_t)v % HASH_POLY); }
//-----------------------------------------------------------------------
void LshFunc::create(const LshOptions& opt)
{
this->type = opt.htype;
this->ndims = (int)opt.ndims;
float norm = 0.;
switch(this->type)
{
case LSH_HASH_TYPE_HAM:
//choose a random dimension
if (!opt.bitsperdim)
this->func.ham = (uint)IRAND(0, opt.ndims);
else
this->func.ham = (uint)IRAND(0, opt.ndims * opt.bitsperdim);
break;
case LSH_HASH_TYPE_L1:
// choose a random dimension
this->func.l1.dim = (uint)IRAND(0, opt.ndims);
//random shift
this->func.l1.s = (float)RAND(0, opt.w);
break;
case LSH_HASH_TYPE_L2:
//random shift
this->func.l2.b = (float)RAND(0, opt.w);
//allocate memory
this->func.l2.r = new float[(uint)opt.ndims];
//random direction
for (uint i=0; i<opt.ndims; ++i)
{
// this->func.l2.r[i] = randn();
this->func.l2.r[i] = (float)NRAND;
// cout << this->func.l2.r[i] << " ";
norm += this->func.l2.r[i] * this->func.l2.r[i];
}
// cout << endl;
//normalize
norm = sqrt(norm);
for (uint i=0; i<opt.ndims; ++i)
this->func.l2.r[i] /= norm;
break;
case LSH_HASH_TYPE_COS:
//allocate memory
this->func.cos = new float[(uint)opt.ndims];
//get random direction
for (uint i=0; i<opt.ndims; ++i)
{
// this->func.cos[i] = randn();
this->func.cos[i] = (float)NRAND;
norm += this->func.cos[i] * this->func.cos[i];
}
//normalize
norm = sqrt(norm);
for (uint i=0; i<opt.ndims; ++i)
this->func.cos[i] /= norm;
break;
case LSH_HASH_TYPE_MHASH:
//choose two random numbers 0->2^32-1
this->func.minhash.a = (uint32_t)IRAND(0, 0xFFFFFFFF); //(uint32_t)IRAND(0, 1e6); // 0xFFFFFFFF);
this->func.minhash.b = (uint32_t)IRAND(0, 0xFFFFFFFF); //(uint32_t)IRAND(0, 1e6); //0xFFFFFFFF);
// cout << "randmax = " << RAND_MAX << endl << flush;
break;
case LSH_HASH_TYPE_SPH_SIMPLEX:
case LSH_HASH_TYPE_SPH_ORTHOPLEX:
case LSH_HASH_TYPE_SPH_HYPERCUBE:
//allocate memory
this->func.sphere = new float[this->ndims*(this->ndims+1)];
//get the random rotation matrix: loop over columns
for (uint j=0; j<this->ndims; ++j)
{
//generate this column
for (uint i=0; i<this->ndims; ++i)
this->func.sphere[j*this->ndims + i] = (float)NRAND;
//do gram-schmidt
for (uint k=0; k<j; ++k)
{
//get dot-product
float dp = 0.;
for (uint i=0; i<this->ndims; ++i)
dp += this->func.sphere[j*this->ndims + i] * this->func.sphere[k*this->ndims + i];
//subtract
for (uint i=0; i<this->ndims; ++i)
this->func.sphere[j*this->ndims+i] -= dp * this->func.sphere[k*this->ndims+i];
}
//calculate norm for this column
norm = 0.;
for (uint i=0; i<this->ndims; ++i)
norm += this->func.sphere[j*this->ndims+i] * this->func.sphere[j*this->ndims+i];
//normalize
norm = sqrt(norm);
for (uint i=0; i<this->ndims; ++i)
this->func.sphere[j*this->ndims+i] /= norm;
}
// cout << "matrix:" << endl;
// std::copy(this->func.sphere, this->func.sphere+this->ndims*(this->ndims+1),
// ostream_iterator<float>(cout, " "));
// cout << endl;
//special handling for simplex
if (this->type == LSH_HASH_TYPE_SPH_SIMPLEX)
{
//compute temp values
float d1 = (this->ndims + 1 - sqrt(this->ndims+1)) /
(this->ndims * (this->ndims+1));
float d2 = (1 - sqrt(this->ndims+1)) / this->ndims - d1;
//compute vertices locations and rotate them
float* v = new float[this->ndims * (this->ndims+1)];
//loop on vertices: 1->d
for (uint j=0; j<this->ndims; ++j)
for (uint i=0; i<this->ndims; ++i)
v[j*this->ndims + i] = (j==i? 1. : 0.) - d1;
//last vertex
for (uint i=0; i<this->ndims; ++i)
v[this->ndims*this->ndims + i] = d2;
//now rotate
float* rot = new float[this->ndims * (this->ndims+1)];
for (uint j=0; j<this->ndims+1; ++j)
for (uint i=0; i<this->ndims; ++i)
{
float p = 0;
for (uint k=0; k<this->ndims; ++k)
p += this->func.sphere[k*this->ndims + i] * v[j*this->ndims + k];
rot[j*this->ndims + i] = p;
}
//copy rotated vertices matrix into a
copy(rot, rot+this->ndims*(this->ndims+1), this->func.sphere);
//cleat memory
delete [] v;
delete [] rot;
}
// cout << "xx=[" ;
// std::copy(this->func.sphere, this->func.sphere+this->ndims*(this->ndims+1),
// ostream_iterator<float>(cout, " "));
// cout << "];" << endl;
break;
//Binary hashing with Gaussian Kernels
case LSH_HASH_TYPE_BIN_GAUSS:
//b: uniform [0 2pi]
this->func.bingauss.b = (float)RAND(0, 2*PI);
//t: uniform [-1 1]
this->func.bingauss.t = (float)RAND(-1, 1);
//w: independent gaussian with variance opt.w
double l = sqrt(opt.w);
this->func.bingauss.w = new float[(uint)opt.ndims];
for (uint i=0; i<opt.ndims; ++i)
this->func.bingauss.w[i] = (float)(NRAND * l);
break;
}
}
//------------------------------------------------------------------------
/// computes the bucket id for this data
template <class T>
// uint64_t getId(LshFunc& func, const T* data, uint ndims, const LshOptions& opt,
// float norm1, float norm2)
uint64_t getId(LshFunc& func, Data<T>& data, uint pid, const LshOptions& opt,
float norm1, float norm2)
{
float val;
uint64_t ret;
uint i;
//stuff for non-sparse data
T* point; uint ndims;
if (!data.isSparse())
{
pair<T*,uint> p = data.getPoint(pid);
point = p.first;
ndims = p.second;
}
//sparse
else
{
ndims = data.getSpPointDim(pid);
}
//rest should work just fine if not sparse, should handle sparse differently
//inside each one
//which hash function
switch(func.type)
{
case LSH_HASH_TYPE_HAM:
//the bit is 1 or 0
if (opt.bitsperdim == 0)
val = point[func.func.ham]==1;
else
{
// compact binary data
int m = func.func.ham % opt.bitsperdim;
int d = (int) func.func.ham / opt.bitsperdim;
val = (float)( static_cast<uint64_t>(point[d]) >> m
& (static_cast<uint64_t>(1)) );
// cout << "m: " << m << " d: " << d << endl;
}
ret = (uint64_t) val;
break;
case LSH_HASH_TYPE_L1:
//which cell is it in?
if (opt.norm)
val = floor(((float)point[func.func.l1.dim]/norm1 - func.func.l1.s + 1.)
/ opt.w);
else
val = floor(((float)point[func.func.l1.dim] - func.func.l1.s) / opt.w);
ret = (uint64_t) val;
break;
case LSH_HASH_TYPE_L2:
//project the data point
for (i=0, val=0; i<ndims; ++i)
{
val += (float)point[i] * func.func.l2.r[i];
// norm += (float)point[i] * (float)point[i];
}
//add offset to make values 0->2
if (opt.norm)
{
norm2 = sqrt(norm2);
val = (val / norm2) + 1.0; //norm2;
}
//which cell
// cout << " val = " << val << " b=" << func.func.l2.b << endl;
val = floor((val + func.func.l2.b) / opt.w);
ret = (uint64_t) val;
break;
case LSH_HASH_TYPE_COS:
//dot product
for (val=0, i=0; i<ndims; ++i)
val += (float)point[i] * func.func.cos[i];
//get sign
val = val>=0 ? 1 : 0;
ret = (uint64_t) val;
break;
case LSH_HASH_TYPE_MHASH:
//init
ret = 0xffffffffffffffffl;
uint64_t h;
//loop
for (i=0; i<ndims; ++i)
{
//get hash
h = ((uint64_t)func.func.minhash.a * (uint64_t)point[i] +
(uint64_t) func.func.minhash.b) % MIN_HASH_P;
//compare min
if (h<ret) ret = h;
}
// //compute hash values
// uint64_t* h = new uint64_t[ndims];
// for (i=0; i<ndims; ++i)
// h[i] = ((uint64_t)func.func.minhash.a * (uint64_t)point[i] +
// (uint64_t) func.func.minhash.b) % MIN_HASH_P;
//
//// cout << " d:"; copy(data, data+ndims, ostream_iterator<T>(cout, " ")); cout << endl;
//// cout << " h:"; copy(h, h+ndims, ostream_iterator<uint64_t>(cout, " ")); cout << endl;
//
// //sort and keep the minimum
// uint64_t* m = min_element(h, h+ndims);
// ret = *m;
//
// //clear
// delete [] h;
break;
case LSH_HASH_TYPE_SPH_SIMPLEX:
{
//compute inner product with all vertices and get the max
float vmax = -10;
uint64_t idmax = 0;
//loop on vertices
uint j;
for (i=0; i<ndims+1; ++i)
{
//get dot-product
for (j=0, val=0.; j<ndims; ++j)
val += func.func.sphere[i*ndims + j] * (float)point[j];
val /= sqrt(norm2);
//compare max
if (val >= vmax)
{
vmax = val;
idmax = i;
}
}
//return
ret = idmax;
break;
}
case LSH_HASH_TYPE_SPH_ORTHOPLEX:
{
//compute inner product with all vertices and get the max
float vmax = 0.;
uint64_t idmax = 0;
//loop on vertices
uint j;
for (i=0; i<func.ndims; ++i)
{
//get dot-product
for (j=0, val=0.; j<ndims; ++j)
val += func.func.sphere[i*ndims + j] * (float)point[j];
val /= sqrt(norm2);
//compare max
if ((val>=0? val : -val) >= (vmax>=0? vmax : -vmax))
{
vmax = val;
idmax = i;
}
}
//return
ret = vmax>0 ? idmax : idmax + ndims;
break;
}
case LSH_HASH_TYPE_SPH_HYPERCUBE:
//compute inner products
ret = 0;
for (i=0; i<ndims; ++i)
{
//get dot-product
float t = 0;
for (uint j=0; j<ndims; ++j)
t += func.func.sphere[i*func.ndims + j] * (float)point[j];
//if +ve, update with 2^i-1
if (t>=0)
ret += (1L << (i-1));
}
break;
//Binary hashing with Gaussian Kernels
case LSH_HASH_TYPE_BIN_GAUSS:
//dot product of w with x: w . x + b
float v;
//sparse?
if (!data.isSparse())
for (i=0; i<ndims; ++i)
v += func.func.bingauss.w[i] * (float)point[i];
//sparse
else
for (i=0; i<ndims; ++i)
{
//get sparse point val
uint row; T val;
data.getSpPointVal(pid, i, val, row);
//update dot product
v += func.func.bingauss.w[row] * (float)val;
}
//normalize if required by L2 norm
if (opt.norm)
v /= sqrt(norm2);
// cout << "w.x=" << v << endl;
//cos(w.x + b) + t
v = (float)cos(v + func.func.bingauss.b) + func.func.bingauss.t;
// cout << " cos(w.x+b)+t=" << v;
//quantize with Q(v) = sign(v)
v = v>=0 ? 1 : -1;
//get binary
v = 0.5 * (1 + v);
ret = (uint64_t) v;
// cout << " final=" << v << endl;
break;
}
// cout << " val = " << ret << endl;
// << " a=" << func.func.minhash.a << " b=" <<
// func.func.minhash.b << endl;
return ret;
}
//------------------------------------------------------------------------
// Stream overloads for LshFunc
ostream& operator<<(ostream& s, LshFunc& f)
{
//write type and ndims
s.write((char*)&f.type, sizeof(f.type));
s.write((char*)&f.ndims, sizeof(f.ndims));
//write function
switch(f.type)
{
case LSH_HASH_TYPE_HAM:
s.write((char*) &f.func.ham, sizeof(f.func.ham));
break;
case LSH_HASH_TYPE_L1:
s.write((char*) &f.func.l1.dim, sizeof(f.func.l1.dim));
s.write((char*) &f.func.l1.s, sizeof(f.func.l1.s));
break;
case LSH_HASH_TYPE_L2:
s.write((char*) &f.func.l2.b, sizeof(f.func.l2.b));
s.write((char*) f.func.l2.r, sizeof(float)*f.ndims);
break;
case LSH_HASH_TYPE_COS:
s.write((char*) f.func.cos, sizeof(float)*f.ndims);
break;
case LSH_HASH_TYPE_MHASH:
s.write((char*) &f.func.minhash, sizeof(f.func.minhash));
break;
case LSH_HASH_TYPE_SPH_SIMPLEX:
case LSH_HASH_TYPE_SPH_ORTHOPLEX:
case LSH_HASH_TYPE_SPH_HYPERCUBE:
s.write((char*) f.func.sphere, sizeof(float)*f.ndims*(f.ndims+1));
break;
}
}
istream& operator>>(istream& s, LshFunc& f)
{
//read type and ndims
s.read((char*)&f.type, sizeof(f.type));
s.read((char*)&f.ndims, sizeof(f.ndims));
//read function
switch(f.type)
{
case LSH_HASH_TYPE_HAM:
s.read((char*) &f.func.ham, sizeof(f.func.ham));
break;
case LSH_HASH_TYPE_L1:
s.read((char*) &f.func.l1.dim, sizeof(f.func.l1.dim));
s.read((char*) &f.func.l1.s, sizeof(f.func.l1.s));
break;
case LSH_HASH_TYPE_L2:
s.read((char*) &f.func.l2.b, sizeof(f.func.l2.b));
f.func.l2.r = new float[f.ndims];
s.read((char*) f.func.l2.r, sizeof(*f.func.l2.r)*f.ndims);
break;
case LSH_HASH_TYPE_COS:
f.func.cos = new float[f.ndims];
s.read((char*) f.func.cos, sizeof(*f.func.l2.r)*f.ndims);
break;
case LSH_HASH_TYPE_MHASH:
s.read((char*) &f.func.minhash, sizeof(f.func.minhash));
break;
case LSH_HASH_TYPE_SPH_SIMPLEX:
case LSH_HASH_TYPE_SPH_ORTHOPLEX:
case LSH_HASH_TYPE_SPH_HYPERCUBE:
f.func.sphere = new float[f.ndims * (f.ndims+1)];
s.read((char*) f.func.sphere, sizeof(float)*f.ndims*(f.ndims+1));
break;
}
}
//------------------------------------------------------------------------
// Stream overloads for LshFunc
ostream& operator<<(ostream& s, LshFuncs& f)
{
//first write the size
uint z = f.size();
s.write((char*) &z, sizeof(z));
//then write the list of functions
for (uint i=0; i<z; ++i)
s << f[i];
}
istream& operator>>(istream& s, LshFuncs& f)
{
//first read the size
uint z;
s.read((char*)&z, sizeof(z));
//create the list and load
f.funcs.resize(z);
for (uint i=0; i<z; ++i)
s >> f[i];
}
//------------------------------------------------------------------------
void LshBucketId::addId(uint64_t val, int pos, const LshOptions& opt)
{
//string id
if (this->str)
{
//make a string stream
stringstream ss;
//convert the value to string
ss << setw((uint)opt.nbits) << val;
//append this value to the string id
this->id.s += ss.str();
}
//double id
else
{
// //shift the input val the right number of bits
// int i;
// double t = 1;
// for (i=0; i < opt.nbits * pos; ++i)
// t *= 2;
// t *= (double)val;
// //add the value to the id
// this->id.d += t;
// //shift val the right number of bits
// this->id.d += (val << opt.nbits*pos);
//compute the hash for the new value
//the new value f(AB) = f( f(A) * f(t^64) ) + f(B)
// where A = this->id.d and B = val
// and f(t^64) is HASH_POLY_REM specific to the HASH_POLY used
// this->id.d = hash(val) + hash(this->id.d * HASH_POLY_REM);
// cout << " " << this->id.d;
if (opt.hwidth == 0)
this->id.d = (val * HASH_POLY_A[pos % HASH_POLY_A_NUM] % HASH_POLY) +
(this->id.d * HASH_POLY_REM % HASH_POLY);
else
this->id.d = (this->id.d << opt.hwidth) | val;
// cout << " + " << val << " -> " << this->id.d << endl;
}
}
//------------------------------------------------------------------------
// LshBucketId getId(LshFuncs& funcs, const T* data, uint ndims,
// const LshOptions& opt, float norm1, float norm2)
template <class T>
LshBucketId getId(LshFuncs& funcs, Data<T>& data, uint pid,
const LshOptions& opt, float norm1, float norm2)
{
//create a bucket id with the correct representation
LshBucketId id(opt.str);
//loop on the individual functions
for (uint i=0, s=funcs.size(); i<s; ++i)
{
//get the value for this function
uint64_t val = getId(funcs[i], data, pid, opt, norm1, norm2);
//combine into the Id
if (s>1)
id.addId(val, i, opt);
else
id.id.d = val;
}
//return
return id;
}
//------------------------------------------------------------------------
/// stream overloads
ostream& operator<<(ostream& s, LshBucketId& b)
{
//put the type
s.write((char*) &b.str, sizeof(b.str));
//put the id
if (b.str)
s << b.id.s;
else
s.write((char*) &b.id.d, sizeof(b.id.d));
}
istream& operator>>(istream& s, LshBucketId& b)
{
//read the type
s.read((char*) &b.str, sizeof(b.str));
//put the id
if (b.str)
s >> b.id.s;
else
s.read((char*) &b.id.d, sizeof(b.id.d));
}
//------------------------------------------------------------------------
// Stream overloads for LshBucket
ostream& operator<<(ostream& s, LshBucket& b)
{
//write the bucket id
s << b.id;
//put the size of list of points
uint z = b.size();
s.write((char*) &z, sizeof(z));
//put the ponits
for (uint i=0; i<z; ++i)
s.write((char*) &(b.plist[i]), sizeof(b.plist[i]));
}
istream& operator>>(istream& s, LshBucket& b)
{
//read the bucket id
s >> b.id;
//get the size of list of points
uint z;
s.read((char*) &z, sizeof(z));
b.resize(z);
//get the ponits
for (uint i=0; i<z; ++i)
s.read((char*) &b.plist[i], sizeof(b.plist[i]));
}
//------------------------------------------------------------------------
void LshFuncs::create(LshOptions& opt)
{
//allocate
this->funcs.resize(opt.nfuncs);
//loop on the list of functions and create
for (uint i=0; i<opt.nfuncs; ++i)
this->funcs[i].create(opt);
}
//------------------------------------------------------------------------
/// find a specific bucket in the list
bool LshBuckets::find(const LshBucket& b, LshBucketIt& bit, uint& id)
{
bool found;
LshBucketList* blist;
//check if not fixed size
if (this->fixed == false)
{
blist = &(this->varBuckets);
}
//fixed size table, so just take the mod and go to the bucket and get its
//list
else
{
//get the index
id = b.id.id.d % this->fixedBuckets.size(); //opt.tablesize;
//get the iterator of the bucket head
blist = &(this->fixedBuckets[id]);
// //now search for it in the list
// bit = lower_bound(bhead->buckets.begin(), bhead->buckets.end(), b);
// //always found
// found = true;
}
// binary search using lower_bound
bit = lower_bound(blist->begin(), blist->end(), b);
//check if found or not
if (bit!=blist->end() && *bit == b)
found = true;
else
found = false;
//return
return found;
}
//------------------------------------------------------------------------
/// insert a bucket into the list
LshBucketIt LshBuckets::insert(LshBucket& b, LshBucketIt bit, uint id)
{
//insert into the list
if (this->fixed)
bit = this->fixedBuckets[id].insert(bit, b);
else
bit = this->varBuckets.insert(bit, b);
return bit;
}
//------------------------------------------------------------------------
/// stream overloads for LshBucketList
ostream& operator<<(ostream& s, LshBucketList& b)
{
//write size
uint z = b.size();
s.write((char*) &z, sizeof(z));
//write the list of buckets
for (LshBucketIt bs=b.begin(), be=b.end(); bs!=be; bs++)
s << *bs;
}
istream& operator>>(istream& s, LshBucketList& b)
{
//read size
uint z;
s.read((char*) &z, sizeof(z));
//allocate
b.resize(z);
//read the list of buckets
for (LshBucketIt bs=b.begin(), be=b.end(); bs!=be; bs++)
s >> *bs;
}
//------------------------------------------------------------------------
/// stream overloads for LshBuckets
ostream& operator<<(ostream& s, LshBuckets& b)
{
//write type
s.write((char*) &b.fixed, sizeof(b.fixed));
//type?
if (fixed)
{
//write size
uint z = b.size();
s.write((char*) &z, sizeof(z));
//loop on lists and write
for (LshBucketListIt bs=b.fixedBuckets.begin(), be=b.fixedBuckets.end();
bs!=be; bs++)
s << *bs;
}
else
s << b.varBuckets;
}
istream& operator>>(istream& s, LshBuckets& b)
{
//read type
s.read((char*) &b.fixed, sizeof(b.fixed));
//type?
if (fixed)
{
//read number of lists
uint z;
s.read((char*) &z, sizeof(z));
//resize
b.fixedBuckets.resize(z);
for (LshBucketListIt bs=b.fixedBuckets.begin(), be=b.fixedBuckets.end();
bs!=be; bs++)
s >> *bs;
}
else
s >> b.varBuckets;
}
//------------------------------------------------------------------------
/// create functions for this table
void LshTable::createFuncs(LshOptions& opt)
{
//create the functions
this->funcs.create(opt);
}
//------------------------------------------------------------------------
/// stream overloads
ostream& operator<<(ostream& s, LshTable& t)
{
//write funcs and buckets
s << t.funcs;
s << t.buckets;
}
istream& operator>>(istream& s, LshTable& t)
{
//read
s >> t.funcs;
s >> t.buckets;
}
//------------------------------------------------------------------------
/// stream overloads for LshTables
ostream& operator<<(ostream& s, LshTables& t)
{
//write size
uint z = t.size();
s.write((char*) &z, sizeof(z));
//write the list of tables
for (uint i=0; i<z; ++i)
s << t[i];
}
istream& operator>>(istream& s, LshTables& t)
{
//read size
uint z;
s.read((char*) &z, sizeof(z));
//allocate
t.resize((uint)z);
//read the list of buckets
for (uint i=0; i<z; ++i)
s >> t[i];
}
//------------------------------------------------------------------------
// void insertPoint(LshTable& lshtab, T* point, uint ndims, uint pid,
// LshOptions& opt, float norm1, float norm2)
template <class T>
void insertPoint(LshTable& lshtab, Data<T>& data, uint inpid, uint pid,
LshOptions& opt, float norm1, float norm2)
{
// if (opt.verbose)
// cout << "Inserting point " << pid << endl;
// get the bucket id
LshBucketId bid = getId(lshtab.funcs, data, inpid, opt, norm1, norm2);
// make a bucket and search for it
LshBucket b(bid);
bool found;
LshBucketIt bit;
uint id;
found = lshtab.buckets.find(b, bit, id);
// LshBucketIt bit = lshtab.buckets.find(b, found, opt);
// cout << "bucket id = " << bid.id.d << endl;
//if not found, isnert it
if (!found)
bit = lshtab.buckets.insert(b, bit, id);
// bit = blist->insert(b, bit);
// bit = lshtab.buckets.insert(b, bit);
//now we have an iterator for the bucket, so insert the point in there
bit->insert(pid);
}
//------------------------------------------------------------------------
/// insert a set of points into the table
template <class T>
void insertPoints(LshTable& lshtab, Data<T>& data, uint idshift,
LshOptions& opt, float* norms1, float* norms2)
{
//loop on the points and insert
uint npoints = data.size();
// pair<T*, uint> p;
if (opt.norm)
for (uint i=0; i<npoints; ++i)
{
insertPoint(lshtab, data, i, i+idshift, opt, norms1[i], norms2[i]);// //get the pointer
// p = data.getPoint(i);
// //insert
// insertPoint(lshtab, p.first, p.second, i+idshift, opt, norms1[i], norms2[i]);
}
else