• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PeterCDMcLean / BitLib / 16544577083

26 Jul 2025 10:39PM UTC coverage: 76.997% (-1.5%) from 78.485%
16544577083

Pull #18

github

web-flow
Merge 4bac8f49d into 079daa142
Pull Request #18: From string

3414 of 5100 branches covered (66.94%)

Branch coverage included in aggregate %.

264 of 313 new or added lines in 12 files covered. (84.35%)

30 existing lines in 2 files now uncovered.

2494 of 2573 relevant lines covered (96.93%)

31115123.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.9
/include/bitlib/bit-containers/bit_array_base.hpp
1
// ================================= BIT_ARRAY_BASE =================================== //
2
// Project:     The Experimental Bit Algorithms Library
3
// \file        bit_array_base.hpp
4
// Description: Base implementation for bit_array variants
5
// Creator:     Vincent Reverdy
6
// Contributor: Peter McLean [2025]
7
// License:     BSD 3-Clause License
8
// ========================================================================== //
9
#ifndef _BIT_ARRAY_BASE_HPP_INCLUDED
10
#define _BIT_ARRAY_BASE_HPP_INCLUDED
11
// ========================================================================== //
12

13
// ================================ PREAMBLE ================================ //
14
// C++ standard library
15
#include <algorithm>
16
#include <bit>
17
#include <cmath>
18
#include <span>
19
#include <string>
20
#include <type_traits>
21
#include <vector>
22

23
// Project sources
24
#include "bitlib/bit-algorithms/bit_algorithm.hpp"
25
#include "bitlib/bit-containers/bit_bitsof.hpp"
26
#include "bitlib/bit-containers/bit_policy.hpp"
27
#include "bitlib/bit-iterator/bit.hpp"
28

