Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: test/cctest/test-hashmap.cc

Issue 113397: Add a remove method to the hash map (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« src/hashmap.cc ('K') | « src/hashmap.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 20 matching lines...) Expand all
31 #include "hashmap.h" 31 #include "hashmap.h"
32 #include "cctest.h" 32 #include "cctest.h"
33 33
34 using namespace v8::internal; 34 using namespace v8::internal;
35 35
36 static bool DefaultMatchFun(void* a, void* b) { 36 static bool DefaultMatchFun(void* a, void* b) {
37 return a == b; 37 return a == b;
38 } 38 }
39 39
40 40
41 typedef uint32_t (*IntKeyHash)(uint32_t key);
42
43
41 class IntSet { 44 class IntSet {
42 public: 45 public:
43 IntSet() : map_(DefaultMatchFun) {} 46 IntSet(IntKeyHash hash) : hash_(hash), map_(DefaultMatchFun) {}
44 47
45 void Insert(int x) { 48 void Insert(int x) {
46 CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value 49 CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value
47 HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), Hash(x), true); 50 HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), hash_(x), true);
48 CHECK(p != NULL); // insert is set! 51 CHECK(p != NULL); // insert is set!
49 CHECK_EQ(reinterpret_cast<void*>(x), p->key); 52 CHECK_EQ(reinterpret_cast<void*>(x), p->key);
50 // we don't care about p->value 53 // we don't care about p->value
51 } 54 }
52 55
56 void Remove(int x) {
57 CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value
58 map_.Remove(reinterpret_cast<void*>(x), hash_(x));
59 }
60
53 bool Present(int x) { 61 bool Present(int x) {
54 HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), Hash(x), false); 62 HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), hash_(x), false) ;
55 if (p != NULL) { 63 if (p != NULL) {
56 CHECK_EQ(reinterpret_cast<void*>(x), p->key); 64 CHECK_EQ(reinterpret_cast<void*>(x), p->key);
57 } 65 }
58 return p != NULL; 66 return p != NULL;
59 } 67 }
60 68
61 void Clear() { 69 void Clear() {
62 map_.Clear(); 70 map_.Clear();
63 } 71 }
64 72
65 uint32_t occupancy() const { 73 uint32_t occupancy() const {
66 uint32_t count = 0; 74 uint32_t count = 0;
67 for (HashMap::Entry* p = map_.Start(); p != NULL; p = map_.Next(p)) { 75 for (HashMap::Entry* p = map_.Start(); p != NULL; p = map_.Next(p)) {
68 count++; 76 count++;
69 } 77 }
70 CHECK_EQ(map_.occupancy(), static_cast<double>(count)); 78 CHECK_EQ(map_.occupancy(), static_cast<double>(count));
71 return count; 79 return count;
72 } 80 }
73 81
74 private: 82 private:
75 HashMap map_; 83 HashMap map_;
76 static uint32_t Hash(uint32_t key) { return key * 23; } 84 IntKeyHash hash_;
77 }; 85 };
78 86
79 87
80 TEST(Set) { 88 static uint32_t Hash(uint32_t key) { return 23; }
81 IntSet set; 89 static uint32_t CollisionHash(uint32_t key) { return key & 0x3; }
90
91
92 void TestSet(IntKeyHash hash, int size) {
93 IntSet set(hash);
82 CHECK_EQ(0, set.occupancy()); 94 CHECK_EQ(0, set.occupancy());
83 95
84 set.Insert(1); 96 set.Insert(1);
85 set.Insert(2); 97 set.Insert(2);
86 set.Insert(3); 98 set.Insert(3);
87 CHECK_EQ(3, set.occupancy()); 99 CHECK_EQ(3, set.occupancy());
88 100
89 set.Insert(2); 101 set.Insert(2);
90 set.Insert(3); 102 set.Insert(3);
91 CHECK_EQ(3, set.occupancy()); 103 CHECK_EQ(3, set.occupancy());
92 104
93 CHECK(set.Present(1)); 105 CHECK(set.Present(1));
94 CHECK(set.Present(2)); 106 CHECK(set.Present(2));
95 CHECK(set.Present(3)); 107 CHECK(set.Present(3));
96 CHECK(!set.Present(4)); 108 CHECK(!set.Present(4));
97 CHECK_EQ(3, set.occupancy()); 109 CHECK_EQ(3, set.occupancy());
98 110
111 set.Remove(1);
112 CHECK(!set.Present(1));
113 CHECK(set.Present(2));
114 CHECK(set.Present(3));
115 CHECK_EQ(2, set.occupancy());
116
117 set.Remove(3);
118 CHECK(!set.Present(1));
119 CHECK(set.Present(2));
120 CHECK(!set.Present(3));
121 CHECK_EQ(1, set.occupancy());
122
99 set.Clear(); 123 set.Clear();
100 CHECK_EQ(0, set.occupancy()); 124 CHECK_EQ(0, set.occupancy());
101 125
102 // Insert a long series of values. 126 // Insert a long series of values.
103 const int start = 453; 127 const int start = 453;
104 const int factor = 13; 128 const int factor = 13;
105 const int offset = 7; 129 const int offset = 7;
106 const uint32_t n = 1000; 130 const uint32_t n = size;
107 131
108 int x = start; 132 int x = start;
109 for (uint32_t i = 0; i < n; i++) { 133 for (uint32_t i = 0; i < n; i++) {
110 CHECK_EQ(i, static_cast<double>(set.occupancy())); 134 CHECK_EQ(i, static_cast<double>(set.occupancy()));
111 set.Insert(x); 135 set.Insert(x);
112 x = x*factor + offset; 136 x = x * factor + offset;
113 } 137 }
138 CHECK_EQ(n, static_cast<double>(set.occupancy()));
114 139
115 // Verify the same sequence of values. 140 // Verify the same sequence of values.
116 x = start; 141 x = start;
117 for (uint32_t i = 0; i < n; i++) { 142 for (uint32_t i = 0; i < n; i++) {
118 CHECK(set.Present(x)); 143 CHECK(set.Present(x));
119 x = x*factor + offset; 144 x = x * factor + offset;
120 } 145 }
146 CHECK_EQ(n, static_cast<double>(set.occupancy()));
121 147
122 CHECK_EQ(n, static_cast<double>(set.occupancy())); 148 // Remove all these values.
149 x = start;
150 for (uint32_t i = 0; i < n; i++) {
151 CHECK_EQ(n - i, static_cast<double>(set.occupancy()));
152 CHECK(set.Present(x));
153 set.Remove(x);
154 CHECK(!set.Present(x));
155 x = x * factor + offset;
156
157 // Verify the the expected values are still there.
158 int y = start;
159 for (uint32_t j = 0; j < n; j++) {
160 if (j <= i) {
161 CHECK(!set.Present(y));
162 } else {
163 CHECK(set.Present(y));
164 }
165 y = y * factor + offset;
166 }
167
168 }
169 CHECK_EQ(0, set.occupancy());
123 } 170 }
171
172
173 TEST(Set) {
174 TestSet(Hash, 1000);
175 TestSet(CollisionHash, 100);
176 }
OLDNEW
« src/hashmap.cc ('K') | « src/hashmap.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698