• 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

92.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

27
namespace bit {
28

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

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

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

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

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

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

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

96
  /**
97
   * @brief Assignment operator - copies content but doesn't rebind
98
   */
99
  constexpr bit_array_ref& operator=(const bit_array_ref& other) {
1✔
100
    if (this != &other) {
1✔
101
      if (m_size != other.m_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
    }
106
    return *this;
2✔
107
  }
108

109
  /**
110
   * @brief Move assignment operator - copies content but doesn't rebind
111
   */
112
  constexpr bit_array_ref& operator=(bit_array_ref&& other) {
113
    if (this != &other) {
114
      if (m_size != other.m_size) {
115
        throw std::invalid_argument("Cannot assign from bit_array_ref of different size");
116
      }
117
      ::bit::copy(other.begin(), other.end(), this->begin());
118
    }
119
    return *this;
120
  }
121

122
  /**
123
   * @brief No destructor needed as we don't own the memory
124
   */
125
  ~bit_array_ref() = default;
126

127
  /*
128
   * Iterators
129
   */
130
  constexpr iterator begin() noexcept {
114✔
131
    return iterator(m_storage);
114✔
132
  }
133

134
  constexpr const_iterator begin() const noexcept {
12✔
135
    return const_iterator(m_storage);
12✔
136
  }
137

138
  /*
139
   * Capacity
140
   */
141
  constexpr size_type size() const noexcept {
90✔
142
    return m_size;
90✔
143
  }
144

145
  /*
146
   * Operations
147
   */
148
  constexpr void swap(bit_array_ref& other) noexcept {
1✔
149
    if (m_size != other.m_size) {
1✔
NEW
150
      throw std::invalid_argument("Cannot swap bit_array_ref of different sizes");
×
151
    }
152
    swap_ranges(begin(), end(), other.begin());
1✔
153
  }
1✔
154
};
155

156
static_assert(bit_range<bit_array_ref<>>, "bit_array_ref<> does not satisfy bit_range concept!");
157
static_assert(bit_sized_range<bit_array_ref<>>, "bit_array_ref<> does not satisfy bit_sized_range concept!");
158
#ifdef CONTIGUOUS_RANGE
159
static_assert(bit_contiguous_range<bit_array_ref<>>, "bit_array_ref<> does not satisfy bit_contiguous_range concept!");
160
static_assert(bit_contiguous_sized_range<bit_array_ref<>>, "bit_array_ref<> does not satisfy bit_contiguous_sized_range concept!");
161
#endif
162

163
// ========================================================================== //
164
}  // namespace bit
165

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