29
namespace bit {
30

31
template <typename T, size_t N, typename W, typename Policy>
32
class array_ref;
33

34
template <typename T,
35
          std::size_t N,
36
          typename W,
37
          typename Policy>
38
class array;
39

40
/**
41
 * @brief Base class template for array implementations
42
 *
43
 * This is a CRTP (Curiously Recurring Template Pattern) base class that provides
44
 * common functionality for array variants.
45
 *
46
 * @tparam Derived The derived class (CRTP pattern)
47
 * @tparam T The value type (typically bit_value)
48
 * @tparam W The word type used for storage
49
 * @tparam N The size of the bit array, or std::dynamic_extent for dynamic size
50
 * @tparam Policy The policy for integral conversion (default is typical)
51
 * @tparam Iterators A struct that provides iterator and const_iterator types
52
 */
53
template <typename Derived, typename T, size_t N, typename W, bool resizable, typename Policy, typename Iterators>
54
class array_base : public detail::container_size_storage<std::size_t, resizable, N> {
55
 protected:
56
  constexpr Derived& derived() noexcept {
4,072✔
57
    return static_cast<Derived&>(*this);
4,072✔
58
  }
2,036✔
59

60
  constexpr const Derived& derived() const noexcept {
168✔
61
    return static_cast<const Derived&>(*this);
168✔
62
  }
84✔
63

64
 public:
65
  using detail::container_size_storage<std::size_t, resizable, N>::size;
66
  using word_type = W;
67
  using value_type = T;
68
  using size_type = std::size_t;
69
  using difference_type = std::ptrdiff_t;
70
  using reference = typename std::conditional<std::is_same_v<T, bit_value>, bit_reference<word_type&>, T&>::type;
71
  using const_reference = typename std::conditional<std::is_same_v<T, bit_value>, const bit_reference<const word_type&>, const T&>::type;
72
  using pointer = typename std::conditional<std::is_same_v<T, bit_value>, bit_pointer<word_type>, T&>::type;
73
  using const_pointer = const pointer;
74
  using iterator = Iterators::iterator;
75
  using const_iterator = Iterators::const_iterator;
76

77
  constexpr array_base() noexcept : detail::container_size_storage<std::size_t, resizable, N>() {}
432✔
78
  constexpr array_base(const size_type& extent) noexcept
123✔
79
    requires(N == std::dynamic_extent)
80
      : detail::container_size_storage<std::size_t, resizable, N>(extent) {}
246✔
81

82
  // Element access
83
  constexpr reference operator[](size_type pos) {
3,576✔
84
    return derived().begin()[pos];
5,364✔
85
  }
1,788✔
86

87
  constexpr const_reference operator[](size_type pos) const {
88
    return derived().begin()[pos];
89
  }
90

91
  constexpr reference at(size_type pos) {
26✔
92
    if (pos < size()) {
26!
93
      return derived().begin()[pos];
30✔
94
    } else {
10✔
95
      throw std::out_of_range("Position is out of range");
6✔
96
    }
3✔
97
  }
13✔
98

99
  constexpr const_reference at(size_type pos) const {
100
    if (pos < size()) {
101
      return derived().begin()[pos];
102
    } else {
103
      throw std::out_of_range("Position is out of range");
104
    }
105
  }
106

107
  constexpr reference front() {
6✔
108
    return derived().begin()[0];
9✔
109
  }
3✔
110

111
  constexpr const_reference front() const {
112
    return derived().begin()[0];
113
  }
114

115
  constexpr reference back() {
6✔
116
    return derived().begin()[size() - 1];
9✔
117
  }
3✔
118

119
  constexpr const_reference back() const {
120
    return derived().begin()[size() - 1];
121
  }
122

123
  constexpr iterator end() noexcept {
152✔
124
    return derived().begin() + size();
152✔
125
  }
76✔
126

127
  constexpr const_iterator end() const noexcept {
130✔
128
    return const_iterator(derived().begin()) + size();
130✔
129
  }
65✔
130

131
  constexpr const_iterator cbegin() const noexcept {
132
    return const_iterator(derived().begin());
133
  }
134

135
  constexpr const_iterator cend() const noexcept {
136
    return const_iterator(derived().end());
137
  }
138

139
  // Capacity
140
  constexpr bool empty() const noexcept {
8✔
141
    return 0 == size();
8✔
142
  }
4✔
143

144
  constexpr size_type max_size() const noexcept {
2✔
145
    return size();
2✔
146
  }
1✔
147

148
  // String representation
149
  constexpr std::string debug_string() const {
150
    return debug_string(derived().begin(), derived().end());
151
  }
152

153
  constexpr std::string debug_string(const_iterator first, const_iterator last) const {
154
    std::string ret = "";
155
    auto position = 0;
156
    for (auto it = first; it != last; ++it) {
157
      if (position % bitsof<word_type>() == 0 && position != 0) {
158
        ret += " ";
159
      } else if (position % 8 == 0 && position != 0) {
160
        ret += '.';
161
      }
162
      ret += *it == bit1 ? '1' : '0';
163
      ++position;
164
    }
165
    return ret;
166
  }
167

168
  /**
169
   * @brief Slice operations - returns a array_ref
170
   */
171
  constexpr auto operator()(size_type offset, size_type right) const noexcept {
172
    return array_ref<value_type, std::dynamic_extent, const word_type, Policy>(&this->at(offset), right - offset);
173
  }
174

175
  /**
176
   * @brief Slice operations - returns a array_ref
177
   */
178
  constexpr auto operator()(size_type offset, size_type right) noexcept {
16✔
179
    return array_ref<value_type, std::dynamic_extent, word_type, Policy>(&this->at(offset), right - offset);
16✔
180
  }
8✔
181

182
  // Common operations
183
  constexpr void fill(value_type bit_val) noexcept {
78✔
184
    std::fill(derived().begin(), derived().end(), bit_val);
78✔
185
  }
78✔
186

187
  /**
188
   * @brief Explicit conversion to integral types
189
   */
190
  template <std::integral U>
191
  explicit constexpr operator U() const noexcept {
16✔
192
    assert(size() <= bitsof<U>());
16!
193
    U integral;
16✔
194

195
    if constexpr (N == std::dynamic_extent) {
8✔
196
      if (size() > bitsof<U>()) {
4!
197
        Policy::truncation::template to_integral<U, N>(derived(), integral);
×
198
      } else {
2✔
199
        ::bit::copy(derived().begin(), end(), &integral);
4✔
200
      }
2✔
201
      if (size() < bitsof<U>()) {
4!
NEW
202
        Policy::extension::template to_integral<U, N>(detail::uninitialized, derived(), integral);
×
UNCOV
203
      }
204
    } else {
6✔
205
      if constexpr (N > bitsof<U>()) {
206
        Policy::truncation::template to_integral<U, N>(derived(), integral);
207
      } else {
6✔
208
        ::bit::copy(derived().begin(), end(), &integral);
12✔
209
      }
6✔
210
      if constexpr (N < bitsof<U>()) {
6✔
211
        Policy::extension::template to_integral<U, N>(detail::uninitialized, derived(), integral);
10✔
212
      }
5✔
213
    }
6✔
214

215
    return integral;
16✔
216
  }
8✔
217

218
  using compatible_bitarray = array<value_type, N, word_type, Policy>;
219

220
  constexpr compatible_bitarray operator~() {
4✔
221
    compatible_bitarray result(detail::uninitialized, size());
6✔
222
    transform(derived().begin(), derived().end(), result.begin(), [](const word_type& bits) -> word_type { return ~bits; });
8✔
223
    return result;
6✔
224
  }
2✔
225

226
  constexpr compatible_bitarray operator|(const bit_sized_range auto& other) const {
2✔
227
    assert(other.size() == size());
2!
228
    compatible_bitarray result(detail::uninitialized, size());
2✔
229
    transform(derived().begin(), derived().end(), other.begin(), result.begin(),
2✔
230
              [](const word_type& a, const word_type& b) -> word_type { return a | b; });
3✔
231
    return result;
3✔
232
  }
1✔
233
  constexpr Derived& operator|=(bit_sized_range auto& other) {
2✔
234
    assert(other.size() == size());
2!
235
    transform(derived().begin(), derived().end(), other.begin(), derived().begin(),
2✔
236
              [](const word_type& a, const word_type& b) -> word_type { return a | b; });
3✔
237
    return derived();
3✔
238
  }
1✔
239
  constexpr compatible_bitarray operator&(const bit_sized_range auto& other) const {
2✔
240
    assert(other.size() == size());
2!
241
    compatible_bitarray result(detail::uninitialized, size());
2✔
242
    transform(derived().begin(), derived().end(), other.begin(), result.begin(),
2✔
243
              [](const word_type& a, const word_type& b) -> word_type { return a & b; });
3✔
244
    return result;
3✔
245
  }
1✔
246
  constexpr Derived& operator&=(bit_sized_range auto& other) {
2✔
247
    assert(other.size() == size());
2!
248
    transform(derived().begin(), derived().end(), other.begin(), derived().begin(),
2✔
249
              [](const word_type& a, const word_type& b) -> word_type { return a & b; });
3✔
250
    return derived();
3✔
251
  }
1✔
252
  constexpr compatible_bitarray operator^(const bit_sized_range auto& other) const {
2✔
253
    assert(other.size() == size());
2!
254
    compatible_bitarray result(detail::uninitialized, size());
2✔
255
    transform(derived().begin(), derived().end(), other.begin(), result.begin(),
2✔
256
              [](const word_type& a, const word_type& b) -> word_type { return a ^ b; });
3✔
257
    return result;
3✔
258
  }
1✔
259
  constexpr Derived& operator^=(bit_sized_range auto& other) {
2✔
260
    assert(other.size() == size());
2!
261
    transform(derived().begin(), derived().end(), other.begin(), derived().begin(),
2✔
262
              [](const word_type& a, const word_type& b) -> word_type { return a ^ b; });
3✔
263
    return derived();
3✔
264
  }
1✔
265

266
 protected:
267
  template <typename U>
268
  constexpr void from_integral(const U& integral) {
124✔
269
    if constexpr (N == std::dynamic_extent) {
270
      if ((size() * bitsof<value_type>()) < bitsof<U>()) {
271
        Policy::truncation::template from_integral<U, N>(derived(), integral);
272
      } else {
273
        ::bit::copy(&integral, &integral + 1, derived().begin());
274
      }
275
      if (bitsof<U>() < (size() * bitsof<value_type>())) {
276
        Policy::extension::template from_integral<U, N>(detail::uninitialized, derived(), integral);
277
      }
278
    } else {
62✔
279
      if constexpr ((N * bitsof<value_type>()) < bitsof<U>()) {
62✔
280
        Policy::truncation::template from_integral<U, N>(derived(), integral);
76✔
281
      } else {
38✔
282
        ::bit::copy(&integral, &integral + 1, derived().begin());
48✔
283
      }
24✔
284
      if constexpr (bitsof<U>() < (N * bitsof<value_type>())) {
285
        Policy::extension::template from_integral<U, N>(detail::uninitialized, derived(), integral);
286
      }
287
    }
62✔
288
  }
124✔
289
};
290

291
constexpr bool operator==(const bit_sized_range auto& lhs, const bit_sized_range auto& rhs) {
80✔
292
  if (lhs.size() != rhs.size()) {
80!
293
    return false;
4✔
294
  }
2✔
295
  return ::bit::equal(lhs.begin(), lhs.end(), rhs.begin());
76✔
296
}
40✔
297

298
}  // namespace bit
299

300
#endif  // _BIT_ARRAY_BASE_HPP_INCLUDED
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc