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

PeterCDMcLean / BitLib / 15936703031

27 Jun 2025 09:56PM UTC coverage: 53.996% (-0.1%) from 54.11%
15936703031

Pull #20

github

web-flow
Merge c0c08e4f7 into d917039b0
Pull Request #20: Pipecleaning policy and casting issues

10137 of 18762 branches covered (54.03%)

Branch coverage included in aggregate %.

12 of 15 new or added lines in 6 files covered. (80.0%)

5 existing lines in 1 file now uncovered.

6058 of 11231 relevant lines covered (53.94%)

7733697.61 hits per line

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

85.06
/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, 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, typename Policy, typename Iterators>
54
class array_base {
55
 protected:
56
  constexpr Derived& derived() noexcept {
2,486✔
57
    return static_cast<Derived&>(*this);
2,486✔
58
  }
1,243✔
59

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

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

76
  // Element access
77
  constexpr reference operator[](size_type pos) {
1,892✔
78
    return derived().begin()[pos];
2,838✔
79
  }
946✔
80

81
  constexpr const_reference operator[](size_type pos) const {
82
    return derived().begin()[pos];
83
  }
84

85
  constexpr reference at(size_type pos) {
26✔
86
    if (pos < derived().size()) {
26!
87
      return derived().begin()[pos];
30✔
88
    } else {
10✔
89
      throw std::out_of_range("Position is out of range");
6✔
90
    }
3✔
91
  }
13✔
92

93
  constexpr const_reference at(size_type pos) const {
94
    if (pos < derived().size()) {
95
      return derived().begin()[pos];
96
    } else {
97
      throw std::out_of_range("Position is out of range");
98
    }
99
  }
100

101
  constexpr reference front() {
6✔
102
    return derived().begin()[0];
9✔
103
  }
3✔
104

105
  constexpr const_reference front() const {
106
    return derived().begin()[0];
107
  }
108

109
  constexpr reference back() {
6✔
110
    return derived().begin()[derived().size() - 1];
9✔
111
  }
3✔
112

113
  constexpr const_reference back() const {
114
    return derived().begin()[derived().size() - 1];
115
  }
116

117
  constexpr iterator end() noexcept {
128✔
118
    return derived().begin() + derived().size();
128✔
119
  }
64✔
120

121
  constexpr const_iterator end() const noexcept {
94✔
122
    return const_iterator(derived().begin()) + derived().size();
94✔
123
  }
47✔
124

125
  constexpr const_iterator cbegin() const noexcept {
126
    return const_iterator(derived().begin());
127
  }
128

129
  constexpr const_iterator cend() const noexcept {
130
    return const_iterator(derived().end());
131
  }
132

133
  // Capacity
134
  constexpr bool empty() const noexcept {
8✔
135
    return 0 == derived().size();
8✔
136
  }
4✔
137

138
  constexpr size_type max_size() const noexcept {
2✔
139
    return derived().size();
2✔
140
  }
1✔
141

142
  // String representation
143
  constexpr std::string debug_string() const {
144
    return debug_string(derived().begin(), derived().end());
145
  }
146

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

162
  /**
163
   * @brief Slice operations - returns a array_ref
164
   */
165
  constexpr auto operator()(size_type offset, size_type right) const noexcept {
166
    return array_ref<value_type, const word_type, Policy>(&this->at(offset), right - offset);
167
  }
168

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

176
  // Common operations
177
  constexpr void fill(value_type bit_val) noexcept {
76✔
178
    std::fill(derived().begin(), derived().end(), bit_val);
76✔
179
  }
76✔
180

181
  /**
182
   * @brief Explicit conversion to integral types
183
   */
184
  template <std::integral U>
185
  explicit constexpr operator U() const noexcept {
6✔
186
    assert(derived().size() <= bitsof<U>());
6!
187
    U integral;
6✔
188

189
    if constexpr (N == std::dynamic_extent) {
3✔
190
      if (derived().size() > bitsof<U>()) {
4!
191
        Policy::truncation::template to_integral<U, N>(derived(), integral);
×
192
      } else {
2✔
193
        ::bit::copy(derived().begin(), end(), bit_pointer<U>(&integral));
4✔
194
      }
2✔
195
      if (derived().size() < bitsof<U>()) {
4!
NEW
196
        Policy::extension::template to_integral<U, N>(derived(), integral, detail::uninitialized);
×
197
      }
198
    } else {
2✔
199
      if constexpr (N > bitsof<U>()) {
200
        Policy::truncation::template to_integral<U, N>(derived(), integral);
201
      } else {
1✔
202
        ::bit::copy(derived().begin(), end(), bit_pointer<U>(&integral));
2✔
203
      }
1✔
204
      if constexpr (N < bitsof<U>()) {
205
        Policy::extension::template to_integral<U, N>(derived(), integral, detail::uninitialized);
206
      }
207
    }
1✔
208

209
    return integral;
6✔
210
  }
3✔
211

212
  using compatible_bitarray = array<value_type, N, word_type, Policy>;
213

214
  constexpr compatible_bitarray operator~() {
4✔
215
    compatible_bitarray result(derived().size());
6✔
216
    transform(derived().begin(), derived().end(), result.begin(), [](const word_type& bits) -> word_type { return ~bits; });
8✔
217
    return result;
6✔
218
  }
2✔
219

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

260
 protected:
261
  template <typename U>
262
  constexpr void from_integral(const U& integral) {
80✔
263
    if constexpr (N == std::dynamic_extent) {
264
      if ((derived().size() * bitsof<value_type>()) < bitsof<U>()) {
265
        Policy::truncation::template from_integral<U, N>(derived(), integral);
266
      } else {
267
        bit_pointer<const U> integral_ptr(&integral);
268
        ::bit::copy(integral_ptr, integral_ptr + bitsof<U>(), derived().begin());
269
      }
270
      if (bitsof<U>() < (derived().size() * bitsof<value_type>())) {
271
        Policy::extension::template from_integral<U, N>(derived(), integral, detail::uninitialized);
272
      }
273
    } else {
40✔
274
      if constexpr ((N * bitsof<value_type>()) < bitsof<U>()) {
40✔
275
        Policy::truncation::template from_integral<U, N>(derived(), integral);
40✔
276
      } else {
20✔
277
        bit_pointer<const U> integral_ptr(&integral);
40✔
278
        ::bit::copy(integral_ptr, integral_ptr + bitsof<U>(), derived().begin());
40✔
279
      }
20✔
280
      if constexpr (bitsof<U>() < (N * bitsof<value_type>())) {
281
        Policy::extension::template from_integral<U, N>(derived(), integral, detail::uninitialized);
282
      }
283
    }
40✔
284
  }
80✔
285
};
286

287
constexpr bool operator==(const bit_sized_range auto& lhs, const bit_sized_range auto& rhs) {
56✔
288
  if (lhs.size() != rhs.size()) {
56!
289
    return false;
4✔
290
  }
2✔
291
  return ::bit::equal(lhs.begin(), lhs.end(), rhs.begin());
52✔
292
}
28✔
293

294
}  // namespace bit
295

296
#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