Skip to content

Commit e486df1

Browse files
committed
resolved comments
1 parent 8bc5aad commit e486df1

4 files changed

Lines changed: 60 additions & 89 deletions

File tree

include/gl/types/flat_jagged_vector.hpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ class flat_jagged_vector {
103103
/// @brief Dereferences the iterator to the current segment.
104104
/// @return A subrange representing the segment at the current position
105105
[[nodiscard]] reference operator*() const noexcept {
106-
const auto beg = to_diff(*this->_offset_ptr);
107-
const auto end = to_diff(*(this->_offset_ptr + 1uz));
108-
return reference(this->_data_iter + beg, this->_data_iter + end);
106+
return reference(
107+
this->_data_iter + to_diff(*this->_offset_ptr),
108+
this->_data_iter + to_diff(*(this->_offset_ptr + 1uz))
109+
);
109110
}
110111

111112
/// @brief Random access to a segment at offset from current position.
@@ -477,9 +478,10 @@ class flat_jagged_vector {
477478
/// @warning No bounds checking is performed for performance. Use `at()` for bounds-checked access.
478479
/// Calling on an out-of-bounds index results in Undefined Behavior.
479480
[[nodiscard]] segment_type operator[](size_type i) {
480-
const auto beg = to_diff(this->_offsets[i]);
481-
const auto end = to_diff(this->_offsets[i + 1uz]);
482-
return segment_type(this->_data.begin() + beg, this->_data.begin() + end);
481+
return segment_type(
482+
this->_data.begin() + to_diff(this->_offsets[i]),
483+
this->_data.begin() + to_diff(this->_offsets[i + 1uz])
484+
);
483485
}
484486

485487
/// @brief Returns a const segment at the given index without bounds checking.
@@ -489,9 +491,10 @@ class flat_jagged_vector {
489491
/// @warning No bounds checking is performed for performance. Use `at()` for bounds-checked access.
490492
/// Calling on an out-of-bounds index results in Undefined Behavior.
491493
[[nodiscard]] const_segment_type operator[](size_type i) const {
492-
const auto beg = to_diff(this->_offsets[i]);
493-
const auto end = to_diff(this->_offsets[i + 1uz]);
494-
return const_segment_type(this->_data.begin() + beg, this->_data.begin() + end);
494+
return const_segment_type(
495+
this->_data.begin() + to_diff(this->_offsets[i]),
496+
this->_data.begin() + to_diff(this->_offsets[i + 1uz])
497+
);
495498
}
496499

497500
/// @brief Returns a reference to an element within a segment without bounds checking.

include/gl/types/flat_matrix.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ namespace gl {
2828
///
2929
/// @warning Iterator invalidation follows `std::vector` semantics: modifying the dimensions or structural
3030
/// capacity of the matrix invalidates all iterators, pointers, and references to its elements.
31-
///
3231
/// @todo Implement the row_unchecked and col_unchecked methods.
3332
template <std::semiregular T>
3433
class flat_matrix {

include/hgl/impl/incidence_matrix.hpp

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
#pragma once
66

7+
#include "gl/types/core.hpp"
78
#include "hgl/constants.hpp"
89
#include "hgl/decl/impl_tags.hpp"
910
#include "hgl/directional_tags.hpp"
11+
#include "hgl/impl/bf_incidence.hpp"
1012
#include "hgl/impl/layout_tags.hpp"
1113
#include "hgl/types.hpp"
1214

@@ -171,10 +173,11 @@ class incidence_matrix<hgl::undirected_t, ImplTag> final {
171173
else { // remove minor
172174
if (this->_matrix_row_size == 0uz)
173175
return;
176+
174177
this->_matrix_row_size--;
175-
for (auto& row : this->_matrix) {
176-
row.erase(row.begin() + to_diff(id));
177-
}
178+
const auto pos = to_diff(id);
179+
for (auto& row : this->_matrix)
180+
row.erase(row.begin() + pos);
178181
}
179182
}
180183

@@ -248,7 +251,7 @@ class incidence_matrix<hgl::bf_directed_t, ImplTag> final {
248251
: _matrix_row_size{layout_tag::minor(n_vertices, n_hyperedges)},
249252
_matrix(
250253
layout_tag::major(n_vertices, n_hyperedges),
251-
matrix_row_type(_matrix_row_size, incidence_type::none)
254+
matrix_row_type(_matrix_row_size, bf_incidence::none)
252255
) {}
253256

254257
incidence_matrix(const incidence_matrix&) = default;
@@ -273,39 +276,39 @@ class incidence_matrix<hgl::bf_directed_t, ImplTag> final {
273276

274277
[[nodiscard]] gl_attr_force_inline auto incident_hyperedges(const id_type vertex_id
275278
) const noexcept {
276-
return this->_query<element_type::vertex>(vertex_id, _is_incident);
279+
return this->_query<element_type::vertex>(vertex_id, bf_is_incident);
277280
}
278281

279282
[[nodiscard]] size_type degree(const id_type vertex_id) const noexcept {
280-
return this->_count<element_type::vertex>(vertex_id, _is_incident);
283+
return this->_count<element_type::vertex>(vertex_id, bf_is_incident);
281284
}
282285

283286
[[nodiscard]] std::vector<size_type> degree_map(const size_type n_vertices) const noexcept {
284-
return this->_count_map<element_type::vertex>(n_vertices, _is_incident);
287+
return this->_count_map<element_type::vertex>(n_vertices, bf_is_incident);
285288
}
286289

287290
[[nodiscard]] gl_attr_force_inline auto out_hyperedges(const id_type vertex_id) const noexcept {
288-
return this->_query<element_type::vertex>(vertex_id, _is_tail);
291+
return this->_query<element_type::vertex>(vertex_id, bf_is_tail);
289292
}
290293

291294
[[nodiscard]] size_type out_degree(const id_type vertex_id) const noexcept {
292-
return this->_count<element_type::vertex>(vertex_id, _is_tail);
295+
return this->_count<element_type::vertex>(vertex_id, bf_is_tail);
293296
}
294297

295298
[[nodiscard]] std::vector<size_type> out_degree_map(const size_type n_vertices) const noexcept {
296-
return this->_count_map<element_type::vertex>(n_vertices, _is_tail);
299+
return this->_count_map<element_type::vertex>(n_vertices, bf_is_tail);
297300
}
298301

299302
[[nodiscard]] gl_attr_force_inline auto in_hyperedges(const id_type vertex_id) const noexcept {
300-
return this->_query<element_type::vertex>(vertex_id, _is_head);
303+
return this->_query<element_type::vertex>(vertex_id, bf_is_head);
301304
}
302305

303306
[[nodiscard]] size_type in_degree(const id_type vertex_id) const noexcept {
304-
return this->_count<element_type::vertex>(vertex_id, _is_head);
307+
return this->_count<element_type::vertex>(vertex_id, bf_is_head);
305308
}
306309

307310
[[nodiscard]] std::vector<size_type> in_degree_map(const size_type n_vertices) const noexcept {
308-
return this->_count_map<element_type::vertex>(n_vertices, _is_head);
311+
return this->_count_map<element_type::vertex>(n_vertices, bf_is_head);
309312
}
310313

311314
// --- hyperedge methods : general ---
@@ -322,44 +325,44 @@ class incidence_matrix<hgl::bf_directed_t, ImplTag> final {
322325

323326
[[nodiscard]] gl_attr_force_inline auto incident_vertices(const id_type hyperedge_id
324327
) const noexcept {
325-
return this->_query<element_type::hyperedge>(hyperedge_id, _is_incident);
328+
return this->_query<element_type::hyperedge>(hyperedge_id, bf_is_incident);
326329
}
327330

328331
[[nodiscard]] size_type hyperedge_size(const id_type hyperedge_id) const noexcept {
329-
return this->_count<element_type::hyperedge>(hyperedge_id, _is_incident);
332+
return this->_count<element_type::hyperedge>(hyperedge_id, bf_is_incident);
330333
}
331334

332335
[[nodiscard]] std::vector<size_type> hyperedge_size_map(const size_type n_hyperedges
333336
) const noexcept {
334-
return this->_count_map<element_type::hyperedge>(n_hyperedges, _is_incident);
337+
return this->_count_map<element_type::hyperedge>(n_hyperedges, bf_is_incident);
335338
}
336339

337340
[[nodiscard]] gl_attr_force_inline auto tail_vertices(const id_type hyperedge_id
338341
) const noexcept {
339-
return this->_query<element_type::hyperedge>(hyperedge_id, _is_tail);
342+
return this->_query<element_type::hyperedge>(hyperedge_id, bf_is_tail);
340343
}
341344

342345
[[nodiscard]] size_type tail_size(const id_type hyperedge_id) const noexcept {
343-
return this->_count<element_type::hyperedge>(hyperedge_id, _is_tail);
346+
return this->_count<element_type::hyperedge>(hyperedge_id, bf_is_tail);
344347
}
345348

346349
[[nodiscard]] std::vector<size_type> tail_size_map(const size_type n_hyperedges
347350
) const noexcept {
348-
return this->_count_map<element_type::hyperedge>(n_hyperedges, _is_tail);
351+
return this->_count_map<element_type::hyperedge>(n_hyperedges, bf_is_tail);
349352
}
350353

351354
[[nodiscard]] gl_attr_force_inline auto head_vertices(const id_type hyperedge_id
352355
) const noexcept {
353-
return this->_query<element_type::hyperedge>(hyperedge_id, _is_head);
356+
return this->_query<element_type::hyperedge>(hyperedge_id, bf_is_head);
354357
}
355358

356359
[[nodiscard]] size_type head_size(const id_type hyperedge_id) const noexcept {
357-
return this->_count<element_type::hyperedge>(hyperedge_id, _is_head);
360+
return this->_count<element_type::hyperedge>(hyperedge_id, bf_is_head);
358361
}
359362

360363
[[nodiscard]] std::vector<size_type> head_size_map(const size_type n_hyperedges
361364
) const noexcept {
362-
return this->_count_map<element_type::hyperedge>(n_hyperedges, _is_head);
365+
return this->_count_map<element_type::hyperedge>(n_hyperedges, bf_is_head);
363366
}
364367

365368
// --- binding methods ---
@@ -368,40 +371,40 @@ class incidence_matrix<hgl::bf_directed_t, ImplTag> final {
368371
const id_type vertex_id, const id_type hyperedge_id
369372
) noexcept {
370373
const auto [major_id, minor_id] = layout_tag::majmin(vertex_id, hyperedge_id);
371-
this->_matrix[to_idx(major_id)][to_idx(minor_id)] = incidence_type::backward;
374+
this->_matrix[to_idx(major_id)][to_idx(minor_id)] = bf_incidence::backward;
372375
}
373376

374377
gl_attr_force_inline void bind_head(
375378
const id_type vertex_id, const id_type hyperedge_id
376379
) noexcept {
377380
const auto [major_id, minor_id] = layout_tag::majmin(vertex_id, hyperedge_id);
378-
this->_matrix[to_idx(major_id)][to_idx(minor_id)] = incidence_type::forward;
381+
this->_matrix[to_idx(major_id)][to_idx(minor_id)] = bf_incidence::forward;
379382
}
380383

381384
gl_attr_force_inline void unbind(const id_type vertex_id, const id_type hyperedge_id) noexcept {
382385
const auto [major_id, minor_id] = layout_tag::majmin(vertex_id, hyperedge_id);
383-
this->_matrix[to_idx(major_id)][to_idx(minor_id)] = incidence_type::none;
386+
this->_matrix[to_idx(major_id)][to_idx(minor_id)] = bf_incidence::none;
384387
}
385388

386389
[[nodiscard]] gl_attr_force_inline bool are_bound(
387390
const id_type vertex_id, const id_type hyperedge_id
388391
) const noexcept {
389392
const auto [major_id, minor_id] = layout_tag::majmin(vertex_id, hyperedge_id);
390-
return this->_matrix[to_idx(major_id)][to_idx(minor_id)] != incidence_type::none;
393+
return this->_matrix[to_idx(major_id)][to_idx(minor_id)] != bf_incidence::none;
391394
}
392395

393396
[[nodiscard]] gl_attr_force_inline bool is_tail(
394397
const id_type vertex_id, const id_type hyperedge_id
395398
) const noexcept {
396399
const auto [major_id, minor_id] = layout_tag::majmin(vertex_id, hyperedge_id);
397-
return this->_matrix[to_idx(major_id)][to_idx(minor_id)] == incidence_type::backward;
400+
return this->_matrix[to_idx(major_id)][to_idx(minor_id)] == bf_incidence::backward;
398401
}
399402

400403
[[nodiscard]] gl_attr_force_inline bool is_head(
401404
const id_type vertex_id, const id_type hyperedge_id
402405
) const noexcept {
403406
const auto [major_id, minor_id] = layout_tag::majmin(vertex_id, hyperedge_id);
404-
return this->_matrix[to_idx(major_id)][to_idx(minor_id)] == incidence_type::forward;
407+
return this->_matrix[to_idx(major_id)][to_idx(minor_id)] == bf_incidence::forward;
405408
}
406409

407410
// --- comparison ---
@@ -421,43 +424,23 @@ class incidence_matrix<hgl::bf_directed_t, ImplTag> final {
421424
#endif
422425

423426
private:
424-
// --- incidence utility ---
425-
426-
enum class incidence_type : std::int8_t {
427-
none = 0, // v not in E
428-
backward = -1, // v in Tail(E)
429-
forward = 1, // v in Head(E)
430-
};
431-
432-
static constexpr auto _is_incident = [](const incidence_type t) {
433-
return t != incidence_type::none;
434-
};
435-
436-
static constexpr auto _is_tail = [](const incidence_type t) {
437-
return t == incidence_type::backward;
438-
};
439-
440-
static constexpr auto _is_head = [](const incidence_type t) {
441-
return t == incidence_type::forward;
442-
};
443-
444427
// --- storage management ---
445428

446-
using matrix_row_type = std::vector<incidence_type>;
429+
using matrix_row_type = std::vector<bf_incidence>;
447430
using hypergraph_storage_type = std::vector<matrix_row_type>;
448431

449432
template <element_type Element>
450433
void _add(const size_type n) noexcept {
451434
if constexpr (Element == layout_tag::major_element) { // add major
452435
this->_matrix.resize(
453436
this->_matrix.size() + n,
454-
matrix_row_type(this->_matrix_row_size, incidence_type::none)
437+
matrix_row_type(this->_matrix_row_size, bf_incidence::none)
455438
);
456439
}
457440
else { // add minor
458441
this->_matrix_row_size += n;
459442
for (auto& row : this->_matrix)
460-
row.resize(this->_matrix_row_size, incidence_type::none);
443+
row.resize(this->_matrix_row_size, bf_incidence::none);
461444
}
462445
}
463446

@@ -469,15 +452,17 @@ class incidence_matrix<hgl::bf_directed_t, ImplTag> final {
469452
else { // remove minor
470453
if (this->_matrix_row_size == 0uz)
471454
return;
455+
472456
this->_matrix_row_size--;
457+
const auto pos = to_diff(id);
473458
for (auto& row : this->_matrix)
474-
row.erase(row.begin() + to_diff(id));
459+
row.erase(row.begin() + pos);
475460
}
476461
}
477462

478463
template <element_type Element>
479464
[[nodiscard]] gl_attr_force_inline auto _query(
480-
const id_type id, std::predicate<incidence_type> auto&& pred
465+
const id_type id, std::predicate<bf_incidence> auto&& pred
481466
) const noexcept {
482467
if constexpr (Element == layout_tag::major_element) { // query major
483468
return std::views::iota(initial_id_v<id_type>, this->_matrix_row_size)
@@ -495,10 +480,10 @@ class incidence_matrix<hgl::bf_directed_t, ImplTag> final {
495480

496481
template <element_type Element>
497482
[[nodiscard]] gl_attr_force_inline size_type
498-
_count(const id_type id, std::predicate<incidence_type> auto&& pred) const noexcept {
483+
_count(const id_type id, std::predicate<bf_incidence> auto&& pred) const noexcept {
499484
size_type count = 0uz;
500485
if constexpr (Element == layout_tag::major_element) { // count major
501-
for (const incidence_type t : this->_matrix[to_idx(id)])
486+
for (const bf_incidence t : this->_matrix[to_idx(id)])
502487
count += static_cast<size_type>(pred(t));
503488
}
504489
else { // count minor
@@ -511,7 +496,7 @@ class incidence_matrix<hgl::bf_directed_t, ImplTag> final {
511496

512497
template <element_type Element>
513498
[[nodiscard]] std::vector<size_type> _count_map(
514-
const size_type n_elements, std::predicate<incidence_type> auto&& pred
499+
const size_type n_elements, std::predicate<bf_incidence> auto&& pred
515500
) const noexcept {
516501
std::vector<size_type> size_map(n_elements, 0uz);
517502

0 commit comments

Comments
 (0)