xref: /aosp_15_r20/art/libartbase/base/bit_utils_iterator.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBARTBASE_BASE_BIT_UTILS_ITERATOR_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTBASE_BASE_BIT_UTILS_ITERATOR_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <iterator>
21*795d594fSAndroid Build Coastguard Worker #include <limits>
22*795d594fSAndroid Build Coastguard Worker #include <type_traits>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker #include "bit_utils.h"
27*795d594fSAndroid Build Coastguard Worker #include "iteration_range.h"
28*795d594fSAndroid Build Coastguard Worker #include "stl_util.h"
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker namespace art {
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker // Using the Curiously Recurring Template Pattern to implement everything shared
33*795d594fSAndroid Build Coastguard Worker // by LowToHighBitIterator and HighToLowBitIterator, i.e. everything but operator*().
34*795d594fSAndroid Build Coastguard Worker template <typename T, typename Iter>
35*795d594fSAndroid Build Coastguard Worker class BitIteratorBase {
36*795d594fSAndroid Build Coastguard Worker   static_assert(std::is_integral_v<T>, "T must be integral");
37*795d594fSAndroid Build Coastguard Worker   static_assert(std::is_unsigned_v<T>, "T must be unsigned");
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker   static_assert(sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint64_t), "Unsupported size");
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker  public:
42*795d594fSAndroid Build Coastguard Worker   using iterator_category = std::forward_iterator_tag;
43*795d594fSAndroid Build Coastguard Worker   using value_type = uint32_t;
44*795d594fSAndroid Build Coastguard Worker   using difference_type = ptrdiff_t;
45*795d594fSAndroid Build Coastguard Worker   using pointer = void;
46*795d594fSAndroid Build Coastguard Worker   using reference = void;
47*795d594fSAndroid Build Coastguard Worker 
BitIteratorBase()48*795d594fSAndroid Build Coastguard Worker   BitIteratorBase() : bits_(0u) { }
BitIteratorBase(T bits)49*795d594fSAndroid Build Coastguard Worker   explicit BitIteratorBase(T bits) : bits_(bits) { }
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker   Iter& operator++() {
52*795d594fSAndroid Build Coastguard Worker     DCHECK_NE(bits_, 0u);
53*795d594fSAndroid Build Coastguard Worker     uint32_t bit = *static_cast<Iter&>(*this);
54*795d594fSAndroid Build Coastguard Worker     bits_ &= ~(static_cast<T>(1u) << bit);
55*795d594fSAndroid Build Coastguard Worker     return static_cast<Iter&>(*this);
56*795d594fSAndroid Build Coastguard Worker   }
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker   Iter& operator++(int) {
59*795d594fSAndroid Build Coastguard Worker     Iter tmp(static_cast<Iter&>(*this));
60*795d594fSAndroid Build Coastguard Worker     ++*this;
61*795d594fSAndroid Build Coastguard Worker     return tmp;
62*795d594fSAndroid Build Coastguard Worker   }
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker  protected:
65*795d594fSAndroid Build Coastguard Worker   T bits_;
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker   template <typename U, typename I>
68*795d594fSAndroid Build Coastguard Worker   friend bool operator==(const BitIteratorBase<U, I>& lhs, const BitIteratorBase<U, I>& rhs);
69*795d594fSAndroid Build Coastguard Worker };
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker template <typename T, typename Iter>
72*795d594fSAndroid Build Coastguard Worker bool operator==(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) {
73*795d594fSAndroid Build Coastguard Worker   return lhs.bits_ == rhs.bits_;
74*795d594fSAndroid Build Coastguard Worker }
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker template <typename T, typename Iter>
77*795d594fSAndroid Build Coastguard Worker bool operator!=(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) {
78*795d594fSAndroid Build Coastguard Worker   return !(lhs == rhs);
79*795d594fSAndroid Build Coastguard Worker }
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker template <typename T>
82*795d594fSAndroid Build Coastguard Worker class LowToHighBitIterator : public BitIteratorBase<T, LowToHighBitIterator<T>> {
83*795d594fSAndroid Build Coastguard Worker  public:
84*795d594fSAndroid Build Coastguard Worker   using BitIteratorBase<T, LowToHighBitIterator<T>>::BitIteratorBase;
85*795d594fSAndroid Build Coastguard Worker 
86*795d594fSAndroid Build Coastguard Worker   uint32_t operator*() const {
87*795d594fSAndroid Build Coastguard Worker     DCHECK_NE(this->bits_, 0u);
88*795d594fSAndroid Build Coastguard Worker     return CTZ(this->bits_);
89*795d594fSAndroid Build Coastguard Worker   }
90*795d594fSAndroid Build Coastguard Worker };
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker template <typename T>
93*795d594fSAndroid Build Coastguard Worker class HighToLowBitIterator : public BitIteratorBase<T, HighToLowBitIterator<T>> {
94*795d594fSAndroid Build Coastguard Worker  public:
95*795d594fSAndroid Build Coastguard Worker   using BitIteratorBase<T, HighToLowBitIterator<T>>::BitIteratorBase;
96*795d594fSAndroid Build Coastguard Worker 
97*795d594fSAndroid Build Coastguard Worker   uint32_t operator*() const {
98*795d594fSAndroid Build Coastguard Worker     DCHECK_NE(this->bits_, 0u);
99*795d594fSAndroid Build Coastguard Worker     static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!");
100*795d594fSAndroid Build Coastguard Worker     return std::numeric_limits<T>::digits - 1u - CLZ(this->bits_);
101*795d594fSAndroid Build Coastguard Worker   }
102*795d594fSAndroid Build Coastguard Worker };
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker template <typename T>
LowToHighBits(T bits)105*795d594fSAndroid Build Coastguard Worker IterationRange<LowToHighBitIterator<T>> LowToHighBits(T bits) {
106*795d594fSAndroid Build Coastguard Worker   return IterationRange<LowToHighBitIterator<T>>(
107*795d594fSAndroid Build Coastguard Worker       LowToHighBitIterator<T>(bits), LowToHighBitIterator<T>());
108*795d594fSAndroid Build Coastguard Worker }
109*795d594fSAndroid Build Coastguard Worker 
110*795d594fSAndroid Build Coastguard Worker template <typename T>
HighToLowBits(T bits)111*795d594fSAndroid Build Coastguard Worker IterationRange<HighToLowBitIterator<T>> HighToLowBits(T bits) {
112*795d594fSAndroid Build Coastguard Worker   return IterationRange<HighToLowBitIterator<T>>(
113*795d594fSAndroid Build Coastguard Worker       HighToLowBitIterator<T>(bits), HighToLowBitIterator<T>());
114*795d594fSAndroid Build Coastguard Worker }
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker }  // namespace art
117*795d594fSAndroid Build Coastguard Worker 
118*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTBASE_BASE_BIT_UTILS_ITERATOR_H_
119