blob: 0fbb01811a3971c97cf819a84053c0684c671f17 [file] [log] [blame]
Leon Clarke4515c472010-02-03 11:58:03 +00001// Copyright 2010 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Leon Clarke4515c472010-02-03 11:58:03 +00004
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005#include "src/bit-vector.h"
Leon Clarke4515c472010-02-03 11:58:03 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/base/bits.h"
Leon Clarke4515c472010-02-03 11:58:03 +00008
9namespace v8 {
10namespace internal {
11
Steve Block6ded16b2010-05-10 14:33:55 +010012#ifdef DEBUG
13void BitVector::Print() {
14 bool first = true;
15 PrintF("{");
16 for (int i = 0; i < length(); i++) {
17 if (Contains(i)) {
18 if (!first) PrintF(",");
19 first = false;
Ben Murdochf87a2032010-10-22 12:50:53 +010020 PrintF("%d", i);
Steve Block6ded16b2010-05-10 14:33:55 +010021 }
22 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023 PrintF("}\n");
Steve Block6ded16b2010-05-10 14:33:55 +010024}
25#endif
26
27
Ben Murdochb0fe1622011-05-05 13:52:32 +010028void BitVector::Iterator::Advance() {
29 current_++;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040030 uintptr_t val = current_value_;
Ben Murdochb0fe1622011-05-05 13:52:32 +010031 while (val == 0) {
32 current_index_++;
33 if (Done()) return;
34 val = target_->data_[current_index_];
Emily Bernierd0a1eb72015-03-24 16:35:39 -040035 current_ = current_index_ << kDataBitShift;
Ben Murdochb0fe1622011-05-05 13:52:32 +010036 }
37 val = SkipZeroBytes(val);
38 val = SkipZeroBits(val);
39 current_value_ = val >> 1;
40}
41
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042
43int BitVector::Count() const {
44 int count = 0;
45 for (int i = 0; i < data_length_; i++) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040046 uintptr_t data = data_[i];
47 if (sizeof(data) == 8) {
48 count += base::bits::CountPopulation64(data);
49 } else {
50 count += base::bits::CountPopulation32(static_cast<uint32_t>(data));
51 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052 }
53 return count;
54}
55
56} // namespace internal
57} // namespace v8