31 std::vector<T> m_data;
32 std::vector<std::size_t> m_row_offsets;
34 void check_row_bounds(std::size_t r_idx,
const char* func_name)
const {
35 if (r_idx >= num_rows()) {
36 throw std::out_of_range(std::string(func_name) +
" - Row index (" + std::to_string(r_idx) +
37 ") out of bounds for " + std::to_string(num_rows()) +
" rows.");
41 void check_row_insert_bounds(std::size_t r_idx,
const char* func_name)
const {
42 if (r_idx > num_rows()) {
43 throw std::out_of_range(std::string(func_name) +
" - Row index for insertion (" +
44 std::to_string(r_idx) +
") out of bounds for " +
45 std::to_string(num_rows()) +
" rows.");
49 template <
typename Row>
50 static std::size_t get_total_elements_hint(std::initializer_list<Row> rows) {
51 return std::ranges::fold_left(
52 rows, std::size_t{0}, [](std::size_t total,
const Row& row) {
return total + row.size(); });
56 JaggedArray() { m_row_offsets.push_back(0); }
58 explicit JaggedArray(std::size_t num_r) : m_row_offsets(num_r + 1, 0) {}
60 JaggedArray(std::initializer_list<std::initializer_list<T>> il) {
61 m_row_offsets.push_back(0);
62 std::size_t current_offset = 0;
63 m_data.reserve(get_total_elements_hint(il));
64 for (
const auto& inner_list : il) {
65 m_data.insert(m_data.end(), inner_list.begin(), inner_list.end());
66 current_offset += inner_list.size();
67 m_row_offsets.push_back(current_offset);
71 JaggedArray(std::initializer_list<std::vector<T>> il) {
72 m_row_offsets.push_back(0);
73 std::size_t current_offset = 0;
74 m_data.reserve(get_total_elements_hint(il));
75 for (
const auto& row_vec : il) {
76 m_data.insert(m_data.end(), row_vec.begin(), row_vec.end());
77 current_offset += row_vec.size();
78 m_row_offsets.push_back(current_offset);
82 JaggedArray(
const JaggedArray& other) =
default;
83 JaggedArray(JaggedArray&& other)
noexcept =
default;
84 JaggedArray& operator=(
const JaggedArray& other) =
default;
85 JaggedArray& operator=(JaggedArray&& other)
noexcept =
default;
87 std::size_t num_rows()
const noexcept {
88 if (m_row_offsets.empty())
return 0;
89 return m_row_offsets.size() - 1;
92 std::size_t num_cols(std::size_t r_idx)
const {
93 check_row_bounds(r_idx,
"num_cols");
94 return m_row_offsets[r_idx + 1] - m_row_offsets[r_idx];
97 std::size_t total_elements()
const noexcept {
return m_data.size(); }
99 std::span<T> operator[](std::size_t r_idx) {
100 if (r_idx >= num_rows()) {
101 throw std::out_of_range(
"JaggedArray::operator[] - Row index (" + std::to_string(r_idx) +
104 std::size_t start_offset = m_row_offsets[r_idx];
105 std::size_t length = m_row_offsets[r_idx + 1] - start_offset;
106 return std::span<T>(m_data.data() + start_offset, length);
109 std::span<const T> operator[](std::size_t r_idx)
const {
110 if (r_idx >= num_rows()) {
111 throw std::out_of_range(
"JaggedArray::operator[] const - Row index (" +
112 std::to_string(r_idx) +
") out of bounds.");
114 std::size_t start_offset = m_row_offsets[r_idx];
115 std::size_t length = m_row_offsets[r_idx + 1] - start_offset;
116 return std::span<const T>(m_data.data() + start_offset, length);
119 T& at(std::size_t r_idx, std::size_t c_idx) {
120 check_row_bounds(r_idx,
"at (row)");
121 std::size_t start_offset = m_row_offsets[r_idx];
122 std::size_t row_len = m_row_offsets[r_idx + 1] - start_offset;
123 if (c_idx >= row_len) {
124 throw std::out_of_range(
"JaggedArray::at - Column index (" + std::to_string(c_idx) +
125 ") out of bounds for row " + std::to_string(r_idx) +
" with length " +
126 std::to_string(row_len) +
".");
128 return m_data[start_offset + c_idx];
131 const T& at(std::size_t r_idx, std::size_t c_idx)
const {
132 check_row_bounds(r_idx,
"at (row)");
133 std::size_t start_offset = m_row_offsets[r_idx];
134 std::size_t row_len = m_row_offsets[r_idx + 1] - start_offset;
135 if (c_idx >= row_len) {
136 throw std::out_of_range(
"JaggedArray::at - Column index (" + std::to_string(c_idx) +
137 ") out of bounds for row " + std::to_string(r_idx) +
" with length " +
138 std::to_string(row_len) +
".");
140 return m_data[start_offset + c_idx];
143 void add_row(std::initializer_list<T> il) {
144 m_data.insert(m_data.end(), il.begin(), il.end());
145 m_row_offsets.push_back(m_data.size());
148 void add_row(
const std::vector<T>& new_row_data) {
149 m_data.insert(m_data.end(), new_row_data.begin(), new_row_data.end());
150 m_row_offsets.push_back(m_data.size());
153 void add_row(std::vector<T>&& new_row_data) {
154 m_data.insert(m_data.end(), std::make_move_iterator(new_row_data.begin()),
155 std::make_move_iterator(new_row_data.end()));
156 m_row_offsets.push_back(m_data.size());
157 new_row_data.clear();
160 void insert_row(std::size_t r_idx, std::initializer_list<T> il) {
161 check_row_insert_bounds(r_idx,
"insert_row");
163 std::size_t insert_data_pos = m_row_offsets[r_idx];
164 std::size_t new_row_len = il.size();
166 m_data.insert(m_data.begin() + insert_data_pos, il.begin(), il.end());
167 m_row_offsets.insert(m_row_offsets.begin() + r_idx + 1, m_row_offsets[r_idx] + new_row_len);
169 for (std::size_t i = r_idx + 2; i < m_row_offsets.size(); ++i) {
170 m_row_offsets[i] += new_row_len;
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");
177 std::size_t insert_data_pos = m_row_offsets[r_idx];
178 std::size_t new_row_len = new_row_data.size();
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;
187 void remove_row(std::size_t r_idx) {
188 check_row_bounds(r_idx,
"remove_row");
190 std::size_t start_data_pos = m_row_offsets[r_idx];
191 std::size_t end_data_pos = m_row_offsets[r_idx + 1];
192 std::size_t removed_len = end_data_pos - start_data_pos;
194 m_data.erase(m_data.begin() + start_data_pos, m_data.begin() + end_data_pos);
195 m_row_offsets.erase(m_row_offsets.begin() + r_idx + 1);
197 for (std::size_t i = r_idx + 1; i < m_row_offsets.size(); ++i) {
198 m_row_offsets[i] -= removed_len;
202 void add_element(std::size_t r_idx,
const T& value) {
203 check_row_bounds(r_idx,
"add_element");
205 std::size_t insert_data_pos = m_row_offsets[r_idx + 1];
206 m_data.insert(m_data.begin() + insert_data_pos, value);
208 for (std::size_t i = r_idx + 1; i < m_row_offsets.size(); ++i) {
209 m_row_offsets[i] += 1;
213 void add_element(std::size_t r_idx, T&& value) {
214 check_row_bounds(r_idx,
"add_element");
216 std::size_t insert_data_pos = m_row_offsets[r_idx + 1];
217 m_data.insert(m_data.begin() + insert_data_pos, std::move(value));
218 for (std::size_t i = r_idx + 1; i < m_row_offsets.size(); ++i) {
219 m_row_offsets[i] += 1;
223 void clear()
noexcept {
225 m_row_offsets.assign(1, 0);
228 bool is_empty()
const noexcept {
return num_rows() == 0; }
230 template <
bool IsConstIter>
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;
239 using ParentArrayPtr = std::conditional_t<IsConstIter, const JaggedArray<T>*, JaggedArray<T>*>;
242 ParentArrayPtr m_parent_array;
243 std::size_t m_current_row_idx;
246 RowIterator(ParentArrayPtr parent, std::size_t r_idx)
247 : m_parent_array(parent), m_current_row_idx(r_idx) {}
249 reference operator*()
const {
250 if (m_current_row_idx == m_parent_array->num_rows()) {
251 if constexpr (IsConstIter) {
252 return std::span<const T>();
254 return std::span<T>();
258 std::size_t start_offset = m_parent_array->m_row_offsets[m_current_row_idx];
259 std::size_t length = m_parent_array->m_row_offsets[m_current_row_idx + 1] - start_offset;
261 if constexpr (IsConstIter) {
262 return std::span<const T>(m_parent_array->m_data.data() + start_offset, length);
264 return std::span<T>(m_parent_array->m_data.data() + start_offset, length);
268 RowIterator& operator++() {
272 RowIterator operator++(
int) {
273 RowIterator tmp = *
this;
277 RowIterator& operator--() {
281 RowIterator operator--(
int) {
282 RowIterator tmp = *
this;
287 RowIterator& operator+=(difference_type n) {
288 m_current_row_idx += n;
291 RowIterator operator+(difference_type n)
const {
292 RowIterator temp = *
this;
296 friend RowIterator operator+(difference_type n,
const RowIterator& iter) {
return iter + n; }
298 RowIterator& operator-=(difference_type n) {
299 m_current_row_idx -= n;
302 RowIterator operator-(difference_type n)
const {
303 RowIterator temp = *
this;
307 difference_type operator-(
const RowIterator& other)
const {
308 return static_cast<difference_type
>(m_current_row_idx) -
309 static_cast<difference_type
>(other.m_current_row_idx);
312 reference operator[](difference_type n)
const {
return *(*
this + n); }
314 bool operator==(
const RowIterator& other)
const {
315 return m_parent_array == other.m_parent_array && m_current_row_idx == other.m_current_row_idx;
317 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;
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); }
329 iterator begin() {
return iterator(
this, 0); }
330 iterator end() {
return iterator(
this, num_rows()); }
332 const_iterator begin()
const {
return const_iterator(
this, 0); }
333 const_iterator end()
const {
return const_iterator(
this, num_rows()); }
334 const_iterator cbegin()
const {
return const_iterator(
this, 0); }
335 const_iterator cend()
const {
return const_iterator(
this, num_rows()); }
337 friend std::ostream& operator<< <>(std::ostream& os,
const JaggedArray<T>& arr);