GCC Code Coverage Report


Directory: src/oiseau/
File: src/oiseau/utils/jagged_array.hpp
Date: 2026-11-23 22:30:49
Exec Total Coverage
Lines: 169 170 99.4%
Functions: 61 61 100.0%
Branches: 117 198 59.1%

Line Branch Exec Source
1 // Copyright (C) 2026 Tiago V. L. Amorim (@tiagovla)
2 //
3 // This file is part of oiseau (https://github.com/tiagovla/oiseau)
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6
7 #pragma once
8
9 #include <algorithm> // For std::copy, std::move, std::next
10 #include <cstddef>
11 #include <initializer_list> // For std::initializer_list
12 #include <iostream> // For std::ostream and operator<<
13 #include <iterator> // For std::iterator related tags
14 #include <span> // For std::span
15 #include <stdexcept> // For std::out_of_range
16 #include <string> // For std::to_string in error messages
17 #include <type_traits>
18 #include <vector>
19
20 namespace oiseau::utils {
21
22 template <typename T>
23 class JaggedArray;
24
25 template <typename T>
26 std::ostream& operator<<(std::ostream& os, const JaggedArray<T>& arr);
27
28 template <typename T>
29 class JaggedArray {
30 private:
31 std::vector<T> m_data;
32 std::vector<std::size_t> m_row_offsets;
33
34 192 void check_row_bounds(std::size_t r_idx, const char* func_name) const {
35
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 92 times.
192 if (r_idx >= num_rows()) {
36
6/12
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 4 times.
✗ Branch 18 not taken.
48 throw std::out_of_range(std::string(func_name) + " - Row index (" + std::to_string(r_idx) +
37
3/6
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
32 ") out of bounds for " + std::to_string(num_rows()) + " rows.");
38 }
39 184 }
40
41 5 void check_row_insert_bounds(std::size_t r_idx, const char* func_name) const {
42
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
5 if (r_idx > num_rows()) {
43
4/8
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
4 throw std::out_of_range(std::string(func_name) + " - Row index for insertion (" +
44
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
4 std::to_string(r_idx) + ") out of bounds for " +
45
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
4 std::to_string(num_rows()) + " rows.");
46 }
47 4 }
48
49 template <typename Row>
50 60 static std::size_t get_total_elements_hint(std::initializer_list<Row> rows) {
51 60 return std::ranges::fold_left(
52 140 rows, std::size_t{0}, [](std::size_t total, const Row& row) { return total + row.size(); });
53 }
54
55 public:
56
1/2
✓ Branch 3 taken 23 times.
✗ Branch 4 not taken.
46 JaggedArray() { m_row_offsets.push_back(0); }
57
58
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 explicit JaggedArray(std::size_t num_r) : m_row_offsets(num_r + 1, 0) {}
59
60 58 JaggedArray(std::initializer_list<std::initializer_list<T>> il) {
61
1/2
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
58 m_row_offsets.push_back(0);
62 58 std::size_t current_offset = 0;
63
2/4
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
58 m_data.reserve(get_total_elements_hint(il));
64
2/2
✓ Branch 2 taken 77 times.
✓ Branch 3 taken 29 times.
212 for (const auto& inner_list : il) {
65
1/2
✓ Branch 4 taken 77 times.
✗ Branch 5 not taken.
308 m_data.insert(m_data.end(), inner_list.begin(), inner_list.end());
66 154 current_offset += inner_list.size();
67
1/2
✓ Branch 1 taken 77 times.
✗ Branch 2 not taken.
154 m_row_offsets.push_back(current_offset);
68 }
69 58 }
70
71 1 JaggedArray(std::initializer_list<std::vector<T>> il) {
72
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 m_row_offsets.push_back(0);
73 1 std::size_t current_offset = 0;
74
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 m_data.reserve(get_total_elements_hint(il));
75
2/2
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
4 for (const auto& row_vec : il) {
76
1/2
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 m_data.insert(m_data.end(), row_vec.begin(), row_vec.end());
77 3 current_offset += row_vec.size();
78
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 m_row_offsets.push_back(current_offset);
79 }
80 1 }
81
82
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 JaggedArray(const JaggedArray& other) = default;
83 1 JaggedArray(JaggedArray&& other) noexcept = default;
84 2 JaggedArray& operator=(const JaggedArray& other) = default;
85 1 JaggedArray& operator=(JaggedArray&& other) noexcept = default;
86
87 368 std::size_t num_rows() const noexcept {
88
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 182 times.
368 if (m_row_offsets.empty()) return 0;
89 364 return m_row_offsets.size() - 1;
90 }
91
92 52 std::size_t num_cols(std::size_t r_idx) const {
93 52 check_row_bounds(r_idx, "num_cols");
94 50 return m_row_offsets[r_idx + 1] - m_row_offsets[r_idx];
95 }
96
97 32 std::size_t total_elements() const noexcept { return m_data.size(); }
98
99 5 std::span<T> operator[](std::size_t r_idx) {
100
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
5 if (r_idx >= num_rows()) {
101
4/8
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
2 throw std::out_of_range("JaggedArray::operator[] - Row index (" + std::to_string(r_idx) +
102 ") out of bounds.");
103 }
104 3 std::size_t start_offset = m_row_offsets[r_idx];
105 3 std::size_t length = m_row_offsets[r_idx + 1] - start_offset;
106 3 return std::span<T>(m_data.data() + start_offset, length);
107 }
108
109 10 std::span<const T> operator[](std::size_t r_idx) const {
110
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
10 if (r_idx >= num_rows()) {
111
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
8 throw std::out_of_range("JaggedArray::operator[] const - Row index (" +
112
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
8 std::to_string(r_idx) + ") out of bounds.");
113 }
114 8 std::size_t start_offset = m_row_offsets[r_idx];
115 8 std::size_t length = m_row_offsets[r_idx + 1] - start_offset;
116 8 return std::span<const T>(m_data.data() + start_offset, length);
117 }
118
119 110 T& at(std::size_t r_idx, std::size_t c_idx) {
120 110 check_row_bounds(r_idx, "at (row)");
121 108 std::size_t start_offset = m_row_offsets[r_idx];
122 108 std::size_t row_len = m_row_offsets[r_idx + 1] - start_offset;
123
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 53 times.
108 if (c_idx >= row_len) {
124
4/8
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
8 throw std::out_of_range("JaggedArray::at - Column index (" + std::to_string(c_idx) +
125
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
8 ") out of bounds for row " + std::to_string(r_idx) + " with length " +
126
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
8 std::to_string(row_len) + ".");
127 }
128 106 return m_data[start_offset + c_idx];
129 }
130
131 2 const T& at(std::size_t r_idx, std::size_t c_idx) const {
132 2 check_row_bounds(r_idx, "at (row)");
133 2 std::size_t start_offset = m_row_offsets[r_idx];
134 2 std::size_t row_len = m_row_offsets[r_idx + 1] - start_offset;
135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (c_idx >= row_len) {
136
4/8
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
4 throw std::out_of_range("JaggedArray::at - Column index (" + std::to_string(c_idx) +
137
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
4 ") out of bounds for row " + std::to_string(r_idx) + " with length " +
138
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
4 std::to_string(row_len) + ".");
139 }
140 1 return m_data[start_offset + c_idx];
141 }
142
143 5 void add_row(std::initializer_list<T> il) {
144
1/2
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
10 m_data.insert(m_data.end(), il.begin(), il.end());
145
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 m_row_offsets.push_back(m_data.size());
146 5 }
147
148 1 void add_row(const std::vector<T>& new_row_data) {
149
1/2
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 m_data.insert(m_data.end(), new_row_data.begin(), new_row_data.end());
150
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 m_row_offsets.push_back(m_data.size());
151 1 }
152
153 1 void add_row(std::vector<T>&& new_row_data) {
154
1/2
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
2 m_data.insert(m_data.end(), std::make_move_iterator(new_row_data.begin()),
155 std::make_move_iterator(new_row_data.end()));
156
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 m_row_offsets.push_back(m_data.size());
157 1 new_row_data.clear();
158 1 }
159
160 5 void insert_row(std::size_t r_idx, std::initializer_list<T> il) {
161 5 check_row_insert_bounds(r_idx, "insert_row");
162
163 4 std::size_t insert_data_pos = m_row_offsets[r_idx];
164 4 std::size_t new_row_len = il.size();
165
166
1/2
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
12 m_data.insert(m_data.begin() + insert_data_pos, il.begin(), il.end());
167
1/2
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
16 m_row_offsets.insert(m_row_offsets.begin() + r_idx + 1, m_row_offsets[r_idx] + new_row_len);
168
169
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 4 times.
10 for (std::size_t i = r_idx + 2; i < m_row_offsets.size(); ++i) {
170 6 m_row_offsets[i] += new_row_len;
171 }
172 4 }
173
174 void insert_row(std::size_t r_idx, const std::vector<T>& new_row_data) {
175 check_row_insert_bounds(r_idx, "insert_row");
176
177 std::size_t insert_data_pos = m_row_offsets[r_idx];
178 std::size_t new_row_len = new_row_data.size();
179
180 m_data.insert(m_data.begin() + insert_data_pos, new_row_data.begin(), new_row_data.end());
181 m_row_offsets.insert(m_row_offsets.begin() + r_idx + 1, m_row_offsets[r_idx] + new_row_len);
182 for (std::size_t i = r_idx + 2; i < m_row_offsets.size(); ++i) {
183 m_row_offsets[i] += new_row_len;
184 }
185 }
186
187 6 void remove_row(std::size_t r_idx) {
188 6 check_row_bounds(r_idx, "remove_row");
189
190 5 std::size_t start_data_pos = m_row_offsets[r_idx];
191 5 std::size_t end_data_pos = m_row_offsets[r_idx + 1];
192 5 std::size_t removed_len = end_data_pos - start_data_pos;
193
194
1/2
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
25 m_data.erase(m_data.begin() + start_data_pos, m_data.begin() + end_data_pos);
195
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
20 m_row_offsets.erase(m_row_offsets.begin() + r_idx + 1);
196
197
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5 times.
9 for (std::size_t i = r_idx + 1; i < m_row_offsets.size(); ++i) {
198 4 m_row_offsets[i] -= removed_len;
199 }
200 5 }
201
202 void add_element(std::size_t r_idx, const T& value) {
203 check_row_bounds(r_idx, "add_element");
204
205 std::size_t insert_data_pos = m_row_offsets[r_idx + 1];
206 m_data.insert(m_data.begin() + insert_data_pos, value);
207
208 for (std::size_t i = r_idx + 1; i < m_row_offsets.size(); ++i) {
209 m_row_offsets[i] += 1;
210 }
211 }
212
213 7 void add_element(std::size_t r_idx, T&& value) {
214 7 check_row_bounds(r_idx, "add_element");
215
216 6 std::size_t insert_data_pos = m_row_offsets[r_idx + 1];
217
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
24 m_data.insert(m_data.begin() + insert_data_pos, std::move(value));
218
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 6 times.
17 for (std::size_t i = r_idx + 1; i < m_row_offsets.size(); ++i) {
219 11 m_row_offsets[i] += 1;
220 }
221 6 }
222
223 3 void clear() noexcept {
224 3 m_data.clear();
225 3 m_row_offsets.assign(1, 0);
226 3 }
227
228 26 bool is_empty() const noexcept { return num_rows() == 0; }
229
230 template <bool IsConstIter>
231 class RowIterator {
232 public:
233 using iterator_category = std::random_access_iterator_tag;
234 using difference_type = std::ptrdiff_t;
235 using value_type = std::conditional_t<IsConstIter, std::span<const T>, std::span<T>>;
236 using pointer = value_type*;
237 using reference = value_type;
238
239 using ParentArrayPtr = std::conditional_t<IsConstIter, const JaggedArray<T>*, JaggedArray<T>*>;
240
241 private:
242 ParentArrayPtr m_parent_array;
243 std::size_t m_current_row_idx;
244
245 public:
246 34 RowIterator(ParentArrayPtr parent, std::size_t r_idx)
247 34 : m_parent_array(parent), m_current_row_idx(r_idx) {}
248
249 26 reference operator*() const {
250
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 11 times.
26 if (m_current_row_idx == m_parent_array->num_rows()) {
251 if constexpr (IsConstIter) {
252 ✗ return std::span<const T>();
253 } else {
254 4 return std::span<T>();
255 }
256 }
257 // CORRECTED: Accessing members without trailing underscore
258 22 std::size_t start_offset = m_parent_array->m_row_offsets[m_current_row_idx];
259 22 std::size_t length = m_parent_array->m_row_offsets[m_current_row_idx + 1] - start_offset;
260
261 if constexpr (IsConstIter) {
262 6 return std::span<const T>(m_parent_array->m_data.data() + start_offset, length);
263 } else {
264 16 return std::span<T>(m_parent_array->m_data.data() + start_offset, length);
265 }
266 }
267
268 14 RowIterator& operator++() {
269 14 ++m_current_row_idx;
270 14 return *this;
271 }
272 RowIterator operator++(int) {
273 RowIterator tmp = *this;
274 ++(*this);
275 return tmp;
276 }
277 1 RowIterator& operator--() {
278 1 --m_current_row_idx;
279 1 return *this;
280 }
281 RowIterator operator--(int) {
282 RowIterator tmp = *this;
283 --(*this);
284 return tmp;
285 }
286
287 2 RowIterator& operator+=(difference_type n) {
288 2 m_current_row_idx += n;
289 2 return *this;
290 }
291 1 RowIterator operator+(difference_type n) const {
292 1 RowIterator temp = *this;
293 1 temp += n;
294 1 return temp;
295 }
296 friend RowIterator operator+(difference_type n, const RowIterator& iter) { return iter + n; }
297
298 RowIterator& operator-=(difference_type n) {
299 m_current_row_idx -= n;
300 return *this;
301 }
302 RowIterator operator-(difference_type n) const {
303 RowIterator temp = *this;
304 temp -= n;
305 return temp;
306 }
307 1 difference_type operator-(const RowIterator& other) const {
308 1 return static_cast<difference_type>(m_current_row_idx) -
309 1 static_cast<difference_type>(other.m_current_row_idx);
310 }
311
312 reference operator[](difference_type n) const { return *(*this + n); }
313
314 30 bool operator==(const RowIterator& other) const {
315
3/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 9 times.
30 return m_parent_array == other.m_parent_array && m_current_row_idx == other.m_current_row_idx;
316 }
317 24 bool operator!=(const RowIterator& other) const { return !(*this == other); }
318 bool operator<(const RowIterator& other) const {
319 return m_current_row_idx < other.m_current_row_idx;
320 }
321 bool operator>(const RowIterator& other) const { return other < *this; }
322 bool operator<=(const RowIterator& other) const { return !(other < *this); }
323 bool operator>=(const RowIterator& other) const { return !(*this < other); }
324 };
325
326 using iterator = RowIterator<false>;
327 using const_iterator = RowIterator<true>;
328
329 7 iterator begin() { return iterator(this, 0); }
330 6 iterator end() { return iterator(this, num_rows()); }
331
332 1 const_iterator begin() const { return const_iterator(this, 0); }
333 1 const_iterator end() const { return const_iterator(this, num_rows()); }
334 1 const_iterator cbegin() const { return const_iterator(this, 0); }
335 1 const_iterator cend() const { return const_iterator(this, num_rows()); }
336
337 friend std::ostream& operator<< <>(std::ostream& os, const JaggedArray<T>& arr);
338 };
339
340 template <typename T>
341 4 std::ostream& operator<<(std::ostream& os, const JaggedArray<T>& arr) {
342 4 os << "JaggedArray (" << arr.num_rows() << " rows, " << arr.total_elements()
343 4 << " total elements):\n";
344
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
4 if (arr.is_empty()) {
345 2 os << " (empty)\n";
346 2 return os;
347 }
348
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
8 for (std::size_t i = 0; i < arr.num_rows(); ++i) {
349
6/12
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 3 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 3 times.
✗ Branch 17 not taken.
6 os << " [" << i << "] (" << arr.num_cols(i) << " elements): {";
350
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 std::span<const T> current_row_span = arr[i];
351
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3 times.
16 for (std::size_t j = 0; j < current_row_span.size(); ++j) {
352
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
10 os << current_row_span[j];
353
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
10 if (j < current_row_span.size() - 1) {
354
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 os << ", ";
355 }
356 }
357
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 os << "}\n";
358 }
359 2 return os;
360 }
361
362 } // namespace oiseau::utils
363