26 #include "../../match.h"
27 #include "../../pmr/aliases.h"
28 #include "../../pmr/pool_resource.h"
29 #include "../../static_vector.h"
36 template <
typename ObjectType >
39 static constexpr
bool is_const = std::is_const_v< ObjectType >;
41 using object_type = ObjectType;
42 using child_id = uint32_t;
43 using node_id = std::tuple_element_t< 1, typename object_type::value_type >;
45 using const_iterator =
typename object_type::const_iterator;
47 std::conditional_t< is_const, const_iterator, typename object_type::iterator >;
62 [[nodiscard]] iterator
end()
67 [[nodiscard]] const_iterator
begin()
const
72 [[nodiscard]] const_iterator
end()
const
77 [[nodiscard]] std::optional< node_id >
get_child( child_id chid )
const
79 if ( obj_->size() < chid )
81 auto iter = obj_->begin();
82 std::advance( iter, chid );
86 [[nodiscard]] std::optional< node_id >
get_child( key_type k )
const
88 auto iter = obj_->find( k );
89 if ( iter == obj_->end() )
94 [[nodiscard]] uint32_t
size()
const
96 return static_cast< uint32_t
>( obj_->size() );
99 [[nodiscard]] key_type
const*
get_key( child_id chid )
const
101 if ( chid >= obj_->size() )
104 auto iter = obj_->begin();
105 std::advance( iter, chid );
109 void set( key_type
const& k, node_id nid )
111 obj_->emplace( k, nid );
118 #ifdef EMLABCPP_USE_OSTREAM
119 template <
typename ObjectType >
120 std::ostream&
operator<<( std::ostream& os, contiguous_object_handle< ObjectType >
const& oh )
122 for (
auto const& [key, nid] : oh )
123 os << key <<
":" << nid <<
",";
128 template <
typename ArrayType >
131 static constexpr
bool is_const = std::is_const_v< ArrayType >;
133 using array_type = ArrayType;
134 using child_id = uint32_t;
135 using node_id = std::tuple_element_t< 1, typename array_type::value_type >;
136 using const_iterator =
typename array_type::const_iterator;
138 std::conditional_t< is_const, const_iterator, typename array_type::iterator >;
150 return arr_->begin();
153 [[nodiscard]] iterator
end()
158 [[nodiscard]] const_iterator
begin()
const
160 return arr_->begin();
163 [[nodiscard]] const_iterator
end()
const
168 [[nodiscard]] std::optional< node_id >
get_child( child_id chid )
const
170 auto iter = arr_->find( chid );
171 if ( iter == arr_->end() )
176 [[nodiscard]] uint32_t
size()
const
178 return static_cast< uint32_t
>( arr_->size() );
183 arr_->emplace( arr_->size(), nid );
190 #ifdef EMLABCPP_USE_OSTREAM
191 template <
typename ArrayType >
192 std::ostream&
operator<<( std::ostream& os, contiguous_array_handle< ArrayType >
const& ah )
194 for (
auto const& [chid, nid] : ah )
195 os << chid <<
":" << nid <<
",";
200 template <
typename Key,
typename Value >
217 : content_( std::move( cont ) )
223 return std::get_if< Value >( &content_ );
228 return std::get_if< Value >( &content_ );
235 if ( std::holds_alternative< array_type >( content_ ) )
236 return array_handle{ std::get_if< array_type >( &content_ ) };
237 if ( std::holds_alternative< object_type >( content_ ) )
238 return object_handle{ std::get_if< object_type >( &content_ ) };
239 return std::ref( *std::get_if< Value >( &content_ ) );
242 [[nodiscard]] std::variant<
243 std::reference_wrapper< Value const >,
248 if ( std::holds_alternative< array_type >( content_ ) )
250 if ( std::holds_alternative< object_type >( content_ ) )
252 return std::ref( *std::get_if< Value >( &content_ ) );
274 #ifdef EMLABCPP_USE_OSTREAM
275 template <
typename Key,
typename Value >
276 std::ostream&
operator<<( std::ostream& os, contiguous_node< Key, Value >
const& node )
279 [&os](
auto const& val ) {
282 node.get_container_handle() );
287 template <
typename Key,
typename Value >
311 template < u
int16_t Count >
316 , mem_res_( mem_res )
327 auto iter = data_.find( nid );
328 if ( iter == data_.end() )
330 return &iter->second;
335 auto iter = data_.find( nid );
336 if ( iter == data_.end() )
338 return &iter->second;
343 return data_.begin();
353 return data_.begin();
361 [[nodiscard]] std::size_t
size()
const
368 return data_.empty();
373 std::optional opt_val = make_node( std::move( val ) );
376 auto [nid, iter] = *opt_val;
382 std::optional opt_val = make_node(
array_type{ mem_res_.get() } );
385 auto [nid, iter] = *opt_val;
386 auto var = iter->second.get_container_handle();
387 return std::make_pair( nid, *std::get_if< array_handle >( &var ) );
392 std::optional opt_val = make_node(
object_type{ mem_res_.get() } );
395 auto [nid, iter] = *opt_val;
396 auto var = iter->second.get_container_handle();
397 return std::make_pair( nid, *std::get_if< object_handle >( &var ) );
401 std::optional< std::pair< node_id, iterator > > make_node(
content_type cont )
403 if ( mem_res_.get().is_full() )
405 auto nid =
static_cast< node_id >( data_.size() );
406 auto [iter, inserted] = data_.emplace( nid,
node_type{ std::move( cont ) } );
407 return std::make_pair( nid, iter );
411 std::reference_wrapper< pmr::memory_resource > mem_res_;
414 #ifdef EMLABCPP_USE_OSTREAM
415 template <
typename Key,
typename Value >
416 std::ostream&
operator<<( std::ostream& os, contiguous_tree< Key, Value >
const& tree )
418 for (
auto const& [nid, node] : tree )
419 os << nid <<
":" << node <<
"\n";
const_iterator begin() const
Definition: tree.h:158
std::optional< node_id > get_child(child_id chid) const
Definition: tree.h:168
contiguous_array_handle(array_type *arr)
Definition: tree.h:143
iterator begin()
Definition: tree.h:148
void append(node_id nid)
Definition: tree.h:181
const_iterator end() const
Definition: tree.h:163
uint32_t size() const
Definition: tree.h:176
typename array_type::node_type node_type
Definition: tree.h:141
iterator end()
Definition: tree.h:153
contiguous_object_handle< object_type const > const_object_handle
Definition: tree.h:212
Value value_type
Definition: tree.h:205
contiguous_array_handle< array_type > array_handle
Definition: tree.h:213
pmr::map< key_type, node_id > object_type
Definition: tree.h:209
uint32_t child_id
Definition: tree.h:207
Value const * get_value() const
Definition: tree.h:221
Value * get_value()
Definition: tree.h:226
contiguous_array_handle< array_type const > const_array_handle
Definition: tree.h:214
std::variant< std::reference_wrapper< Value const >, object_handle, array_handle > get_container_handle()
Definition: tree.h:233
std::variant< Value, array_type, object_type > content_type
Definition: tree.h:210
pmr::map< child_id, node_id > array_type
Definition: tree.h:208
contiguous_node(content_type cont)
Definition: tree.h:216
contiguous_tree_type get_type() const
Definition: tree.h:255
uint32_t node_id
Definition: tree.h:206
Key key_type
Definition: tree.h:204
contiguous_object_handle< object_type > object_handle
Definition: tree.h:211
std::variant< std::reference_wrapper< Value const >, const_object_handle, const_array_handle > get_container_handle() const
Definition: tree.h:246
uint32_t size() const
Definition: tree.h:94
key_type const * get_key(child_id chid) const
Definition: tree.h:99
const_iterator begin() const
Definition: tree.h:67
typename object_type::node_type node_type
Definition: tree.h:50
iterator begin()
Definition: tree.h:57
std::optional< node_id > get_child(child_id chid) const
Definition: tree.h:77
const_iterator end() const
Definition: tree.h:72
contiguous_object_handle(object_type *obj)
Definition: tree.h:52
std::optional< node_id > get_child(key_type k) const
Definition: tree.h:86
void set(key_type const &k, node_id nid)
Definition: tree.h:109
iterator end()
Definition: tree.h:62
std::optional< std::pair< node_id, array_handle > > make_array_node()
Definition: tree.h:380
const_iterator end() const
Definition: tree.h:356
Key key_type
Definition: tree.h:291
Value value_type
Definition: tree.h:292
bool empty() const
Definition: tree.h:366
node_type * get_node(node_id nid)
Definition: tree.h:333
pmr::map< key_type, node_id > object_type
Definition: tree.h:296
typename container::iterator iterator
Definition: tree.h:305
uint32_t child_id
Definition: tree.h:294
contiguous_node< Key, Value > node_type
Definition: tree.h:297
typename node_type::object_handle object_handle
Definition: tree.h:300
typename container::const_iterator const_iterator
Definition: tree.h:306
std::optional< std::pair< node_id, object_handle > > make_object_node()
Definition: tree.h:390
typename node_type::array_handle array_handle
Definition: tree.h:302
void clear()
Definition: tree.h:320
static constexpr std::size_t required_pool_size
Definition: tree.h:309
pmr::map< child_id, node_id > array_type
Definition: tree.h:295
pmr::map< node_id, node_type > container
Definition: tree.h:298
iterator begin()
Definition: tree.h:341
node_type const * get_node(node_id nid) const
Definition: tree.h:325
typename node_type::const_array_handle const_array_handle
Definition: tree.h:303
uint32_t node_id
Definition: tree.h:293
contiguous_tree(pmr::memory_resource &mem_res)
Definition: tree.h:314
std::optional< node_id > make_value_node(value_type val)
Definition: tree.h:371
std::size_t size() const
Definition: tree.h:361
const_iterator begin() const
Definition: tree.h:351
typename node_type::const_object_handle const_object_handle
Definition: tree.h:301
typename node_type::content_type content_type
Definition: tree.h:299
iterator end()
Definition: tree.h:346
Definition: memory_resource.h:33
Definition: pool_resource.h:36
std::map< Key, T, std::less< Key >, allocator< std::pair< Key const, T > > > map
Definition: aliases.h:62
key_type_buffer key_type
Definition: base.h:47
MIT License.
Definition: impl.h:31
decltype(auto) match(Variant &&var, Callables &&... cals)
Definition: match.h:55
decltype(auto) visit(Visitor &&vis, Variant &&var)
Reimplementation of std::visit.
Definition: visit.h:44
contiguous_tree_type
Definition: base.h:33
std::ostream & operator<<(std::ostream &os, string_buffer< N > const &sb)
Definition: string_buffer.h:112