MCPcopy Create free account
hub / github.com/LemonOSProject/LemonOS / HashMap

Class HashMap

Kernel/include/hash.h:27–98  ·  view source on GitHub ↗

Key, Value

Source from the content-addressed store, hash-verified

25
26template<typename K, typename T> // Key, Value
27class HashMap{
28private:
29 class KeyValuePair{
30 friend class HashMap;
31
32 protected:
33 T value;
34 K key;
35
36 KeyValuePair() { value = T(); key = K(); }
37 KeyValuePair(K key, T value) { this->value = value; this->key = key; }
38 };
39
40 List<KeyValuePair>* buckets;
41 unsigned bucketCount = 2048;
42
43 lock_t lock = 0;
44public:
45 HashMap(){
46 buckets = new List<KeyValuePair>[bucketCount];
47 lock = 0;
48 }
49
50 HashMap(unsigned bCount){
51 bucketCount = bCount;
52
53 HashMap();
54 }
55
56 void insert(K key, T& value){
57 auto& bucket = buckets[hash(key) % bucketCount];
58
59 acquireLock(&lock);
60 bucket.add_back(KeyValuePair(key, value));
61 releaseLock(&lock);
62 }
63
64 T remove(K key){
65 auto& bucket = buckets[hash(key) % bucketCount];
66
67 acquireLock(&lock);
68 for(unsigned i = 0; i < bucket.get_length(); i++){
69 if(bucket[i].key == key){
70 auto pair = bucket.remove_at(i);
71 releaseLock(&lock);
72 return pair.value;
73 }
74 }
75 releaseLock(&lock);
76
77 return T();
78 }
79
80 T get(K key){
81 auto& bucket = buckets[hash(key) % bucketCount];
82
83 acquireLock(&lock);
84 for(KeyValuePair& val : bucket){

Callers 1

HashMapMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected