Peano
Loading...
Searching...
No Matches
linked_grid.h
Go to the documentation of this file.
1#pragma once
2
3#include <lang/type.h>
4#include <lang/numvec.h>
5#include <geometry/geometry.h>
6
7template<typename _Geo, u32 SPLIT_FACTOR, typename Derived> requires (SPLIT_FACTOR > 1)
8class LinkedGrid {
9 static _Geo::RowMajorIdx getIdxForLinearSpaceIdx(u32 linearSpaceIdx) {
10 auto idx = typename Geo::RowMajorIdx(0);
11
12 for (int d = 0; d < Geo::D::N; d++) {
13 idx[d] = (linearSpaceIdx / (u32) std::pow(SPLIT_FACTOR, d)) % SPLIT_FACTOR;
14 }
15
16 return idx;
17 }
18
19 _Geo::RowMajorIdx getChildIdx(int linearSpaceChildIdx) const {
20 auto childIdx = getIdxForLinearSpaceIdx(linearSpaceChildIdx);
21 childIdx = this->idx * SPLIT_FACTOR + childIdx;
22 return childIdx;
23 }
24
25 void moveOp(LinkedGrid &&other) {
26 assert(this->subtrees == nullptr)
27 assert(other.parent == nullptr)
28
29 this->range = other.range;
30 this->idx = other.idx;
31 this->level = other.level;
32 this->subtrees = other.subtrees;
33
34 other.subtrees = nullptr;
35
36 if (!this->subtrees) return;
37
38 for (u32 i = 0; i < LinkedGrid::getMaxChildren(); i++) {
39 this->subtrees[i].parent = (Derived*) this;
40 }
41 }
42
43public:
44 using Geo = _Geo;
45
46 static constexpr u32 SplitFactor = SPLIT_FACTOR;
47
48 Derived *parent = nullptr;
49 Derived *subtrees = nullptr; // TODO find more suitable names for these
50
51 Geo::Region range = typename Geo::Region();
52
54 unsigned int level = 0;
55
56 LinkedGrid() = default;
57
58 explicit LinkedGrid(Geo::Region range,
59 Derived *parent = nullptr,
60 Geo::RowMajorIdx idx = typename Geo::RowMajorIdx(0),
61 int level = 0) : parent(parent), range(range), idx(idx), level(level) {}
62
63 static constexpr u32 getMaxChildren() {
64 return Geo::template PowerN<SPLIT_FACTOR>();
65 }
66
67 LinkedGrid(LinkedGrid&& other) noexcept {
68 this->moveOp(std::move(other));
69 }
70
71 LinkedGrid& operator=(LinkedGrid&& other) noexcept {
72 this->moveOp(std::move(other));
73 return *this;
74 }
75
76 void split(u32 splitLevel = 1) {
77 if (splitLevel == 0) return;
78
79 if (!this->subtrees) {
80 this->subtrees = (Derived*) malloc(sizeof(Derived) * getMaxChildren());
81
82 typename Geo::Region subranges[getMaxChildren()];
83 this->range.split(SPLIT_FACTOR, subranges);
84
85 for (int i = 0; i < getMaxChildren(); i++) {
86 auto childIdx = this->getChildIdx(i);
87 new (this->subtrees + i) Derived (subranges[i], (Derived*) this, childIdx, this->level + 1);
88 }
89 }
90
91 for (int i = 0; i < getMaxChildren(); i++) {
92 this->subtrees[i].split(splitLevel - 1);
93 }
94 }
95
96 Derived *getRoot() {
97 auto *node = (Derived*) this;
98 while (node->parent) node = node->parent;
99 return node;
100 }
101
102 Derived *getChild(const Geo::Point &p) {
103 auto childIdx = this->range.splitIdx(SPLIT_FACTOR, p);
104 auto *child = &this->subtrees[childIdx];
105 return child;
106 }
107
109 assert(this->parent)
110 return (Derived *) this - this->parent->subtrees;
111 }
112
113 Derived *getNextSibling() {
114 if (!this->parent) return nullptr;
115
116 auto siblingIdx = this->getSiblingIdx();
117 if (siblingIdx < getMaxChildren() - 1) {
118 return &this->parent->subtrees[siblingIdx + 1];
119 }
120
121 return nullptr;
122 }
123};
#define assert(...)
Definition LinuxAMD.h:28
LinkedGrid(LinkedGrid &&other) noexcept
Definition linked_grid.h:67
LinkedGrid(Geo::Region range, Derived *parent=nullptr, Geo::RowMajorIdx idx=typename Geo::RowMajorIdx(0), int level=0)
Definition linked_grid.h:58
void moveOp(LinkedGrid &&other)
Definition linked_grid.h:25
u32 getSiblingIdx() const
static _Geo::RowMajorIdx getIdxForLinearSpaceIdx(u32 linearSpaceIdx)
Definition linked_grid.h:9
Derived * getRoot()
Definition linked_grid.h:96
LinkedGrid & operator=(LinkedGrid &&other) noexcept
Definition linked_grid.h:71
LinkedGrid()=default
_Geo::RowMajorIdx getChildIdx(int linearSpaceChildIdx) const
Definition linked_grid.h:19
Derived * getNextSibling()
static constexpr u32 getMaxChildren()
Definition linked_grid.h:63
Derived * getChild(const Geo::Point &p)
void split(u32 splitLevel=1)
Definition linked_grid.h:76
Region< dim, fp, topBoundaryInclusive > Region
Definition geometry.h:243
NumVec< u64, D::N > RowMajorIdx
Definition geometry.h:245
Point< dim, fp > Point
Definition geometry.h:242
std::uint32_t u32
Definition type.h:11