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

PeterCDMcLean / BitLib / 15863679023

24 Jun 2025 11:32PM UTC coverage: 96.199% (+42.1%) from 54.11%
15863679023

Pull #19

github

web-flow
Merge a9979881d into e50fa034f
Pull Request #19: Make bit::bit_array an alias of bit::array

13 of 13 new or added lines in 6 files covered. (100.0%)

10 existing lines in 4 files now uncovered.

1569 of 1631 relevant lines covered (96.2%)

20701395.67 hits per line

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

89.81
/include/bitlib/bit-containers/bit_array_dynamic_extent.hpp
1
// ================================= BIT_ARRAY =================================== //
2
// Project:     The Experimental Bit Algorithms Library
3
// \file        bit_array_dynamic_extent.hpp
4
// Description: Implementation of bit_array
5
// Creator:     Vincent Reverdy
6
// Contributor: Peter McLean [2025]
7
// License:     BSD 3-Clause License
8
// ========================================================================== //
9
#ifndef _BIT_ARRAY_DYNAMIC_EXTENT_HPP_INCLUDED
10
#define _BIT_ARRAY_DYNAMIC_EXTENT_HPP_INCLUDED
11

12
#include <algorithm>
13
#include <cstddef>
14
#include <initializer_list>
15
#include <memory>
16
#include <new>
17
#include <span>  // std::dynamic_extent
18
#include <stdexcept>
19

20
#include "bitlib/bit-algorithms/bit_algorithm.hpp"
21
#include "bitlib/bit-containers/bit_array.hpp"
22
#include "bitlib/bit-containers/bit_array_base.hpp"
23
#include "bitlib/bit-containers/bit_bitsof.hpp"
24
#include "bitlib/bit-containers/bit_policy.hpp"
25
#include "bitlib/bit-containers/bit_span.hpp"
26
#include "bitlib/bit-iterator/bit.hpp"
27
#include "bitlib/bit_concepts.hpp"
28

