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

PeterCDMcLean / BitLib / 14950244216

10 May 2025 11:50PM UTC coverage: 96.875% (-0.2%) from 97.036%
14950244216

Pull #8

github

web-flow
Merge ac41f7e7e into a837637fd
Pull Request #8: Common bit array base

161 of 169 new or added lines in 5 files covered. (95.27%)

2 existing lines in 2 files now uncovered.

1302 of 1344 relevant lines covered (96.88%)

16284812.23 hits per line

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

97.37
/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 <cstring>  // memcpy
19
#include <span>
20
#include <string>
21
#include <type_traits>
22
#include <vector>
23

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

30
namespace bit {
31

32
template <typename T, typename W>
33
class bit_array_ref;
34
// ========================================================================== //
35

36
/**
37
 * @brief Base class template for bit_array implementations
38
 *
39
 * This is a CRTP (Curiously Recurring Template Pattern) base class that provides
40
 * common functionality for bit_array variants.
41
 *
42
 * @tparam Derived The derived class (CRTP pattern)
43
 * @tparam T The value type (typically bit_value)
44
 * @tparam W The word type used for storage
45
 * @tparam It The iterator type for the derived class
46
 * @tparam CIt The const_iterator type for the derived class
47
 */
48
template <typename Derived, typename T, typename W, typename It, typename CIt>
49
class bit_array_base {
50
 public:
51
  using word_type = W;
52
  using value_type = T;
53
  using size_type = std::size_t;
54
  using difference_type = std::ptrdiff_t;
55
  using reference = typename std::conditional<std::is_same_v<T, bit_value>, bit_reference<word_type>, T&>::type;
56
  using const_reference = typename std::conditional<std::is_same_v<T, bit_value>, const bit_reference<const word_type>, const T&>::type;
57
  using pointer = typename std::conditional<std::is_same_v<T, bit_value>, bit_pointer<word_type>, T&>::type;
58
  using const_pointer = const pointer;
59
  using iterator = It;
60
  using const_iterator = CIt;
61

62
  // Element access
63
  constexpr reference operator[](size_type pos) {
918✔
64
    return derived().begin()[pos];
1,834✔
65
  }
66

67
  constexpr const_reference operator[](size_type pos) const {
68
    return derived().begin()[pos];
69
  }
70

71
  constexpr reference at(size_type pos) {
7✔
72
    if (pos < derived().size()) {
7✔
73
      return derived().begin()[pos];
12✔
74
    } else {
75
      throw std::out_of_range("Position is out of range");
1✔
76
    }
77
  }
78

79
  constexpr const_reference at(size_type pos) const {
80
    if (pos < derived().size()) {
81
      return derived().begin()[pos];
82
    } else {
83
      throw std::out_of_range("Position is out of range");
84
    }
85
  }
86

87
  constexpr reference front() {
3✔
88
    return derived().begin()[0];
6✔
89
  }
90

91
  constexpr const_reference front() const {
92
    return derived().begin()[0];
93
  }
94

95
  constexpr reference back() {
3✔
96
    return derived().begin()[derived().size() - 1];
6✔
97
  }
98

99
  constexpr const_reference back() const {
100
    return derived().begin()[derived().size() - 1];
101
  }
102

103
  constexpr iterator end() noexcept {
52✔
104
    return derived().begin() + derived().size();
52✔
105
  }
106

107
  constexpr const_iterator end() const noexcept {
17✔
108
    return const_iterator(derived().begin()) + derived().size();
17✔
109
  }
110

111
  constexpr const_iterator cbegin() const noexcept {
112
    return const_iterator(derived().begin());
113
  }
114

115
  constexpr const_iterator cend() const noexcept {
116
    return const_iterator(derived().end());
117
  }
118

119
  // Capacity
120
  constexpr bool empty() const noexcept {
4✔
121
    return 0 == derived().size();
4✔
122
  }
123

124
  constexpr size_type max_size() const noexcept {
1✔
125
    return derived().size();
1✔
126
  }
127

128
  // String representation
129
  constexpr std::string debug_string() const {
130
    return debug_string(derived().begin(), derived().end());
131
  }
132

133
  constexpr std::string debug_string(const_iterator first, const_iterator last) const {
134
    std::string ret = "";
135
    auto position = 0;
136
    for (auto it = first; it != last; ++it) {
137
      if (position % bitsof<word_type>() == 0 && position != 0) {
138
        ret += " ";
139
      } else if (position % 8 == 0 && position != 0) {
140
        ret += '.';
141
      }
142
      ret += *it == bit1 ? '1' : '0';
143
      ++position;
144
    }
145
    return ret;
146
  }
147

148
  // Comparison
149
  constexpr bool operator==(const Derived& other) const noexcept {
10✔
150
    if (derived().size() != other.size()) {
10✔
NEW
151
      return false;
×
152
    }
153
    return equal(derived().begin(), derived().end(), other.begin());
10✔
154
  }
155

156
  /**
157
   * @brief Slice operations - returns a bit_array_ref
158
   */
159
  constexpr auto operator()(size_type offset, size_type right) const noexcept {
160
    return bit_array_ref<bit_value, const word_type>(&this->at(offset), right - offset);
161
  }
162

163
  /**
164
   * @brief Slice operations - returns a bit_array_ref
165
   */
166
  constexpr auto operator()(size_type offset, size_type right) noexcept {
4✔
167
    return bit_array_ref<bit_value, word_type>(&this->at(offset), right - offset);
4✔
168
  }
169

170
  // Common operations
171
  constexpr void fill(value_type bit_val) noexcept {
33✔
172
    std::fill(derived().begin(), derived().end(), bit_val);
33✔
173
  }
33✔
174

175
  /**
176
   * @brief Explicit conversion to integral types
177
   */
178
  template <std::integral U>
179
  explicit constexpr operator U() const noexcept {
2✔
180
    assert(derived().size() <= bitsof<U>());
2✔
181
    U integral;
2✔
182
    bit_span<uint8_t, bitsof<U>()> integral_ref(reinterpret_cast<uint8_t*>(&integral));
2✔
183
    copy(derived().begin(), derived().begin() + bitsof<U>(), integral_ref.begin());
2✔
184
    if constexpr (std::is_signed_v<U>) {
185
      ::bit::fill(integral_ref.begin() + derived().size(), integral_ref.end(), integral_ref[bitsof<U>() - 1]);
186
    } else {
187
      ::bit::fill(integral_ref.begin() + derived().size(), integral_ref.end(), bit0);
2✔
188
    }
189
    return integral;
2✔
190
  }
191

192
 protected:
193
  constexpr Derived& derived() noexcept {
1,110✔
194
    return static_cast<Derived&>(*this);
1,110✔
195
  }
196

197
  constexpr const Derived& derived() const noexcept {
77✔
198
    return static_cast<const Derived&>(*this);
77✔
199
  }
200
};
201

202
}  // namespace bit
203

204
#endif  // _BIT_ARRAY_BASE_HPP_INCLUDED
205
        // ========================================================================== //
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