numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
node_connection_list.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <span>
23
27
29
37template <typename StorageIndex = int>
39public:
41 using storage_index_type = StorageIndex;
42
49
54
66
72 void push_back(storage_index_type node_index) {
73 node_indices_.push_back(node_index);
74 }
75
84
86
91
97 [[nodiscard]] auto num_nodes() const -> index_type {
98 return static_cast<index_type>(begin_indices_.size()) -
99 static_cast<index_type>(1);
100 }
101
109 [[nodiscard]] auto connected_nodes_to(index_type node_index) const
110 -> std::span<const storage_index_type> {
111 return std::span<const storage_index_type>(
112 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
113 node_indices_.data() + begin_indices_[node_index],
114 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
115 node_indices_.data() + begin_indices_[node_index + 1]);
116 }
117
119
128 [[nodiscard]] auto transpose() const
130 const auto num_nodes_cached =
131 static_cast<storage_index_type>(num_nodes());
132
134 begin_indices_.size(), static_cast<storage_index_type>(0));
135 for (const storage_index_type index : node_indices_) {
136 NUM_COLLECT_DEBUG_ASSERT(index < num_nodes_cached);
137 ++begin_indices[index + 1];
138 }
139 for (index_type i = 1; i < begin_indices.size() - 1; ++i) {
140 begin_indices[i + 1] += begin_indices[i];
141 }
143
145 util::vector<storage_index_type> next_index = begin_indices;
146 for (storage_index_type i = 0; i < num_nodes_cached; ++i) {
147 for (const storage_index_type j : connected_nodes_to(i)) {
148 auto& index = next_index[j];
149 NUM_COLLECT_DEBUG_ASSERT(index < begin_indices[j + 1]);
150 node_indices[index] = i;
151 ++index;
152 }
153 }
154
156 std::move(node_indices), std::move(begin_indices));
157 }
158
159private:
169 : node_indices_(std::move(node_indices)),
170 begin_indices_(std::move(begin_indices)) {}
171
174
177};
178
179} // namespace num_collect::linear::impl::amg
Definition of assertion macros.
#define NUM_COLLECT_DEBUG_ASSERT(CONDITION)
Macro to check whether a condition is satisfied in debug build only.
Definition assert.h:75
Class of lists of connected nodes per node.
node_connection_list(util::vector< storage_index_type > node_indices, util::vector< storage_index_type > begin_indices)
Constructor.
void finish_current_node()
Finish adding connected nodes to the current node and start the next node.
util::vector< storage_index_type > node_indices_
Indices of connected nodes.
auto num_nodes() const -> index_type
Get the number of nodes.
StorageIndex storage_index_type
Type of indices in storages.
auto transpose() const -> node_connection_list< storage_index_type >
Make a transposed list.
auto connected_nodes_to(index_type node_index) const -> std::span< const storage_index_type >
Get the list of indices of connected nodes to the node with the given index.
void push_back(storage_index_type node_index)
Add a connected node to the current node.
util::vector< storage_index_type > begin_indices_
List of indices of beginning of the list of connected nodes per node.
Class of vectors wrapping std::vector class to use singed integers as indices.
Definition vector.h:37
auto size() const -> index_type
Get the size of this vector.
Definition vector.h:226
auto data() noexcept -> pointer
Get the pointer to the first element.
Definition vector.h:202
void push_back(const value_type &value)
Add an element.
Definition vector.h:258
void clear()
Remove the all elements in this vector.
Definition vector.h:251
auto back() -> reference
Access to the final element.
Definition vector.h:140
Definition of index_type type.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of internal implementations of algebraic multigrid method ruge1987.
STL namespace.
Definition of vector class.