YOrch 1.0.0
Loading...
Searching...
No Matches
layout.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5
6namespace yorch::detail {
7
8template <typename... Nodes>
9[[nodiscard]] consteval auto make_level_array() {
10 return std::array<std::size_t, sizeof...(Nodes)> {Nodes::level...};
11}
12
13template <std::size_t N>
14[[nodiscard]] consteval auto make_slot_index_array() {
15 std::array<std::size_t, N> indices {};
16
17 for (std::size_t i = 0; i < N; ++i) {
18 indices[i] = i;
19 }
20
21 return indices;
22}
23
39template <std::size_t N>
40[[nodiscard]] consteval auto make_parent_index_array(const std::array<std::size_t, N>& levels) {
41 std::array<std::size_t, N> parents {};
42 parents.fill(N);
43
44 if constexpr (N > 0) {
45 parents[0] = N;
46
47 for (std::size_t node = 1; node < N; ++node) {
48 const auto target_level = levels[node] - 1;
49
50 for (std::size_t cursor = node; cursor > 0; --cursor) {
51 const auto candidate = cursor - 1;
52
53 if (levels[candidate] == target_level) {
54 parents[node] = candidate;
55 break;
56 }
57 }
58 }
59 }
60
61 return parents;
62}
63
78template <std::size_t N>
80 std::array<std::size_t, N> counts {};
81 std::array<std::size_t, N> offsets {};
82 std::array<std::size_t, (N > 0 ? N - 1 : 0)> indices {};
83};
84
103template <std::size_t N>
104[[nodiscard]] consteval auto make_child_layout(const std::array<std::size_t, N>& parents) {
106
107 if constexpr (N > 0) {
108 for (std::size_t node = 1; node < N; ++node) {
109 ++layout.counts[parents[node]];
110 }
111
112 std::size_t offset = 0;
113 for (std::size_t node = 0; node < N; ++node) {
114 layout.offsets[node] = offset;
115 offset += layout.counts[node];
116 }
117
118 auto next_offsets = layout.offsets;
119
120 for (std::size_t node = 1; node < N; ++node) {
121 const auto parent = parents[node];
122 layout.indices[next_offsets[parent]++] = node;
123 }
124 }
125
126 return layout;
127}
128
129template <typename... Nodes>
130inline constexpr auto compiled_levels_v = make_level_array<Nodes...>();
131
132template <typename... Nodes>
133inline constexpr auto compiled_parent_indices_v =
135
136template <typename... Nodes>
137inline constexpr auto compiled_child_layout_v =
139
140template <typename... Nodes>
141inline constexpr auto compiled_slot_indices_v =
142 make_slot_index_array<sizeof...(Nodes)>();
143
144} // namespace yorch::detail
constexpr auto compiled_parent_indices_v
Definition layout.hpp:133
consteval auto make_child_layout(const std::array< std::size_t, N > &parents)
Builds a flat direct-child adjacency layout from per-node parent indices.
Definition layout.hpp:104
constexpr auto compiled_slot_indices_v
Definition layout.hpp:141
consteval auto make_level_array()
Definition layout.hpp:9
constexpr auto compiled_levels_v
Definition layout.hpp:130
consteval auto make_slot_index_array()
Definition layout.hpp:14
consteval auto make_parent_index_array(const std::array< std::size_t, N > &levels)
Reconstructs each node's direct parent index from the recorded level sequence.
Definition layout.hpp:40
constexpr auto compiled_child_layout_v
Definition layout.hpp:137
constexpr bool is_adapter_descriptor_v
Definition adapters.hpp:63
Flat direct-child adjacency storage for a tree-shaped plan.
Definition layout.hpp:79
std::array< std::size_t, N > counts
Definition layout.hpp:80
std::array< std::size_t,(N > 0 ? N - 1 :0)> indices
Definition layout.hpp:82
std::array< std::size_t, N > offsets
Definition layout.hpp:81