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

Side by Side Diff: src/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
« no previous file with comments | « src/hashmap.h ('k') | test/cctest/test-hashmap.cc » ('j') | 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 HashMap::~HashMap() { 52 HashMap::~HashMap() {
53 if (allocator_) { 53 if (allocator_) {
54 allocator_->Delete(map_); 54 allocator_->Delete(map_);
55 } 55 }
56 } 56 }
57 57
58 58
59 HashMap::Entry* HashMap::Lookup(void* key, uint32_t hash, bool insert) { 59 HashMap::Entry* HashMap::Lookup(void* key, uint32_t hash, bool insert) {
60 // Find a matching entry. 60 // Find a matching entry.
61 Entry* p = Probe(key, hash); 61 Entry* p = Probe(key, hash);
62 if (p->key != NULL) { 62 if (p->key != NULL) {
63 return p; 63 return p;
64 } 64 }
65 65
66 // No entry found; insert one if necessary. 66 // No entry found; insert one if necessary.
67 if (insert) { 67 if (insert) {
68 p->key = key; 68 p->key = key;
69 p->value = NULL; 69 p->value = NULL;
70 p->hash = hash; 70 p->hash = hash;
71 occupancy_++; 71 occupancy_++;
72 72
73 // Grow the map if we reached >= 80% occupancy. 73 // Grow the map if we reached >= 80% occupancy.
74 if (occupancy_ + occupancy_/4 >= capacity_) { 74 if (occupancy_ + occupancy_/4 >= capacity_) {
75 Resize(); 75 Resize();
76 p = Probe(key, hash); 76 p = Probe(key, hash);
77 } 77 }
78 78
79 return p; 79 return p;
80 } 80 }
81 81
82 // No entry found and none inserted. 82 // No entry found and none inserted.
83 return NULL; 83 return NULL;
84 } 84 }
85 85
86 86
87 void HashMap::Remove(void* key, uint32_t hash) {
88 // Lookup the entry for the key to remove.
89 Entry *p = Probe(key, hash);
90 if (p->key == NULL) {
91 // Key not found nothing to remove.
92 return;
93 }
94
95 // To remove the entry we need to ensure that it does not create an empty
96 // entry that will cause search for an entry to stop to soon. If all the
Dean McNamee 2009/05/15 07:24:00 too soon
97 // entries between the entry to remove and the next empty slot have their
98 // initial position inside this interval clearing the entry to remove will not
Dean McNamee 2009/05/15 07:24:00 interval,
99 // break the search. If while searching for the next empty entry an entry is
100 // encountered which does not have its initial position between the entry to
101 // remove and the position looked at this entry can be moved to the place of
Dean McNamee 2009/05/15 07:24:00 looked at,
102 // the entry to remove without breaking the search for it and the new entry to
103 // remove will be its previous position.
Dean McNamee 2009/05/15 07:24:00 runon :)
104 // Algorithm from http://en.wikipedia.org/wiki/Open_addressing.
105
106 // This guarantees loop termination as there is at least one empty entry so
107 // eventually the removed entyr will have an empty entry after it.
Dean McNamee 2009/05/15 07:24:00 entry
108 ASSERT(occupancy_ < capacity_);
109
110 // p is the candidate entry to clear. q is used to scan forwards.
111 Entry* q = p; // Start at the entry to remove.
112 while (true) {
113 // Move q to the next entry.
114 q = q + 1;
115 if (q == map_end()) {
116 q = map_;
117 }
118
119 // All entries between p and q have their initial position between p and q
120 // and the entry p can be cleared without breaking the search for these
121 // entries.
122 if (q->key == NULL) {
123 break;
124 }
125
126 // Find the initial position for the entry at position q.
127 Entry* r = map_ + (q->hash & (capacity_ - 1));
128
129 // If the entry at position q has its initial position outside the range
130 // between p and q it can be moved forward to position p and will still be
131 // found. There is now a new candidate entry for clearing.
132 if (q > p && (r <= p || r > q) ||
133 q < p && (r <= p && r > q)) {
134 *p = *q;
135 p = q;
136 }
137 }
138
139 // Clear the entry which is allowed to en emptied.
140 p->key = NULL;
141 occupancy_--;
142 }
143
144
87 void HashMap::Clear() { 145 void HashMap::Clear() {
88 // Mark all entries as empty. 146 // Mark all entries as empty.
89 const Entry* end = map_end(); 147 const Entry* end = map_end();
90 for (Entry* p = map_; p < end; p++) { 148 for (Entry* p = map_; p < end; p++) {
91 p->key = NULL; 149 p->key = NULL;
92 } 150 }
93 occupancy_ = 0; 151 occupancy_ = 0;
94 } 152 }
95 153
96 154
(...skipping 15 matching lines...) Expand all
112 170
113 171
114 HashMap::Entry* HashMap::Probe(void* key, uint32_t hash) { 172 HashMap::Entry* HashMap::Probe(void* key, uint32_t hash) {
115 ASSERT(key != NULL); 173 ASSERT(key != NULL);
116 174
117 ASSERT(IsPowerOf2(capacity_)); 175 ASSERT(IsPowerOf2(capacity_));
118 Entry* p = map_ + (hash & (capacity_ - 1)); 176 Entry* p = map_ + (hash & (capacity_ - 1));
119 const Entry* end = map_end(); 177 const Entry* end = map_end();
120 ASSERT(map_ <= p && p < end); 178 ASSERT(map_ <= p && p < end);
121 179
122 ASSERT(occupancy_ < capacity_); // guarantees loop termination 180 ASSERT(occupancy_ < capacity_); // Guarantees loop termination.
123 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) { 181 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) {
124 p++; 182 p++;
125 if (p >= end) { 183 if (p >= end) {
126 p = map_; 184 p = map_;
127 } 185 }
128 } 186 }
129 187
130 return p; 188 return p;
131 } 189 }
132 190
(...skipping 21 matching lines...) Expand all
154 n--; 212 n--;
155 } 213 }
156 } 214 }
157 215
158 // Delete old map. 216 // Delete old map.
159 allocator_->Delete(map); 217 allocator_->Delete(map);
160 } 218 }
161 219
162 220
163 } } // namespace v8::internal 221 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/hashmap.h ('k') | test/cctest/test-hashmap.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698