-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_associative.cc
91 lines (70 loc) · 1.84 KB
/
test_associative.cc
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
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <unistd.h>
#include <lace/singleton.h>
#include <lace/random.h>
#include "heap.h"
#include "table.h"
struct node {
int value;
node(int v) : value(v) { }
lite::heap_link<node> heap_link;
lite::table_link<node> table_link;
bool
bound() const {
return false
|| heap_link.bound()
|| table_link.bound()
;;
}
void kill() { if (!bound()) delete this; }
typedef lite::heap<node, &node::heap_link, typeof(node::value), &node::value> heap_t;
typedef lite::table<node, &node::table_link, typeof(node::value), &node::value> table_t;
};
int
main(int, char*[]) {
lace::random & rng = lace::singleton<lace::random>::instance();
node::heap_t heap;
node::table_t table;
const size_t BUCKETS = 8;
node::table_t::bucket_t buckets[BUCKETS];
table.rehash(buckets, BUCKETS/2);
node* v = new node(rng.l() % 1000);
table.set(v);
static const unsigned n = 16;
for (unsigned i = 0 ; i < n ; ++i) {
node* x = new node(rng.l() % 1000);
table.set(x);
heap.inhume(x);
}
std::cout << "values";
for (node* i = table.iterator() ; i ; i = table.next(i)) {
std::cout << " " << i->value;
if (i == v)
std::cout << "*";
}
std::cout << std::endl;
table[v->value];
std::cout << "access";
for (node* i = table.iterator() ; i ; i = table.next(i)) {
std::cout << " " << i->value;
if (i == v)
std::cout << "*";
}
std::cout << std::endl;
table.rehash(buckets, BUCKETS);
std::cout << "rehash";
for (node* i = table.iterator() ; i ; i = table.next(i)) {
std::cout << " " << i->value;
if (i == v)
std::cout << "*";
}
std::cout << std::endl;
while (!heap.empty())
table.bus(heap.exhume())->kill();
table.bus(table.get(v->value))->kill();
table.dehash();
return EXIT_SUCCESS;
}
//