-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintset.hpp
129 lines (111 loc) · 2.46 KB
/
intset.hpp
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
#ifndef __INTSET_HPP__
#define __INTSET_HPP__
#include <map>
#include <vector>
#include <algorithm>
#include <stdexcept>
using namespace std;
struct node
{
int key_val;
struct node *next_id;
};
struct bstNode
{
int val;
struct bstNode *left;
struct bstNode *right;
};
unsigned int MurmurHash2(const void *key, int len, unsigned int seed);
class IntSet
{
protected:
static const unsigned int MASK = 0x80000000;
unsigned int set_size;
const unsigned int max_elements;
const int max_val;
public:
virtual void insert(int element) = 0;
virtual const void report(int *v) = 0;
const unsigned int size()
{
return set_size;
}
IntSet(unsigned int _max_elements, int _max_val) : set_size(0),
max_elements(_max_elements),
max_val(_max_val)
{
/* Check max_elements */
if ((_max_elements & MASK) == MASK)
{
throw invalid_argument("Max elements must be bigger than 0");
}
}
virtual ~IntSet() {}
};
class IntSetArr : public IntSet
{
private:
bool *arr;
public:
IntSetArr(unsigned int max_elements, int max_val);
~IntSetArr();
void insert(int element);
void const report(int *v);
};
class IntSetList : public IntSet
{
private:
struct node *root_node;
void deleteAllNodes();
void list_insert(int elements);
public:
IntSetList(unsigned int max_elements, int max_val);
~IntSetList();
void insert(int element);
void const report(int *v);
};
class IntSetBST : public IntSet
{
private:
struct bstNode *root_node = nullptr;
vector<struct bstNode *> node_vector;
inline void setBstNode(int element, struct bstNode *node)
{
node->val = element;
node->left = nullptr;
node->right = nullptr;
}
public:
IntSetBST(unsigned int max_elements, int max_val);
~IntSetBST();
void insert(int element);
void const report(int *v);
};
class IntSetBitVec : public IntSet
{
private:
char *bits;
public:
IntSetBitVec(unsigned int max_elements, int max_val);
~IntSetBitVec();
void insert(int element);
void remove(int element);
int contains(int element);
void const report(int *v);
};
class IntSetBins : public IntSet
{
private:
map<int, vector<int> *> bucket;
public:
static inline unsigned int getKey(int element)
{
return MurmurHash2(&element, 1, 0xfffffff);
}
IntSetBins(unsigned int max_elements, int max_val);
~IntSetBins();
void insert(int element);
void const report(int *v);
};
#endif