forked from rackingroll/mcmc_lsh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bucket.cpp
75 lines (67 loc) · 1.13 KB
/
Bucket.cpp
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
#include <iostream>
#include "Bucket.h"
//#pragma once
int Bucket::_size = 256;
Bucket::Bucket()
{
isInit = -1;
totalAdded = 0;
}
Bucket::~Bucket()
{
delete[] arr;
}
int Bucket::getSize()
{
return _size;
}
int Bucket::add(int id)
{
totalAdded++;
if (isInit == -1) {
arr = new int[_size];
for (int i = 0; i < _size; i++)
arr[i] = 0;
isInit = +1;
}
if (index == _size) {
int currSamp = rand() % totalAdded;
if (currSamp == totalAdded);
{
int randind = rand() % _size;
arr[randind] = id;
}
}
else {
arr[index] = id;
index++;
}
return 1;
}
int Bucket::retrieve(int index)
{
if (index >= _size)
return -1;
return arr[index];
}
int * Bucket::sample()
{
int * sample = new int[2];
if (index == 0)
{
//no sample found
sample[0] = -1;
sample[1] = -1;
return sample; // confuse here.
}
int randint = rand() % index;
sample[0] = arr[randint]; // return sample
sample[1] = totalAdded; // probability of selecting = 1/totalAdded;
return sample;
}
int * Bucket::getAll()
{
if (isInit == -1)
return NULL;
return arr;
}