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