-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.cpp
More file actions
55 lines (48 loc) · 1.2 KB
/
table.cpp
File metadata and controls
55 lines (48 loc) · 1.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
//table.cpp
#include "table.h"
Table::Table(){
alm.push_back(new vector<Entry *>);
}
void Table::levelUp() {
alm.push_back(new vector<Entry *>);
}
void Table::levelDown() {
alm.pop_back();
}
void Table::insert(string key,Value *s) {
vector<Entry *> &v=*alm[alm.size()-1];
for(size_t i=0; i<v.size(); i++) {
if(v[i]->value && v[i]->key.compare(key)==0){
//printf("redefine %d %s\n",i,s->name.c_str());
v[i]->value=s;
return;
}
}
//printf("insert %d %s\n",i,s->name.c_str());
v.push_back(new Entry(key,s));
return;
}
Value *Table::search(string key) {
for(size_t l=alm.size(); l>=1; l--) { //size_t is unsigned
vector<Entry *> &v=*alm[l-1];
for(size_t i=0; i<v.size(); i++) {
//printf("searching %s %s\n",key.c_str(),alm[i].key.c_str());
if(v[i]->value && v[i]->key.compare(key)==0) return v[i]->value;
}
}
return 0;
}
void Table::print() {
printf("table print\n");
for(size_t l=alm.size(); l>=1; l--) {
vector<Entry *> &v=*alm[l-1];
printf("level %ld \n",l-1);
for(size_t i=0; i<v.size(); i++) {
if(v[i]->value) {
printf("* %s ",v[i]->key.c_str());
v[i]->value->print();
}
}
}
printf("\n");
}