neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
set.hpp
Go to the documentation of this file.
1// set.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
41#include <neolib/core/i_set.hpp>
42
43namespace neolib
44{
45 template <typename T, typename Pred = std::less<typename crack_key<T>::key_type>, typename Alloc = std::allocator<T>>
46 class set : public reference_counted<i_set<abstract_t<T>>>
47 {
48 typedef set<T, Pred, Alloc> self_type;
50 // types
51 public:
55 typedef T key_type;
56 typedef T value_type;
57 typedef Pred compare_type;
58 typedef Alloc allocator_type;
60 private:
62 public:
66 protected:
69 public:
72 // construction
73 public:
75 {
76 }
77 set(const abstract_container& aOther)
78 {
79 assign(aOther);
80 }
81 set(std::initializer_list<value_type> aElements) :
82 iSet(aElements)
83 {}
84 template <typename InputIter>
85 set(InputIter aFirst, InputIter aLast) :
86 iSet(aFirst, aLast)
87 {}
88 // operations
89 public:
90 const std_type& as_std_set() const { return iSet; }
91 std_type& as_std_set() { return iSet; }
92 std_type to_std_set() const { return iSet; }
93 // comparison
94 public:
95 constexpr bool operator==(const self_type& that) const noexcept
96 {
97 return as_std_set() == that.as_std_set();
98 }
99 constexpr std::partial_ordering operator<=>(const self_type& that) const noexcept
100 {
101 return as_std_set() <=> that.as_std_set();
102 }
103 // implementation
104 public:
105 // from i_container
106 size_type size() const noexcept final { return iSet.size(); }
107 size_type max_size() const noexcept final { return iSet.max_size(); }
108 void clear() final { iSet.clear(); }
109 void assign(const abstract_container& aOther) final
110 {
111 if (&aOther == this)
112 return;
113 clear();
114 for (const_iterator i = aOther.begin(); i != aOther.end(); ++i)
115 iSet.insert(value_type{ *i });
116 }
117 private:
118 // from i_container
119 abstract_const_iterator* do_begin(void* memory) const final { return new (memory) container_const_iterator(iSet.begin()); }
120 abstract_const_iterator* do_end(void* memory) const final { return new (memory) container_const_iterator(iSet.end()); }
121 abstract_iterator* do_begin(void* memory) final { return new (memory) container_iterator(iSet.begin()); }
122 abstract_iterator* do_end(void* memory) final { return new (memory) container_iterator(iSet.end()); }
123 abstract_iterator* do_erase(void* memory, const abstract_const_iterator& aPosition) final { return new (memory) container_iterator(iSet.erase(static_cast<const container_const_iterator&>(aPosition))); }
124 abstract_iterator* do_erase(void* memory, const abstract_const_iterator& aFirst, const abstract_const_iterator& aLast) final { return new (memory) container_iterator(iSet.erase(static_cast<const container_const_iterator&>(aFirst), static_cast<const container_const_iterator&>(aLast))); }
125 public:
126 // from i_set
127 abstract_iterator* do_insert(void* memory, const abstract_value_type& aValue) final { return new (memory) container_iterator(iSet.insert(value_type(aValue))); }
128 abstract_const_iterator* do_find(void* memory, const abstract_key_type& aKey) const final { return new (memory) container_const_iterator(iSet.find(value_type{ aKey })); }
129 abstract_iterator* do_find(void* memory, const abstract_key_type& aKey) final { return new (memory) container_iterator(iSet.find(value_type{ aKey })); }
130 private:
131 std_type iSet;
132 };
133
134 template <typename T, typename Pred = std::less<typename crack_key<T>::key_type>, typename Alloc = std::allocator<T>>
135 class multiset : public reference_counted<i_multiset<abstract_t<T>>>
136 {
137 typedef multiset<T, Pred, Alloc> self_type;
139 // types
140 public:
144 typedef T key_type;
145 typedef T value_type;
146 typedef Pred compare_type;
147 typedef Alloc allocator_type;
149 private:
151 public:
155 protected:
158 public:
161 // construction
162 public:
164 {}
166 {
167 assign(aOther);
168 }
169 multiset(std::initializer_list<value_type> aElements) :
170 iSet(aElements)
171 {}
172 template <typename InputIter>
173 multiset(InputIter aFirst, InputIter aLast) :
174 iSet(aFirst, aLast)
175 {}
176 // operations
177 public:
178 const std_type& as_std_multiset() const { return iSet; }
179 std_type& as_std_multiset() { return iSet; }
180 std_type to_std_multiset() const { return iSet; }
181 // comparison
182 public:
183 constexpr bool operator==(const self_type& that) const noexcept
184 {
185 return as_std_multiset() == that.as_std_multiset();
186 }
187 constexpr std::partial_ordering operator<=>(const self_type& that) const noexcept
188 {
189 return as_std_multiset() <=> that.as_std_multiset();
190 }
191 // implementation
192 public:
193 // from i_container
194 size_type size() const noexcept final { return iSet.size(); }
195 size_type max_size() const noexcept final { return iSet.max_size(); }
196 void clear() final { iSet.clear(); }
197 void assign(const abstract_container& aOther) final
198 {
199 if (&aOther == this)
200 return;
201 clear();
202 for (const_iterator i = aOther.begin(); i != aOther.end(); ++i)
203 iSet.insert(value_type{ *i });
204 }
205 private:
206 // from i_container
207 abstract_const_iterator* do_begin(void* memory) const final { return new (memory) container_const_iterator(iSet.begin()); }
208 abstract_const_iterator* do_end(void* memory) const final { return new (memory) container_const_iterator(iSet.end()); }
209 abstract_iterator* do_begin(void* memory) final { return new (memory) container_iterator(iSet.begin()); }
210 abstract_iterator* do_end(void* memory) final { return new (memory) container_iterator(iSet.end()); }
211 abstract_iterator* do_erase(void* memory, const abstract_const_iterator& aPosition) final { return new (memory) container_iterator(iSet.erase(static_cast<const container_const_iterator&>(aPosition))); }
212 abstract_iterator* do_erase(void* memory, const abstract_const_iterator& aFirst, const abstract_const_iterator& aLast) final { return new (memory) container_iterator(iSet.erase(static_cast<const container_const_iterator&>(aFirst), static_cast<const container_const_iterator&>(aLast))); }
213 public:
214 // from i_multiset
215 abstract_iterator* do_insert(void* memory, const abstract_value_type& aValue) final { return new (memory) container_iterator(iSet.insert(value_type{ aValue })); }
216 abstract_const_iterator* do_find(void* memory, const abstract_key_type& aKey) const final { return new (memory) container_const_iterator(iSet.find(value_type{ aKey })); }
217 abstract_iterator* do_find(void* memory, const abstract_key_type& aKey) final { return new (memory) container_iterator(iSet.find(value_type{ aKey })); }
218 private:
219 std_type iSet;
220 };
221}
base_type::abstract_iterator abstract_iterator
Definition i_set.hpp:81
base_type::const_iterator const_iterator
Definition i_set.hpp:83
base_type::const_iterator const_iterator
Definition i_set.hpp:57
base_type::abstract_iterator abstract_iterator
Definition i_set.hpp:55
base_type::iterator iterator
Definition i_set.hpp:58
abstract_type::abstract_iterator abstract_iterator
Definition set.hpp:157
multiset(InputIter aFirst, InputIter aLast)
Definition set.hpp:173
constexpr bool operator==(const self_type &that) const noexcept
Definition set.hpp:183
abstract_const_iterator * do_find(void *memory, const abstract_key_type &aKey) const final
Definition set.hpp:216
multiset(std::initializer_list< value_type > aElements)
Definition set.hpp:169
std_type to_std_multiset() const
Definition set.hpp:180
abstract_type::const_iterator const_iterator
Definition set.hpp:159
const std_type & as_std_multiset() const
Definition set.hpp:178
Pred compare_type
Definition set.hpp:146
constexpr std::partial_ordering operator<=>(const self_type &that) const noexcept
Definition set.hpp:187
mutable_multiset< value_type, compare_type, allocator_type > std_type
Definition set.hpp:148
abstract_iterator * do_find(void *memory, const abstract_key_type &aKey) final
Definition set.hpp:217
abstract_type::iterator iterator
Definition set.hpp:160
size_type size() const noexcept final
Definition set.hpp:194
container::iterator< abstract_value_type, typename std_type::iterator, typename std_type::const_iterator > container_iterator
Definition set.hpp:154
size_type max_size() const noexcept final
Definition set.hpp:195
abstract_t< T > abstract_value_type
Definition set.hpp:143
abstract_type::abstract_const_iterator abstract_const_iterator
Definition set.hpp:156
abstract_t< T > abstract_key_type
Definition set.hpp:142
abstract_type::size_type size_type
Definition set.hpp:152
void clear() final
Definition set.hpp:196
i_multiset< abstract_t< T > > abstract_type
Definition set.hpp:141
void assign(const abstract_container &aOther) final
Definition set.hpp:197
std_type & as_std_multiset()
Definition set.hpp:179
abstract_iterator * do_insert(void *memory, const abstract_value_type &aValue) final
Definition set.hpp:215
multiset(const abstract_container &aOther)
Definition set.hpp:165
Alloc allocator_type
Definition set.hpp:147
container::const_iterator< abstract_value_type, typename std_type::const_iterator > container_const_iterator
Definition set.hpp:153
iterator insert(const typename base_type::value_type &aValue)
void assign(const abstract_container &aOther) final
Definition set.hpp:109
void clear() final
Definition set.hpp:108
T value_type
Definition set.hpp:56
size_type size() const noexcept final
Definition set.hpp:106
std_type & as_std_set()
Definition set.hpp:91
Pred compare_type
Definition set.hpp:57
abstract_iterator * do_find(void *memory, const abstract_key_type &aKey) final
Definition set.hpp:129
size_type max_size() const noexcept final
Definition set.hpp:107
const std_type & as_std_set() const
Definition set.hpp:90
abstract_type::size_type size_type
Definition set.hpp:63
std_type to_std_set() const
Definition set.hpp:92
abstract_type::abstract_iterator abstract_iterator
Definition set.hpp:68
mutable_set< value_type, compare_type, allocator_type > std_type
Definition set.hpp:59
container::const_iterator< value_type, typename std_type::const_iterator > container_const_iterator
Definition set.hpp:64
abstract_type::abstract_const_iterator abstract_const_iterator
Definition set.hpp:67
abstract_type::iterator iterator
Definition set.hpp:71
abstract_const_iterator * do_find(void *memory, const abstract_key_type &aKey) const final
Definition set.hpp:128
abstract_t< T > abstract_key_type
Definition set.hpp:53
i_set< abstract_t< T > > abstract_type
Definition set.hpp:52
constexpr std::partial_ordering operator<=>(const self_type &that) const noexcept
Definition set.hpp:99
Alloc allocator_type
Definition set.hpp:58
set(InputIter aFirst, InputIter aLast)
Definition set.hpp:85
constexpr bool operator==(const self_type &that) const noexcept
Definition set.hpp:95
abstract_type::const_iterator const_iterator
Definition set.hpp:70
container::iterator< value_type, typename std_type::iterator, typename std_type::const_iterator > container_iterator
Definition set.hpp:65
set(const abstract_container &aOther)
Definition set.hpp:77
set(std::initializer_list< value_type > aElements)
Definition set.hpp:81
abstract_iterator * do_insert(void *memory, const abstract_value_type &aValue) final
Definition set.hpp:127
abstract_t< T > abstract_value_type
Definition set.hpp:54
T key_type
Definition set.hpp:55
typename detail::abstract_type< T >::type abstract_t
Definition neolib.hpp:178