2012-07-21 00:55:47 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2016-10-26 07:26:03 +00:00
|
|
|
#ifndef RUNTIME_VM_BIT_SET_H_
|
|
|
|
#define RUNTIME_VM_BIT_SET_H_
|
2012-07-21 00:55:47 +00:00
|
|
|
|
2012-07-24 02:45:31 +00:00
|
|
|
#include "platform/utils.h"
|
2012-07-21 00:55:47 +00:00
|
|
|
#include "vm/globals.h"
|
|
|
|
|
|
|
|
namespace dart {
|
|
|
|
|
|
|
|
// Just like its namesake in the STL, a BitSet object contains a fixed
|
|
|
|
// length sequence of bits.
|
2016-11-08 21:54:47 +00:00
|
|
|
template <intptr_t N>
|
2012-07-21 00:55:47 +00:00
|
|
|
class BitSet {
|
|
|
|
public:
|
2016-11-08 21:54:47 +00:00
|
|
|
BitSet() { Reset(); }
|
2012-07-21 00:55:47 +00:00
|
|
|
|
|
|
|
void Set(intptr_t i, bool value) {
|
|
|
|
ASSERT(i >= 0);
|
|
|
|
ASSERT(i < N);
|
2014-09-05 01:07:57 +00:00
|
|
|
uword mask = (static_cast<uword>(1) << (i & (kBitsPerWord - 1)));
|
2012-07-21 00:55:47 +00:00
|
|
|
if (value) {
|
2014-09-05 01:07:57 +00:00
|
|
|
data_[i >> kBitsPerWordLog2] |= mask;
|
2012-07-21 00:55:47 +00:00
|
|
|
} else {
|
2014-09-05 01:07:57 +00:00
|
|
|
data_[i >> kBitsPerWordLog2] &= ~mask;
|
2012-07-21 00:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 01:07:57 +00:00
|
|
|
bool Test(intptr_t i) const {
|
2012-07-21 00:55:47 +00:00
|
|
|
ASSERT(i >= 0);
|
|
|
|
ASSERT(i < N);
|
2014-09-05 01:07:57 +00:00
|
|
|
uword mask = (static_cast<uword>(1) << (i & (kBitsPerWord - 1)));
|
|
|
|
return (data_[i >> kBitsPerWordLog2] & mask) != 0;
|
2012-07-21 00:55:47 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 01:07:57 +00:00
|
|
|
intptr_t Next(intptr_t i) const {
|
2012-07-24 02:45:31 +00:00
|
|
|
ASSERT(i >= 0);
|
|
|
|
ASSERT(i < N);
|
2014-09-05 01:07:57 +00:00
|
|
|
intptr_t w = i >> kBitsPerWordLog2;
|
2014-09-10 05:50:08 +00:00
|
|
|
uword mask = ~static_cast<uword>(0) << (i & (kBitsPerWord - 1));
|
2012-07-24 02:45:31 +00:00
|
|
|
if ((data_[w] & mask) != 0) {
|
|
|
|
uword tz = Utils::CountTrailingZeros(data_[w] & mask);
|
2014-09-05 01:07:57 +00:00
|
|
|
return (w << kBitsPerWordLog2) + tz;
|
2012-07-24 02:45:31 +00:00
|
|
|
}
|
2014-09-05 01:07:57 +00:00
|
|
|
while (++w < kLengthInWords) {
|
2012-07-24 02:45:31 +00:00
|
|
|
if (data_[w] != 0) {
|
2014-09-05 01:07:57 +00:00
|
|
|
return (w << kBitsPerWordLog2) + Utils::CountTrailingZeros(data_[w]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
intptr_t Last() const {
|
|
|
|
for (int w = kLengthInWords - 1; w >= 0; --w) {
|
|
|
|
uword d = data_[w];
|
|
|
|
if (d != 0) {
|
2014-09-09 21:17:07 +00:00
|
|
|
return ((w + 1) << kBitsPerWordLog2) - Utils::CountLeadingZeros(d) - 1;
|
2012-07-24 02:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-09 21:17:07 +00:00
|
|
|
intptr_t ClearLastAndFindPrevious(intptr_t current_last) {
|
|
|
|
ASSERT(Test(current_last));
|
|
|
|
ASSERT(Last() == current_last);
|
|
|
|
intptr_t w = current_last >> kBitsPerWordLog2;
|
|
|
|
uword bits = data_[w];
|
|
|
|
// Clear the current last.
|
|
|
|
bits ^= (static_cast<uword>(1) << (current_last & (kBitsPerWord - 1)));
|
|
|
|
data_[w] = bits;
|
|
|
|
// Search backwards for a non-zero word.
|
|
|
|
while (bits == 0 && w > 0) {
|
|
|
|
bits = data_[--w];
|
|
|
|
}
|
|
|
|
if (bits == 0) {
|
|
|
|
// None found.
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
// Bitlength incl. w, minus leading zeroes of w, minus 1 to 0-based index.
|
|
|
|
return ((w + 1) << kBitsPerWordLog2) - Utils::CountLeadingZeros(bits) - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 21:54:47 +00:00
|
|
|
void Reset() { memset(data_, 0, sizeof(data_)); }
|
2012-07-21 00:55:47 +00:00
|
|
|
|
2016-11-08 21:54:47 +00:00
|
|
|
intptr_t Size() const { return N; }
|
2012-07-21 00:55:47 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-05 01:07:57 +00:00
|
|
|
static const int kLengthInWords = 1 + ((N - 1) / kBitsPerWord);
|
|
|
|
uword data_[kLengthInWords];
|
2012-07-21 00:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dart
|
|
|
|
|
2016-10-26 07:26:03 +00:00
|
|
|
#endif // RUNTIME_VM_BIT_SET_H_
|