29
namespace bit {
30
namespace detail {
31

32
template <typename value_type, typename word_type>
33
struct array_dextent_iterator_types {
34
  using iterator = typename std::conditional<std::is_same_v<value_type, bit_value>,
35
                                             bit_iterator<word_type*>,
36
                                             word_type*>::type;
37
  using const_iterator = typename std::conditional<std::is_same_v<value_type, bit_value>,
38
                                                   bit_iterator<const word_type*>,
39
                                                   const word_type*>::type;
40
};
41
}  // namespace detail
42
template <typename T, typename W, typename Policy>
43
class array<T, std::dynamic_extent, W, Policy>
44
    : public array_base<array<T, std::dynamic_extent, W>, T, std::dynamic_extent, W, Policy, detail::array_dextent_iterator_types<T, W>> {
45
 public:
46
  using base = array_base<array<T, std::dynamic_extent, W>, T, std::dynamic_extent, W, Policy, detail::array_dextent_iterator_types<T, W>>;
47
  using base::end;
48
  using typename base::const_iterator;
49
  using typename base::const_pointer;
50
  using typename base::const_reference;
51
  using typename base::difference_type;
52
  using typename base::iterator;
53
  using typename base::pointer;
54
  using typename base::reference;
55
  using typename base::size_type;
56
  using typename base::value_type;
57
  using typename base::word_type;
58
  using Allocator = typename Policy::allocator;
59

60
 private:
61
  using word_type_ptr = word_type*;
62
  static const size_type FixedWords = sizeof(word_type_ptr) / sizeof(word_type);
63
  static const size_type FixedBits = FixedWords * bitsof<word_type>();
64

65
  const size_type m_size;
66

67
  struct Storage {
68
    union {
69
      word_type_ptr pointer;
70
      word_type fixed[FixedWords];
71
    };
72
    Allocator m_allocator;
73

74
    Storage(size_type words, const Allocator& allocator, detail::uninitialized_t) : m_allocator(allocator) {
74✔
75
      if (words > FixedWords) {
37✔
76
        new (&pointer) word_type_ptr(m_allocator.allocate(words));
×
77
      }
78
    }
37✔
79

80
    Storage(size_type words, const Allocator& allocator = Allocator()) : m_allocator(allocator) {
24✔
81
      if (words > FixedWords) {
12✔
82
        new (&pointer) word_type_ptr(m_allocator.allocate(words));
8✔
83
        for (size_type i = 0; i < words; ++i) {
67,701,776✔
84
          new (pointer + i) word_type();
67,701,772✔
85
        }
86
      } else {
87
        for (size_type i = 0; i < words; ++i) {
16✔
88
          new (&fixed[i]) word_type();
8✔
89
        }
90
      }
91
    }
12✔
92

93
    Storage(size_type words, Storage&& other) {
8✔
94
      if (words > FixedWords) {
4✔
95
        new (&pointer) word_type_ptr(std::move(other.pointer));
×
UNCOV
96
        other.pointer = nullptr;  // Prevent double deletion
×
97
      } else {
98
        for (size_type i = 0; i < words; ++i) {
8✔
99
          new (&fixed[i]) word_type(std::move(other.fixed[i]));
4✔
100
        }
101
      }
102
    }
4✔
103

104
    Storage(size_type words, const Storage& other, const Allocator& allocator = Allocator())
4✔
105
        : m_allocator(allocator) {
2✔
106
      if (words > FixedWords) {
2✔
107
        new (&pointer) word_type_ptr(m_allocator.allocate(words));
×
108
        for (size_t i = 0; i < words; ++i) {
×
109
          new (pointer + i) word_type(other.pointer[i]);
×
110
        }
UNCOV
111
        std::copy_n(other.pointer, words, pointer);
×
112
      } else {
113
        for (size_type i = 0; i < words; ++i) {
4✔
114
          new (&fixed[i]) word_type(other.fixed[i]);
2✔
115
        }
116
      }
117
    }
2✔
118
    template <typename U>
119
    Storage(size_type words, const U& val, const Allocator& allocator = Allocator())
120
        : m_allocator(allocator) {
121
      if (words > FixedWords) {
122
        new (&pointer) word_type_ptr(m_allocator.allocate(words));
123
        for (size_t i = 0; i < words; ++i) {
124
          new (pointer + i) word_type(val);
125
        }
126
      } else {
127
        for (size_type i = 0; i < words; ++i) {
128
          new (&fixed[i]) word_type(val);
129
        }
130
      }
131
    }
132
    Storage() = delete;
133
    ~Storage() {};
165✔
134
  } storage;
135

136
  static constexpr size_type Words(size_type N) {
170✔
137
    return (N * bitsof<value_type>() + bitsof<word_type>() - 1) / bitsof<word_type>();
170✔
138
  };
139

140
 public:
141
  ~array() {
55✔
142
    if (size() > FixedBits) {
55✔
143
      storage.m_allocator.deallocate(storage.pointer, Words(size()));
4✔
144
    } else {
145
      for (size_type i = 0; i < Words(size()); ++i) {
103✔
146
        storage.fixed[i].~word_type();
52✔
147
      }
148
    }
149
  }
55✔
150

151
  /*
152
  * Constructors, copies and moves...
153
  */
154
  array() = delete;
155

156
  constexpr array(const size_type size, const Allocator& allocator = Allocator())
12✔
157
      : m_size(size), storage(Words(size), allocator) {
12✔
158
  }
12✔
159

160
  template <std::integral U>
161
  constexpr array(const size_type size, const U& integral, const Allocator& allocator = Allocator())
162
      : m_size(size), storage(Words(size), allocator, detail::uninitialized) {
163
    this->from_integral(integral);
164
  }
165

166
  constexpr array(const size_type size, const word_type val, const Allocator& allocator = Allocator())
167
      : m_size(size), storage(Words(size), val, allocator) {
168
  }
169

170
  constexpr array(const size_type size, const value_type bit_val, const Allocator& allocator = Allocator())
22✔
171
    requires(!std::is_same<value_type, word_type>::value)
172
      : m_size(size), storage(Words(size), allocator, detail::uninitialized) {
22✔
173
    this->fill(bit_val);
22✔
174
  }
22✔
175

176
  constexpr array(const array<T, std::dynamic_extent, W, Policy>& other)
2✔
177
      : m_size(other.size()), storage(Words(size()), other.storage) {
6✔
178
  }
2✔
179

180
  constexpr array(const array<T, std::dynamic_extent, W, Policy>& other, const Allocator& allocator)
181
      : m_size(other.size()), storage(Words(size()), other.storage, allocator) {
182
  }
183

184
  constexpr array(const bit_sized_range auto& other, const Allocator& allocator = Allocator())
185
      : array(other.size(), allocator, detail::uninitialized) {
186
    ::bit::copy(other.begin(), other.end(), this->begin());
187
  }
188

189
  constexpr array(array<T, std::dynamic_extent, W>&& other)
4✔
190
      : m_size(other.size()), storage(Words(size()), std::move(other.storage)) {
4✔
191
  }
4✔
192

193
  constexpr array(array<T, std::dynamic_extent, W>&& other, const Allocator& allocator)
194
      : m_size(other.size()), storage(Words(size()), std::move(other.storage), allocator) {
195
  }
196

197
  constexpr array(const std::initializer_list<value_type> init, const Allocator& allocator = Allocator())
12✔
198
    requires(!std::is_same_v<value_type, word_type>)
199
      : m_size(init.size()), storage(Words(size()), allocator, detail::uninitialized) {
12✔
200
    std::copy(init.begin(), init.end(), this->begin());
12✔
201
  }
12✔
202

203
#if 0
204
  No known conversion from bool to bit_value
205
  bit_value has explicit constructor from bool to bit_value so this doesnt work
206
  constexpr array<std::dynamic_extent,W>::array(const std::initializer_list<bool> init)
207
      : storage(std::make_unique<word_type[]>(Words(init.size()))),
208
        m_size(init.size()) {
209
    std::copy(init.begin(), init.end(), this->begin());
210
  }
211
#endif
212

213
  template <typename U>
214
  constexpr array(const std::initializer_list<U> init, const Allocator& allocator = Allocator())
1✔
215
      : m_size(bitsof<U>() * init.size()), storage(Words(size()), allocator, detail::uninitialized) {
1✔
216
    std::copy(init.begin(), init.end(), data());
1✔
217
  }
1✔
218

219
  constexpr array(const std::string_view s, const Allocator& allocator = Allocator())
2✔
220
    requires(std::is_same_v<value_type, bit_value>)
221
      : m_size(std::count(s.begin(), s.end(), '0') + std::count(s.begin(), s.end(), '1')), storage(Words(size()), allocator, detail::uninitialized) {
2✔
222
    size_type i = 0;
2✔
223
    for (char c : s) {
17✔
224
      if (c == '0') {
15✔
225
        begin()[i++] = bit0;
7✔
226
      } else if (c == '1') {
8✔
227
        begin()[i++] = bit1;
8✔
228
      }
229
    }
230
  }
2✔
231

232
  /*
233
   * Assignment
234
   */
235
  constexpr array<T, std::dynamic_extent, W>& operator=(const array<T, std::dynamic_extent, W>& other) {
3✔
236
    if (nullptr == data() || size() != other.size()) {
3✔
237
      throw std::invalid_argument("Cannot reassign array<std::dynamic_extent,V,W> size");
2✔
238
    }
239
    if (this == &other) [[unlikely]] {
1✔
240
      return *this;
×
241
    }
242
    std::copy(other.begin(), other.end(), this->begin());
1✔
243
    return *this;
1✔
244
  }
245

246
  constexpr array<T, std::dynamic_extent, W>& operator=(const bit_sized_range auto& other) {
2✔
247
    if (other.size() != this->size()) [[unlikely]] {
2✔
248
      throw std::invalid_argument("other bit_range contains an invalid number of bits for array.");
1✔
249
    }
250
    ::bit::copy(other.begin(), other.end(), this->begin());
1✔
251
    return *this;
2✔
252
  };
253

254
  constexpr array<T, std::dynamic_extent, W>& operator=(array<T, std::dynamic_extent, W>&& other) {
4✔
255
    if (nullptr == data() || size() != other.size()) {
4✔
256
      throw std::invalid_argument("Cannot reassign array<std::dynamic_extent,V,W> size");
1✔
257
    }
258
    array<T, std::dynamic_extent, W> temp(std::move(other));
3✔
259
    swap(temp);
3✔
260
    return *this;
6✔
261
  }
3✔
262

263
  /*
264
   * Element Access
265
   */
266
  constexpr word_type* data() noexcept {
18✔
267
    if (size() > FixedBits) {
18✔
UNCOV
268
      return storage.pointer;
×
269
    } else {
270
      return storage.fixed;
18✔
271
    }
272
  }
273

274
  constexpr const word_type* data() const noexcept {
275
    if (size() > FixedBits) {
276
      return storage.pointer;
277
    } else {
278
      return storage.fixed;
279
    }
280
  }
281

282
  /*
283
   * Iterators
284
   */
285
  constexpr iterator begin() noexcept {
851✔
286
    if (size() > FixedBits) {
851✔
287
      return iterator(storage.pointer);
421✔
288
    } else {
289
      return iterator(storage.fixed);
430✔
290
    }
291
  }
292

293
  constexpr const_iterator begin() const noexcept {
22✔
294
    if (size() > FixedBits) {
22✔
UNCOV
295
      return const_iterator(storage.pointer);
×
296
    } else {
297
      return const_iterator(storage.fixed);
22✔
298
    }
299
  }
300

301
  /*
302
   * Capacity
303
   */
304
  constexpr size_type size() const noexcept {
1,329✔
305
    return m_size;
1,329✔
306
  }
307

308
  /*
309
   * Operations
310
   */
311
  constexpr void swap(array<T, std::dynamic_extent, W>& other) noexcept {
4✔
312
    assert(size() == other.size());
4✔
313
    if (size() > FixedBits) {
4✔
UNCOV
314
      std::swap(this->storage.pointer, other.storage.pointer);
×
315
    } else {
316
      for (size_type i = 0; i < Words(size()); ++i) {
8✔
317
        std::swap(this->storage.fixed[i], other.storage.fixed[i]);
4✔
318
      }
319
    }
320
  }
4✔
321
};
322

323
static_assert(bit_range<array<>>, "array<> does not satisfy bit_contiguous_range concept!");
324
static_assert(bit_sized_range<array<>>, "array<> does not satisfy bit_contiguous_sized_range concept!");
325
#ifdef CONTIGUOUS_RANGE
326
static_assert(bit_contiguous_range<array<>>, "array<> does not satisfy bit_contiguous_range concept!");
327
static_assert(bit_contiguous_sized_range<array<>>, "array<> does not satisfy bit_contiguous_sized_range concept!");
328
#endif
329

330
// ========================================================================== //
331
}  // namespace bit
332

333
#endif  // _BIT_ARRAY_DYNAMIC_EXTENT_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