-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted_set.h
68 lines (60 loc) · 2.09 KB
/
sorted_set.h
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
#ifndef QUORA_SORTED_LIST_H
#define QUORA_SORTED_LIST_H
#include "hash_map.h"
#include "skiplist.h"
#include "util.h"
#include <cmath>
class SortedSet {
public:
typedef long IndexKey;
typedef HashMap<Value, Value> Set;
typedef HashMap<Value, Set*> Sets;
typedef SkipList<IndexKey, Value> Indexer;
typedef HashMap<Value, Indexer*> IndexerList;
public:
// @params max_set_size specifies the maximal set numbers.
// @params max_element_size_in_set specifies how many elements,
// at most, can be held in a single set.
SortedSet(int max_set_size, int max_element_size_in_set):
sets(max_set_size, &naive_hash),
indexerList(max_set_size, &naive_hash) {
this->max_element_size_in_set = max_element_size_in_set;
}
~SortedSet();
void add(Value set_id, Value key, Value score);
void remove(Value set_id, Value key);
Value get(Value set_id, Value key) ;
Value size(Value set_id);
void get_range(Value* setBegin, Value* setEnd,
Value lower, Value upper,
Indexer::Callback callback,
void* args);
private:
// Set creation and destruction
static Set* create_set(void* arg) {
SortedSet* pThis = (SortedSet*) arg;
return new Set(pThis->max_element_size_in_set,
naive_hash);
}
static Indexer* create_skip_list(void* arg) {
SortedSet* pThis = (SortedSet*) arg;
// skiplist of level max_level can hold
// 2^max_level elements
int max_level = log(pThis->max_element_size_in_set) / log(2) + 1;
return new Indexer(InvalidValue, max_level);
}
template <class TValue>
static void remove_set(const Value& key, TValue& val) {
delete val;
}
// Index Keys
IndexKey make_index_key(Value key, Value score) {
IndexKey combinedKey = score;
return (combinedKey << ValueBitSize) + key;
}
// Fields
int max_element_size_in_set;
Sets sets;
IndexerList indexerList;
};
#endif