IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
associative_container.hxx
Go to the documentation of this file.
1
3
4#pragma once
6
8{
9
11 {
12 template<ice::concepts::AssociativeContainer Self, ice::concepts::HashableKeyType KeyType>
13 bool has(this Self const& self, KeyType const& key) noexcept
14 {
15 return self.find(ice::hash(key)) != nullptr;
16 }
17
18 template<ice::concepts::AssociativeContainer Self, ice::concepts::HashableKeyType KeyType>
19 bool missing(this Self const& self, KeyType const& key) noexcept
20 {
21 return self.find(ice::hash(key)) == nullptr;
22 }
23
24 template<
27 typename InValueType = typename std::remove_reference_t<Self>::ValueType
28 >
29 auto get(
30 this Self const& self,
31 KeyType const& key,
32 InValueType const& fallback_value
34 {
35 ice::container::ValuePtr<Self const> result = self.find(ice::hash(key));
36 // TODO: We need to rethink all 'get' methods since
37 // we will always run into lifetime issue with this approach.
38 return result != nullptr ? *result : fallback_value;
39 }
40
41 template<
44 auto try_get(
45 this Self&& self,
46 KeyType const& key
48 {
49 return self.find(ice::hash(key));
50 }
51 };
52
53
55 {
56 template<
59 typename InValueType = typename std::remove_reference_t<Self>::ValueType
60 >
61 requires(std::convertible_to<InValueType, typename Self::ValueType>)
62 auto set(
63 this Self& self,
64 KeyType const& key,
65 InValueType&& in_value
67 {
68 return self.store(ice::hash(key), std::forward<InValueType>(in_value));
69 }
70
71 template<
74 typename InValueType = typename std::remove_reference_t<Self>::ValueType
75 >
76 requires(std::convertible_to<InValueType, typename Self::ValueType>)
78 this Self& self,
79 KeyType const& key,
80 InValueType&& in_value
81 ) noexcept
82 {
83 ice::u64 const key_hash = ice::hash(key);
84 if (self.missing(key_hash))
85 {
86 self.set(key_hash, std::forward<InValueType>(in_value));
87 }
88 }
89
90 template<
93 typename InValueType = typename std::remove_reference_t<Self>::ValueType
94 >
96 this Self&& self,
97 KeyType const& key,
98 InValueType&& in_value
100 {
101 ice::u64 const key_hash = ice::hash(key);
102 if (self.missing(key_hash))
103 {
104 return self.set(key_hash, std::forward<InValueType>(in_value));
105 }
106 else
107 {
108 ice::container::ValuePtr<Self> result = self.try_get(key_hash);
109 ICE_ASSERT_CORE(result != nullptr);
110 return *result;
111 }
112 }
113
114 template<
117 bool remove(this Self&& self, KeyType const& key) noexcept
118 {
119 return self.remove(ice::hash(key));
120 }
121 };
122
123} // namespace ice::container
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
Definition container_concepts.hxx:33
Definition container_concepts.hxx:47
Definition container_concepts.hxx:83
Definition associative_container.hxx:8
ValueType< ContainerT > * ValuePtr
Definition container_concepts.hxx:155
typename std::remove_reference_t< ContainerT >::KeyType KeyType
Definition container_concepts.hxx:135
ValueType< ContainerT > & ValueRef
Definition container_concepts.hxx:149
std::uint64_t u64
Definition types.hxx:27
constexpr auto hash(ice::HeapString<> const &value) noexcept -> ice::u64
Definition heap_string.hxx:251
Definition associative_container.hxx:11
auto get(this Self const &self, KeyType const &key, InValueType const &fallback_value) noexcept -> ice::container::ValueRef< Self const >
Definition associative_container.hxx:29
auto try_get(this Self &&self, KeyType const &key) noexcept -> ice::container::ValuePtr< Self >
Definition associative_container.hxx:44
bool has(this Self const &self, KeyType const &key) noexcept
Definition associative_container.hxx:13
bool missing(this Self const &self, KeyType const &key) noexcept
Definition associative_container.hxx:19
Definition associative_container.hxx:55
bool remove(this Self &&self, KeyType const &key) noexcept
Definition associative_container.hxx:117
auto set(this Self &self, KeyType const &key, InValueType &&in_value) noexcept -> ice::container::ValueRef< Self >
Definition associative_container.hxx:62
auto get_or_set(this Self &&self, KeyType const &key, InValueType &&in_value) noexcept -> ice::container::ValueRef< Self >
Definition associative_container.hxx:95
void set_if_missing(this Self &self, KeyType const &key, InValueType &&in_value) noexcept
Definition associative_container.hxx:77
Definition basic_container.hxx:11