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

PeterCDMcLean / BitLib / 14955974867

11 May 2025 12:51PM UTC coverage: 95.522% (-1.5%) from 97.036%
14955974867

Pull #8

github

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

203 of 232 new or added lines in 6 files covered. (87.5%)

1 existing line in 1 file now uncovered.

1344 of 1407 relevant lines covered (95.52%)

15555644.28 hits per line

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

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

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

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

28
namespace bit {
29

30
/**
31
 * @brief A non-owning reference to a bit array
32
 *
33
 * Similar to bit_array_dynamic_extent but does not allocate or deallocate memory.
34
 * The pointer and size are const class members and cannot be re-bound.
35
 * Assignment operations always copy content and can't rebind the pointer/size.
36
 *
37
 * @tparam T The value type (typically bit_value)
38
 * @tparam W The word type used for storage
39
 */
40
template <typename T = bit_value, typename W = std::uint8_t>
41
class bit_array_ref
42
    : public bit_array_base<bit_array_ref<T, W>, T, W, bit_iterator<W*>, bit_iterator<const W*>> {
43
 public:
44
  using base = bit_array_base<bit_array_ref<T, W>, T, W, bit_iterator<W*>, bit_iterator<const W*>>;
45
  using base::end;
46
  using typename base::const_iterator;
47
  using typename base::const_pointer;
48
  using typename base::const_reference;
49
  using typename base::difference_type;
50
  using typename base::iterator;
51
  using typename base::pointer;
52
  using typename base::reference;
53
  using typename base::size_type;
54
  using typename base::value_type;
55
  using typename base::word_type;
56

57
 private:
58
  const bit_pointer<word_type> m_storage;
59
  const size_type m_size;
60

61
 public:
62
  // Constructors
63
  bit_array_ref() = delete;
64

65
  /**
66
   * @brief Constructs a non-owning reference to a bit array
67
   *
68
   * @param storage Pointer to the storage
69
   * @param size Number of bits
70
   */
71
  constexpr bit_array_ref(word_type* storage, size_type size)
7✔
72
      : m_storage(storage),
7✔
73
        m_size(size) {
7✔
74
  }
7✔
75

76
  /**
77
   * @brief Constructs a non-owning reference to a bit array using a bit_pointer
78
   *
79
   * @param storage bit_pointer to the storage
80
   * @param size Number of bits
81
   */
82
  constexpr bit_array_ref(bit_pointer<word_type> storage, size_type size)
8✔
83
      : m_storage(storage),
8✔
84
        m_size(size) {
8✔
85
  }
8✔
86

87
  /**
88
   * @brief Copy constructor
89
   */
90
  constexpr bit_array_ref(const bit_array_ref& other) = default;
91

92
  /**
93
   * @brief Move constructor
94
   */
95
  constexpr bit_array_ref(bit_array_ref&& other) = default;
96

97
  /**
98
   * @brief Assignment operator - copies content but doesn't rebind
99
   */
100
  constexpr bit_array_ref& operator=(const bit_range auto& other) {
1✔
101
    if (m_size != other.size()) {
1✔
NEW
102
      throw std::invalid_argument("Cannot assign from bit_array_ref of different size");
×
103
    }
104
    ::bit::copy(other.begin(), other.end(), this->begin());
1✔
105
    return *this;
2✔
106
  }
107

108
  constexpr bit_array_ref& operator=(const bit_array_ref& other) {
1✔
109
    if (this != &other) {
1✔
110
      if (m_size != other.m_size) {
1✔
NEW
111
        throw std::invalid_argument("Cannot assign from bit_array_ref of different size");
×
112
      }
113
      ::bit::copy(other.begin(), other.end(), this->begin());
1✔
114
    }
115
    return *this;
2✔
116
  }
117

118
  /**
119
   * @brief Move assignment operator - copies content but doesn't rebind
120
   */
121
  constexpr bit_array_ref& operator=(bit_array_ref&& other) {
122
    if (this != &other) {
123
      if (m_size != other.size()) {
124
        throw std::invalid_argument("Cannot assign from bit_array_ref of different size");
125
      }
126
      ::bit::copy(other.begin(), other.end(), this->begin());
127
    }
128
    return *this;
129
  }
130

131
  /**
132
   * @brief No destructor needed as we don't own the memory
133
   */
134
  ~bit_array_ref() = default;
135

136
  /*
137
   * Iterators
138
   */
139
  constexpr iterator begin() noexcept {
115✔
140
    return iterator(m_storage);
115✔
141
  }
142

143
  constexpr const_iterator begin() const noexcept {
12✔
144
    return const_iterator(m_storage);
12✔
145
  }
146

147
  /*
148
   * Capacity
149
   */
150
  constexpr size_type size() const noexcept {
90✔
151
    return m_size;
90✔
152
  }
153

154
  /*
155
   * Operations
156
   */
157
  constexpr void swap(bit_array_ref& other) noexcept {
1✔
158
    if (m_size != other.m_size) {
1✔
NEW
159
      throw std::invalid_argument("Cannot swap bit_array_ref of different sizes");
×
160
    }
161
    swap_ranges(begin(), end(), other.begin());
1✔
162
  }
1✔
163
};
164

165
static_assert(bit_range<bit_array_ref<>>, "bit_array_ref<> does not satisfy bit_range concept!");
166
static_assert(bit_sized_range<bit_array_ref<>>, "bit_array_ref<> does not satisfy bit_sized_range concept!");
167
#ifdef CONTIGUOUS_RANGE
168
static_assert(bit_contiguous_range<bit_array_ref<>>, "bit_array_ref<> does not satisfy bit_contiguous_range concept!");
169
static_assert(bit_contiguous_sized_range<bit_array_ref<>>, "bit_array_ref<> does not satisfy bit_contiguous_sized_range concept!");
170
#endif
171

172
// ========================================================================== //
173
}  // namespace bit
174

175
#endif  // _BIT_ARRAY_REF_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