entttree 0.1.0
Hierarchical entity management for EnTT
Loading...
Searching...
No Matches
position.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <entttree/defs.h>
9
10namespace entttree {
11
27struct Position {
28 using symbol_t = uint8_t;
29
30private:
31
32 static constexpr size_t SYMBOL_BITS = sizeof(symbol_t) * 8;
33 static constexpr symbol_t HIGH_BIT = ((symbol_t) 1) << (SYMBOL_BITS - 1);
34 static constexpr symbol_t FULL_SYMBOL = ~((symbol_t) 0);
35
36 // number of locally-stored symbols. chosen so that the whole struct
37 // is a multiple of 8 bytes, for alignment.
38 static constexpr size_t K = 14;
39
40 template <typename T, typename H>
41 friend struct geom::Digest;
42
43 // high order bits are at low indices.
44 union {
45 symbol_t _symbols[K];
46 symbol_t* _symbol_ptr;
47 };
48 uint16_t _size;
49
50 Position(size_t n);
51
52 void _push_back(symbol_t s);
53
54protected:
55
56 symbol_t* _data();
57 const symbol_t* _data() const;
58
59 symbol_t operator[](size_t i) const;
60
61public:
62
64 Position();
65 Position(const Position& other);
67 ~Position();
68
69 Position& operator=(const Position& other);
70 Position& operator=( Position&& other);
71
73 std::strong_ordering operator<=>(const Position& other) const;
74 bool operator==(const Position& other) const {
75 return (*this <=> other) == std::strong_ordering::equal;
76 }
77
79 Position before() const;
80
88 Position between(const Position& other) const;
89
91 Position after() const;
92
94 size_t byte_count() const { return _size; }
95
97 const symbol_t* bytes() const { return _data(); }
98
102 static Position from_bytes(const symbol_t* data, size_t count);
103};
104
105} // namespace entttree
106
107
108template <typename H>
109struct geom::Digest<entttree::Position,H> {
110 H operator()(const entttree::Position& pos) const {
111 using symbol_t = entttree::Position::symbol_t;
112 H nonce = geom::truncated_constant<H>(0x249707f545e427faULL, 0x5905299ebf487d2b);
113 return geom::hash_bytes<H>(
114 nonce,
115 pos._data(),
116 pos._size * sizeof(symbol_t)
117 );
118 }
119};
120
121
122template <>
123struct std::hash<entttree::Position> {
124 size_t operator()(const entttree::Position& pos) const;
125};
Common type aliases, enumerations, and utility functions used throughout entttree.
A system for maintaining parent-child relationships on entities.
Definition hierarchy.h:37
A fractional index with strong ordering, used for sibling positioning.
Definition position.h:27
static Position from_bytes(const symbol_t *data, size_t count)
Construct a Position from raw symbol bytes.
Definition position.cpp:208
const symbol_t * bytes() const
Read-only pointer to the raw symbol bytes (big-endian, high-order first).
Definition position.h:97
symbol_t * _symbol_ptr
Heap pointer when size exceeds K.
Definition position.h:46
Position()
Construct a Position at the midpoint (0.5).
Definition position.cpp:49
Position after() const
Return a position immediately after this one.
Definition position.cpp:140
Position before() const
Return a position immediately before this one.
Definition position.cpp:160
std::strong_ordering operator<=>(const Position &other) const
Lexicographic three-way comparison.
Definition position.cpp:123
size_t byte_count() const
Number of symbols in the fractional-index representation.
Definition position.h:94
Position between(const Position &other) const
Return a position midway between this position and other.
Definition position.cpp:178