Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 30 | 30 |
| 31 #include "v8.h" | 31 #include "v8.h" |
| 32 | 32 |
| 33 #include "ast.h" | 33 #include "ast.h" |
| 34 #include "compiler.h" | 34 #include "compiler.h" |
| 35 #include "zone-inl.h" | 35 #include "zone-inl.h" |
| 36 | 36 |
| 37 namespace v8 { | 37 namespace v8 { |
| 38 namespace internal { | 38 namespace internal { |
| 39 | 39 |
| 40 // Forward declarations. | |
| 41 class Node; | |
| 42 | |
| 43 class BitVector: public ZoneObject { | 40 class BitVector: public ZoneObject { |
| 44 public: | 41 public: |
| 45 // Iterator for the elements of this BitVector. | 42 // Iterator for the elements of this BitVector. |
| 46 class Iterator BASE_EMBEDDED { | 43 class Iterator BASE_EMBEDDED { |
| 47 public: | 44 public: |
| 48 explicit Iterator(BitVector* target) | 45 explicit Iterator(BitVector* target) |
| 49 : target_(target), | 46 : target_(target), |
| 50 current_index_(0), | 47 current_index_(0), |
| 51 current_value_(target->data_[0]), | 48 current_value_(target->data_[0]), |
| 52 current_(-1) { | 49 current_(-1) { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 #ifdef DEBUG | 191 #ifdef DEBUG |
| 195 void Print(); | 192 void Print(); |
| 196 #endif | 193 #endif |
| 197 | 194 |
| 198 private: | 195 private: |
| 199 int length_; | 196 int length_; |
| 200 int data_length_; | 197 int data_length_; |
| 201 uint32_t* data_; | 198 uint32_t* data_; |
| 202 }; | 199 }; |
| 203 | 200 |
| 204 | |
| 205 // An implementation of a sparse set whose elements are drawn from integers | |
|
Kevin Millikin (Chromium)
2011/05/02 16:37:00
This is all dead code now.
| |
| 206 // in the range [0..universe_size[. It supports constant-time Contains, | |
| 207 // destructive Add, and destructuve Remove operations and linear-time (in | |
| 208 // the number of elements) destructive Union. | |
| 209 class SparseSet: public ZoneObject { | |
| 210 public: | |
| 211 // Iterator for sparse set elements. Elements should not be added or | |
| 212 // removed during iteration. | |
| 213 class Iterator BASE_EMBEDDED { | |
| 214 public: | |
| 215 explicit Iterator(SparseSet* target) : target_(target), current_(0) { | |
| 216 ASSERT(++target->iterator_count_ > 0); | |
| 217 } | |
| 218 ~Iterator() { | |
| 219 ASSERT(target_->iterator_count_-- > 0); | |
| 220 } | |
| 221 bool Done() const { return current_ >= target_->dense_.length(); } | |
| 222 void Advance() { | |
| 223 ASSERT(!Done()); | |
| 224 ++current_; | |
| 225 } | |
| 226 int Current() { | |
| 227 ASSERT(!Done()); | |
| 228 return target_->dense_[current_]; | |
| 229 } | |
| 230 | |
| 231 private: | |
| 232 SparseSet* target_; | |
| 233 int current_; | |
| 234 | |
| 235 friend class SparseSet; | |
| 236 }; | |
| 237 | |
| 238 explicit SparseSet(int universe_size) | |
| 239 : dense_(4), | |
| 240 sparse_(ZONE->NewArray<int>(universe_size)) { | |
| 241 #ifdef DEBUG | |
| 242 size_ = universe_size; | |
| 243 iterator_count_ = 0; | |
| 244 #endif | |
| 245 } | |
| 246 | |
| 247 bool Contains(int n) const { | |
| 248 ASSERT(0 <= n && n < size_); | |
| 249 int dense_index = sparse_[n]; | |
| 250 return (0 <= dense_index) && | |
| 251 (dense_index < dense_.length()) && | |
| 252 (dense_[dense_index] == n); | |
| 253 } | |
| 254 | |
| 255 void Add(int n) { | |
| 256 ASSERT(0 <= n && n < size_); | |
| 257 ASSERT(iterator_count_ == 0); | |
| 258 if (!Contains(n)) { | |
| 259 sparse_[n] = dense_.length(); | |
| 260 dense_.Add(n); | |
| 261 } | |
| 262 } | |
| 263 | |
| 264 void Remove(int n) { | |
| 265 ASSERT(0 <= n && n < size_); | |
| 266 ASSERT(iterator_count_ == 0); | |
| 267 if (Contains(n)) { | |
| 268 int dense_index = sparse_[n]; | |
| 269 int last = dense_.RemoveLast(); | |
| 270 if (dense_index < dense_.length()) { | |
| 271 dense_[dense_index] = last; | |
| 272 sparse_[last] = dense_index; | |
| 273 } | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 void Union(const SparseSet& other) { | |
| 278 for (int i = 0; i < other.dense_.length(); ++i) { | |
| 279 Add(other.dense_[i]); | |
| 280 } | |
| 281 } | |
| 282 | |
| 283 private: | |
| 284 // The set is implemented as a pair of a growable dense list and an | |
| 285 // uninitialized sparse array. | |
| 286 ZoneList<int> dense_; | |
| 287 int* sparse_; | |
| 288 #ifdef DEBUG | |
| 289 int size_; | |
| 290 int iterator_count_; | |
| 291 #endif | |
| 292 }; | |
| 293 | |
| 294 | |
| 295 // Simple fixed-capacity list-based worklist (managed as a queue) of | |
| 296 // pointers to T. | |
| 297 template<typename T> | |
| 298 class WorkList BASE_EMBEDDED { | |
| 299 public: | |
| 300 // The worklist cannot grow bigger than size. We keep one item empty to | |
| 301 // distinguish between empty and full. | |
| 302 explicit WorkList(int size) | |
| 303 : capacity_(size + 1), head_(0), tail_(0), queue_(capacity_) { | |
| 304 for (int i = 0; i < capacity_; i++) queue_.Add(NULL); | |
| 305 } | |
| 306 | |
| 307 bool is_empty() { return head_ == tail_; } | |
| 308 | |
| 309 bool is_full() { | |
| 310 // The worklist is full if head is at 0 and tail is at capacity - 1: | |
| 311 // head == 0 && tail == capacity-1 ==> tail - head == capacity - 1 | |
| 312 // or if tail is immediately to the left of head: | |
| 313 // tail+1 == head ==> tail - head == -1 | |
| 314 int diff = tail_ - head_; | |
| 315 return (diff == -1 || diff == capacity_ - 1); | |
| 316 } | |
| 317 | |
| 318 void Insert(T* item) { | |
| 319 ASSERT(!is_full()); | |
| 320 queue_[tail_++] = item; | |
| 321 if (tail_ == capacity_) tail_ = 0; | |
| 322 } | |
| 323 | |
| 324 T* Remove() { | |
| 325 ASSERT(!is_empty()); | |
| 326 T* item = queue_[head_++]; | |
| 327 if (head_ == capacity_) head_ = 0; | |
| 328 return item; | |
| 329 } | |
| 330 | |
| 331 private: | |
| 332 int capacity_; // Including one empty slot. | |
| 333 int head_; // Where the first item is. | |
| 334 int tail_; // Where the next inserted item will go. | |
| 335 List<T*> queue_; | |
| 336 }; | |
| 337 | |
| 338 } } // namespace v8::internal | 201 } } // namespace v8::internal |
| 339 | 202 |
| 340 | 203 |
| 341 #endif // V8_DATAFLOW_H_ | 204 #endif // V8_DATAFLOW_H_ |
| OLD | NEW |