forked from chad3814/node-hashtable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.cpp
More file actions
200 lines (136 loc) · 5.2 KB
/
hashtable.cpp
File metadata and controls
200 lines (136 loc) · 5.2 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
#include "hashtable.h"
#include <iostream>
using namespace v8;
void HashTable::init(Handle<Object> exports) {
Local<FunctionTemplate> constructor = FunctionTemplate::New(Constructor);
constructor->SetClassName(String::NewSymbol("HashTable"));
constructor->InstanceTemplate()->SetInternalFieldCount(1);
auto prototype = constructor->PrototypeTemplate();
prototype->Set("put", FunctionTemplate::New(Put)->GetFunction());
prototype->Set("get", FunctionTemplate::New(Get)->GetFunction());
prototype->Set("keys", FunctionTemplate::New(Keys)->GetFunction());
prototype->Set("remove", FunctionTemplate::New(Remove)->GetFunction());
prototype->Set("clear", FunctionTemplate::New(Clear)->GetFunction());
prototype->Set("size", FunctionTemplate::New(Size)->GetFunction());
prototype->Set("rehash", FunctionTemplate::New(Rehash)->GetFunction());
prototype->Set("reserve", FunctionTemplate::New(Reserve)->GetFunction());
prototype->Set("max_load_factor", FunctionTemplate::New(MaxLoadFactor)->GetFunction());
exports->Set(String::NewSymbol("HashTable"), Persistent<Function>::New(constructor->GetFunction()));
}
HashTable::HashTable() {}
HashTable::HashTable(size_t buckets) : map(buckets) {}
HashTable::~HashTable() {
for(auto itr = this->map.begin(); itr != this->map.end(); ) {
Persistent<Value> value = itr->second;
value.Dispose();
itr = this->map.erase(itr);
}
}
Handle<Value> HashTable::Constructor(const Arguments& args) {
HashTable *obj;
if(args.Length() > 0 && args[0]->IsInt32()) {
int buckets = args[0]->Int32Value();
obj = new HashTable(buckets);
} else {
obj = new HashTable();
}
obj->Wrap(args.This());
return args.This();
}
Handle<Value> HashTable::Get(const Arguments& args) {
HandleScope scope;
HashTable *obj = ObjectWrap::Unwrap<HashTable>(args.This());
//How the fuck do I throw exceptions from V8
Local<Value> key = Local<Value>(args[0]);
String::AsciiValue keyStr(key);
auto itr = obj->map.find(std::string(*keyStr));
if(itr == obj->map.end()) {
return scope.Close(Handle<Value>()); //return undefined
}
Persistent<Value> value = itr->second;
return scope.Close(value);
}
Handle<Value> HashTable::Put(const Arguments& args) {
HandleScope scope;
HashTable *obj = ObjectWrap::Unwrap<HashTable>(args.This());
Local<Value> key = Local<Value>(args[0]);
Local<Value> value = Local<Value>(args[1]);
String::AsciiValue keyStr(key);
auto itr = obj->map.find(std::string(*keyStr));
//overwriting an existing value
if(itr != obj->map.end()) {
Persistent<Value> oldValue = itr->second;
oldValue.Dispose(); //release the handle to the GC
}
Persistent<Value> persistent = Persistent<Value>::New(value);
obj->map.insert(std::pair<std::string, Persistent<Value> >(std::string(*keyStr), persistent));
//Return undefined
return scope.Close(Local<Value>());
}
Handle<Value> HashTable::Keys(const Arguments& args) {
HandleScope scope;
HashTable *obj = ObjectWrap::Unwrap<HashTable>(args.This());
Local<Array> array = Array::New();
int i = 0;
for(auto itr = obj->map.begin(); itr != obj->map.end(); ++itr, ++i) {
array->Set(Integer::New(i), String::New(itr->first.c_str()));
}
return scope.Close(array);
}
Handle<Value> HashTable::Remove(const Arguments& args) {
HandleScope scope;
HashTable *obj = ObjectWrap::Unwrap<HashTable>(args.This());
Local<Value> key = Local<Value>(args[0]);
String::AsciiValue keyStr(key);
auto itr = obj->map.find(std::string(*keyStr));
if(itr == obj->map.end()) {
return scope.Close(Local<Value>()); //do nothing and return undefined
}
Persistent<Value> value = itr->second;
value.Dispose();
obj->map.erase(itr);
return scope.Close(Local<Value>());
}
Handle<Value> HashTable::Clear(const Arguments& args) {
HandleScope scope;
HashTable *obj = ObjectWrap::Unwrap<HashTable>(args.This());
for(auto itr = obj->map.begin(); itr != obj->map.end(); ) {
Persistent<Value> value = itr->second;
value.Dispose();
itr = obj->map.erase(itr);
}
return scope.Close(Local<Value>());
}
Handle<Value> HashTable::Size(const Arguments& args) {
HandleScope scope;
HashTable *obj = ObjectWrap::Unwrap<HashTable>(args.This());
return scope.Close(Integer::New(obj->map.size()));
}
Handle<Value> HashTable::Rehash(const Arguments& args) {
HandleScope scope;
HashTable *obj = ObjectWrap::Unwrap<HashTable>(args.This());
size_t buckets = args[0]->Int32Value();
obj->map.rehash(buckets);
return scope.Close(Local<Value>());
}
Handle<Value> HashTable::Reserve(const Arguments& args) {
HandleScope scope;
HashTable *obj = ObjectWrap::Unwrap<HashTable>(args.This());
size_t elements = args[0]->Int32Value();
obj->map.rehash(elements);
return scope.Close(Local<Value>());
}
Handle<Value> HashTable::MaxLoadFactor(const Arguments& args) {
HandleScope scope;
HashTable *obj = ObjectWrap::Unwrap<HashTable>(args.This());
if(args.Length() > 0) {
Number *num = static_cast<Number*>(*args[0]);
float factor = (float)num->Value();
if(factor > 0)
obj->map.max_load_factor(factor);
return scope.Close(Local<Value>());
} else {
float factor = obj->map.max_load_factor();
return scope.Close(Number::New((double)factor));
}
}