-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathentity_adapter.h
More file actions
191 lines (155 loc) · 7.83 KB
/
entity_adapter.h
File metadata and controls
191 lines (155 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
MIT License
Copyright (c) 2021 Attila Csikós
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include "../detail/common.h"
#include "../detail/utils.h"
#include <functional>
#include <iterator>
#include <ranges>
#include <span>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
namespace OrthoTree
{
enum class EntityIdStrategy
{
ContiguousIndex, // Index-based ID, and the tree actively maintains the continuity of indexes (e.g. by swapping elements on removal, so indexes might change).
StableIndex, // Index-based ID, contiguous upon creation, but removals may create "holes", keeping remaining indexes stable.
EntityKeyed // The entity itself contains the ID.
};
template<GeometryType GEOMETRY_TYPE_, typename TEntity, typename TEntityID, typename TEntityContainer, typename TEntityContainerView, typename TGeometry, typename TEntityIDHash = std::hash<TEntityID>>
struct EntityAdapterDefault
{
public:
// OctreeContainer requires the TEntityContainer to eliminate data conversion
using EntityContainer = TEntityContainer;
// Generally, EntityContainerView is used in the Core type
using EntityContainerView = TEntityContainerView;
// Underlying data, that could be anything
using Entity = TEntity;
// Trivially copyable type, that is stored in the nodes
using EntityID = TEntityID;
// Geometric representation of the Entity: Point/Box
using Geometry = TGeometry;
// Hash to store EntityID in hashmaps
using Hash = TEntityIDHash;
static constexpr GeometryType GEOMETRY_TYPE = GEOMETRY_TYPE_;
private:
using EntityContainerViewType = std::remove_cvref_t<EntityContainerView>;
// Whether the Entity contains its ID or not. If IS_ENTITY_KEYED = false, integer-based EntityID-s is required, because continuous indexing should be maintained in the tree.
static constexpr bool IS_ENTITY_KEYED =
!(std::contiguous_iterator<typename EntityContainerViewType::iterator> && std::is_same_v<typename EntityContainerViewType::value_type, Geometry> &&
std::is_integral_v<EntityID> && std::is_default_constructible_v<EntityID>);
public:
static constexpr EntityIdStrategy ENTITY_ID_STRATEGY = IS_ENTITY_KEYED ? EntityIdStrategy::EntityKeyed : EntityIdStrategy::ContiguousIndex;
public: // General accessors
static constexpr Entity GetEntity(EntityContainerView entities, EntityID const& entityID) noexcept { return detail::get(entities, entityID); }
static constexpr EntityID GetEntityID(EntityContainerView entities, Entity const& entity) noexcept
{
return detail::getKeyPart(entities, entity);
}
static constexpr Geometry const& GetGeometry(EntityContainerView entities, EntityID const& entityID) noexcept
{
return detail::at(entities, entityID);
}
static constexpr Geometry const& GetGeometry(Entity const& entity) noexcept { return detail::getValuePart(entity); }
static constexpr void SetGeometry(Entity& entity, Geometry const& geometry) noexcept { return detail::setValuePart(entity, geometry); }
static constexpr std::size_t GetEntityCount(EntityContainerView entities) noexcept
{
if constexpr (std::ranges::sized_range<EntityContainerViewType>)
return std::ranges::size(entities);
else
return std::distance(std::ranges::begin(entities), std::ranges::end(entities));
}
public: // ENTITY_ID_STRATEGY == EntityKeyed required overload
static constexpr EntityID GetEntityID(Entity const& entity) noexcept { return detail::getKeyPart(entity); }
public: // Container mutation
// Insert entity into the container. Returns the EntityID of the newly inserted entity.
static constexpr EntityID Insert(EntityContainer& entities, auto&& entity) noexcept
{
if constexpr (ENTITY_ID_STRATEGY == EntityIdStrategy::EntityKeyed)
{
detail::emplace(entities, std::forward<decltype(entity)>(entity));
return detail::getKeyPart(entities, entity);
}
else
{
auto const id = EntityID(entities.size());
detail::emplace(entities, std::forward<decltype(entity)>(entity));
return id;
}
}
// Erase entity from the container by EntityID.
static constexpr void Erase(EntityContainer& entities, EntityID entityID) noexcept
{
if constexpr (ENTITY_ID_STRATEGY == EntityIdStrategy::EntityKeyed)
detail::erase(entities, entityID);
else
{
if (entityID >= entities.size())
return;
if constexpr (ENTITY_ID_STRATEGY == EntityIdStrategy::ContiguousIndex)
detail::erase(entities, std::next(entities.begin(), entityID));
}
}
// Exchange (replace) the entity at the given EntityID with a new value. The EntityID does not change.
static constexpr auto Exchange(EntityContainer& entities, EntityID entityID, auto&& entity) noexcept
{
if constexpr (ENTITY_ID_STRATEGY == EntityIdStrategy::EntityKeyed)
{
return Entity{ entityID, std::exchange(entities.find(entityID)->second, std::forward<decltype(entity.second)>(entity.second)) };
}
else
{
return std::exchange(detail::at(entities, entityID), std::forward<decltype(entity)>(entity));
}
}
// Clear all entities from the container.
static constexpr void Clear(EntityContainer& entities) noexcept { detail::clear(entities); }
// Check entity containment in the container
static constexpr bool Contains(EntityContainer& entities, EntityID entityID) noexcept
{
if constexpr (ENTITY_ID_STRATEGY == EntityIdStrategy::EntityKeyed)
return detail::contains(entities, entityID);
else
return entityID < entities.size();
}
};
template<typename TPoint>
using PointEntitySpanAdapter = EntityAdapterDefault<GeometryType::Point, TPoint, index_t, std::vector<TPoint>, std::span<TPoint const>, TPoint>;
template<typename TBox>
using BoxEntitySpanAdapter = EntityAdapterDefault<GeometryType::Box, TBox, index_t, std::vector<TBox>, std::span<TBox const>, TBox>;
template<
typename TPoint,
typename TEntityContainer = std::unordered_map<index_t, TPoint>,
typename TEntityContainerView = TEntityContainer const&,
typename THash = std::hash<typename TEntityContainer::key_type>>
using PointEntityMapAdapter =
EntityAdapterDefault<GeometryType::Point, typename TEntityContainer::value_type, typename TEntityContainer::key_type, TEntityContainer, TEntityContainerView, TPoint, THash>;
template<
typename TBox,
typename TEntityContainer = std::unordered_map<index_t, TBox>,
typename TEntityContainerView = TEntityContainer const&,
typename THash = std::hash<typename TEntityContainer::key_type>>
using BoxEntityMapAdapter =
EntityAdapterDefault<GeometryType::Box, typename TEntityContainer::value_type, typename TEntityContainer::key_type, TEntityContainer, TEntityContainerView, TBox, THash>;
} // namespace OrthoTree