| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VM_BIT_SET_H_ | 5 #ifndef VM_BIT_SET_H_ |
| 6 #define VM_BIT_SET_H_ | 6 #define VM_BIT_SET_H_ |
| 7 | 7 |
| 8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
| 9 #include "vm/globals.h" | 9 #include "vm/globals.h" |
| 10 | 10 |
| 11 namespace dart { | 11 namespace dart { |
| 12 | 12 |
| 13 // Just like its namesake in the STL, a BitSet object contains a fixed | 13 // Just like its namesake in the STL, a BitSet object contains a fixed |
| 14 // length sequence of bits. | 14 // length sequence of bits. |
| 15 template<intptr_t N> | 15 template<intptr_t N> |
| 16 class BitSet { | 16 class BitSet { |
| 17 public: | 17 public: |
| 18 BitSet() { | 18 BitSet() { |
| 19 Reset(); | 19 Reset(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 void Set(intptr_t i, bool value) { | 22 void Set(intptr_t i, bool value) { |
| 23 ASSERT(i >= 0); | 23 ASSERT(i >= 0); |
| 24 ASSERT(i < N); | 24 ASSERT(i < N); |
| 25 uword mask = (static_cast<uword>(1) << (i % kBitsPerWord)); | 25 uword mask = (static_cast<uword>(1) << (i & (kBitsPerWord - 1))); |
| 26 if (value) { | 26 if (value) { |
| 27 data_[i / kBitsPerWord] |= mask; | 27 data_[i >> kBitsPerWordLog2] |= mask; |
| 28 } else { | 28 } else { |
| 29 data_[i / kBitsPerWord] &= ~mask; | 29 data_[i >> kBitsPerWordLog2] &= ~mask; |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 bool Test(intptr_t i) { | 33 bool Test(intptr_t i) const { |
| 34 ASSERT(i >= 0); | 34 ASSERT(i >= 0); |
| 35 ASSERT(i < N); | 35 ASSERT(i < N); |
| 36 uword mask = (static_cast<uword>(1) << (i % kBitsPerWord)); | 36 uword mask = (static_cast<uword>(1) << (i & (kBitsPerWord - 1))); |
| 37 return (data_[i / kBitsPerWord] & mask) != 0; | 37 return (data_[i >> kBitsPerWordLog2] & mask) != 0; |
| 38 } | 38 } |
| 39 | 39 |
| 40 intptr_t Next(intptr_t i) { | 40 intptr_t Next(intptr_t i) const { |
| 41 ASSERT(i >= 0); | 41 ASSERT(i >= 0); |
| 42 ASSERT(i < N); | 42 ASSERT(i < N); |
| 43 intptr_t w = i / kBitsPerWord; | 43 intptr_t w = i >> kBitsPerWordLog2; |
| 44 uword mask = ~static_cast<uword>(0) << i; | 44 uword mask = ~static_cast<uword>(0) << i; |
| 45 if ((data_[w] & mask) != 0) { | 45 if ((data_[w] & mask) != 0) { |
| 46 uword tz = Utils::CountTrailingZeros(data_[w] & mask); | 46 uword tz = Utils::CountTrailingZeros(data_[w] & mask); |
| 47 return kBitsPerWord*w + tz; | 47 return (w << kBitsPerWordLog2) + tz; |
| 48 } | 48 } |
| 49 while (++w < (1 + ((N - 1) / kBitsPerWord))) { | 49 while (++w < kLengthInWords) { |
| 50 if (data_[w] != 0) { | 50 if (data_[w] != 0) { |
| 51 return kBitsPerWord*w + Utils::CountTrailingZeros(data_[w]); | 51 return (w << kBitsPerWordLog2) + Utils::CountTrailingZeros(data_[w]); |
| 52 } |
| 53 } |
| 54 return -1; |
| 55 } |
| 56 |
| 57 intptr_t Last() const { |
| 58 for (int w = kLengthInWords - 1; w >= 0; --w) { |
| 59 uword d = data_[w]; |
| 60 if (d != 0) { |
| 61 // TODO(koda): Define HighestBit(uword) or use uint64_t[] for data_. |
| 62 return (w << kBitsPerWordLog2) + Utils::HighestBit(d); |
| 52 } | 63 } |
| 53 } | 64 } |
| 54 return -1; | 65 return -1; |
| 55 } | 66 } |
| 56 | 67 |
| 57 void Reset() { | 68 void Reset() { |
| 58 memset(data_, 0, sizeof(data_)); | 69 memset(data_, 0, sizeof(data_)); |
| 59 } | 70 } |
| 60 | 71 |
| 61 intptr_t Size() const { | 72 intptr_t Size() const { |
| 62 return N; | 73 return N; |
| 63 } | 74 } |
| 64 | 75 |
| 65 private: | 76 private: |
| 66 uword data_[1 + ((N - 1) / kBitsPerWord)]; | 77 static const int kLengthInWords = 1 + ((N - 1) / kBitsPerWord); |
| 78 uword data_[kLengthInWords]; |
| 67 }; | 79 }; |
| 68 | 80 |
| 69 } // namespace dart | 81 } // namespace dart |
| 70 | 82 |
| 71 #endif // VM_BIT_SET_H_ | 83 #endif // VM_BIT_SET_H_ |
| OLD | NEW |