Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #ifndef VM_BIT_VECTOR_H_ | |
| 6 #define VM_BIT_VECTOR_H_ | |
| 7 | |
| 8 #include "vm/allocation.h" | |
| 9 #include "vm/zone.h" | |
| 10 | |
| 11 namespace dart { | |
| 12 | |
|
srdjan
2012/05/12 00:00:55
This class seems to generic for what you currently
Kevin Millikin (Google)
2012/05/15 11:51:44
I've added tests.
It's a bit unfortunate to strip
srdjan
2012/05/15 22:05:32
Generally we should separate CLs that contribute c
| |
| 13 // Bit vector implementation. | |
| 14 class BitVector: public ZoneAllocated { | |
| 15 public: | |
| 16 // Iterator for the elements of this BitVector. | |
| 17 class Iterator : public ValueObject { | |
| 18 public: | |
| 19 explicit Iterator(BitVector* target) | |
| 20 : target_(target), | |
| 21 bit_index_(-1), | |
| 22 word_index_(0), | |
| 23 current_word_(target->data_[0]) { | |
| 24 ASSERT(target->data_length_ > 0); | |
| 25 Advance(); | |
| 26 } | |
| 27 ~Iterator() { } | |
| 28 | |
| 29 bool Done() const { return word_index_ >= target_->data_length_; } | |
| 30 void Advance(); | |
| 31 | |
| 32 int Current() const { | |
| 33 ASSERT(!Done()); | |
| 34 return bit_index_; | |
| 35 } | |
| 36 | |
| 37 private: | |
| 38 BitVector* target_; | |
| 39 int bit_index_; | |
| 40 int word_index_; | |
| 41 uword current_word_; | |
| 42 | |
| 43 friend class BitVector; | |
| 44 }; | |
| 45 | |
| 46 BitVector(int length, Zone* zone) | |
| 47 : length_(length), | |
| 48 data_length_(SizeFor(length)), | |
| 49 data_(zone->AllocateArray<uword>(data_length_)) { | |
| 50 ASSERT(length > 0); | |
| 51 Clear(); | |
| 52 } | |
| 53 | |
| 54 BitVector(const BitVector& other, Zone* zone) | |
| 55 : length_(other.length()), | |
| 56 data_length_(SizeFor(length_)), | |
| 57 data_(zone->AllocateArray<uword>(data_length_)) { | |
|
srdjan
2012/05/12 00:00:55
Do not pass in the Zone, instead use Isolate::Curr
Kevin Millikin (Google)
2012/05/15 11:51:44
OK, but it seems better without the needing to go
srdjan
2012/05/15 22:05:32
The interface is more complex when passing Zone, w
| |
| 58 CopyFrom(other); | |
| 59 } | |
| 60 | |
| 61 static int SizeFor(int length) { | |
|
srdjan
2012/05/12 00:00:55
intptr_t instead of int (everywhere).
Kevin Millikin (Google)
2012/05/15 11:51:44
OK, I've done that here.
This class is currently
srdjan
2012/05/15 22:05:32
The VM rule is that all memory sizes should be in
| |
| 62 return 1 + ((length - 1) / sizeof(uword)); | |
| 63 } | |
| 64 | |
| 65 BitVector& operator=(const BitVector& rhs) { | |
| 66 if (this != &rhs) CopyFrom(rhs); | |
| 67 return *this; | |
| 68 } | |
| 69 | |
| 70 void CopyFrom(const BitVector& other) { | |
| 71 ASSERT(other.length() <= length()); | |
| 72 for (int i = 0; i < other.data_length_; i++) { | |
| 73 data_[i] = other.data_[i]; | |
| 74 } | |
| 75 for (int i = other.data_length_; i < data_length_; i++) { | |
| 76 data_[i] = 0; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 bool Contains(int i) const { | |
| 81 ASSERT(i >= 0 && i < length()); | |
| 82 uword block = data_[i / sizeof(uword)]; | |
| 83 return (block & (1U << (i % sizeof(uword)))) != 0; | |
| 84 } | |
| 85 | |
| 86 void Add(int i) { | |
| 87 ASSERT(i >= 0 && i < length()); | |
| 88 data_[i / sizeof(uword)] |= (1U << (i % sizeof(uword))); | |
| 89 } | |
| 90 | |
| 91 void Remove(int i) { | |
| 92 ASSERT(i >= 0 && i < length()); | |
| 93 data_[i / sizeof(uword)] &= ~(1U << (i % sizeof(uword))); | |
| 94 } | |
| 95 | |
| 96 void Union(const BitVector& other) { | |
| 97 ASSERT(other.length() == length()); | |
| 98 for (int i = 0; i < data_length_; i++) { | |
| 99 data_[i] |= other.data_[i]; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 bool UnionIsChanged(const BitVector& other) { | |
| 104 ASSERT(other.length() == length()); | |
| 105 bool changed = false; | |
| 106 for (int i = 0; i < data_length_; i++) { | |
| 107 uword old_data = data_[i]; | |
| 108 data_[i] |= other.data_[i]; | |
| 109 if (data_[i] != old_data) changed = true; | |
| 110 } | |
| 111 return changed; | |
| 112 } | |
| 113 | |
| 114 void Intersect(const BitVector& other) { | |
| 115 ASSERT(other.length() == length()); | |
| 116 for (int i = 0; i < data_length_; i++) { | |
| 117 data_[i] &= other.data_[i]; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void Subtract(const BitVector& other) { | |
| 122 ASSERT(other.length() == length()); | |
| 123 for (int i = 0; i < data_length_; i++) { | |
| 124 data_[i] &= ~other.data_[i]; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void Clear() { | |
| 129 for (int i = 0; i < data_length_; i++) { | |
| 130 data_[i] = 0; | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 bool IsEmpty() const { | |
| 135 for (int i = 0; i < data_length_; i++) { | |
| 136 if (data_[i] != 0) return false; | |
| 137 } | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 bool Equals(const BitVector& other) { | |
| 142 for (int i = 0; i < data_length_; i++) { | |
| 143 if (data_[i] != other.data_[i]) return false; | |
| 144 } | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 int length() const { return length_; } | |
| 149 | |
| 150 #ifdef DEBUG | |
| 151 void Print(); | |
| 152 #endif | |
| 153 | |
| 154 private: | |
| 155 int length_; | |
| 156 int data_length_; | |
| 157 uword* data_; | |
| 158 }; | |
| 159 | |
| 160 } // namespace dart | |
| 161 | |
| 162 #endif // VM_BIT_VECTOR_H_ | |
| OLD | NEW |