neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
mutable_set.hpp
Go to the documentation of this file.
1// mutable_set.hpp v1.2
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>
39#include <type_traits>
40#include <map>
41#include <memory>
42
43namespace neolib
44{
45 template<typename T, typename U = void>
46 struct crack_key { typedef T key_type; };
47 template<typename T>
48 struct crack_key<T, typename std::void_t<typename T::key_type>> { typedef typename T::key_type key_type; };
49
50 template <typename Container>
51 class mutable_base : public Container
52 {
53 public:
54 typedef Container std_type;
55 typedef typename std_type::key_type key_type;
56 typedef typename std_type::mapped_type value_type;
57 public:
58 class iterator : public std_type::iterator
59 {
60 public:
64 public:
66 iterator(typename std_type::iterator aIterator) : std_type::iterator(aIterator) {}
67 pointer operator->() const { return &std_type::iterator::operator*().second; }
68 reference operator*() const { return std_type::iterator::operator*().second; }
69 };
70 class const_iterator : public std_type::const_iterator
71 {
72 public:
74 typedef const value_type* pointer;
75 typedef const value_type& reference;
76 public:
78 const_iterator(typename std_type::const_iterator aIterator) : std_type::const_iterator(aIterator) {}
79 const_iterator(typename std_type::iterator aIterator) : std_type::const_iterator(aIterator) {}
80 pointer operator->() const { return &std_type::const_iterator::operator*().second; }
81 reference operator*() const { return std_type::const_iterator::operator*().second; }
82 };
83 class reverse_iterator : public std_type::reverse_iterator
84 {
85 public:
89 public:
91 reverse_iterator(typename std_type::reverse_iterator aIterator) : std_type::reverse_iterator(aIterator) {}
92 pointer operator->() const { return &std_type::reverse_iterator::operator*().second; }
93 reference operator*() const { return std_type::reverse_iterator::operator*().second; }
94 };
95 class const_reverse_iterator : public std_type::const_reverse_iterator
96 {
97 public:
99 typedef const value_type* pointer;
100 typedef const value_type& reference;
101 public:
103 const_reverse_iterator(typename std_type::const_reverse_iterator aIterator) : std_type::const_reverse_iterator(aIterator) {}
104 const_reverse_iterator(typename std_type::reverse_iterator aIterator) : std_type::const_reverse_iterator(aIterator) {}
105 pointer operator->() const { return &std_type::const_reverse_iterator::operator*().second; }
106 reference operator*() const { return std_type::const_reverse_iterator::operator*().second; }
107 };
108 public:
109 const_iterator cbegin() const { return std_type::cbegin(); }
110 const_iterator begin() const { return std_type::begin(); }
111 iterator begin() { return std_type::begin(); }
112 const_iterator cend() const { return std_type::cend(); }
113 const_iterator end() const { return std_type::end(); }
114 iterator end() { return std_type::end(); }
115 const_reverse_iterator crbegin() const { return std_type::crbegin(); }
116 const_reverse_iterator rbegin() const { return std_type::rbegin(); }
117 reverse_iterator rbegin() { return std_type::rbegin(); }
118 const_reverse_iterator crend() const { return std_type::crend(); }
119 const_reverse_iterator rend() const { return std_type::rend(); }
120 reverse_iterator rend() { return std_type::rend(); }
121 iterator find(const key_type& aKey) { return std_type::find(aKey); }
122 const_iterator find(const key_type& aKey) const { return std_type::find(aKey); }
123 };
124
125 template <typename T, typename Pr = std::less<typename crack_key<T>::key_type>, typename Alloc = std::allocator<std::pair<typename crack_key<T>::key_type const, T> > >
126 class mutable_set : public mutable_base<std::map<typename crack_key<T>::key_type, T, Pr, typename std::allocator_traits<Alloc>::template rebind_alloc<std::pair<typename crack_key<T>::key_type const, T>>>>
127 {
128 typedef mutable_base<std::map<typename crack_key<T>::key_type, T, Pr, typename std::allocator_traits<Alloc>::template rebind_alloc<std::pair<typename crack_key<T>::key_type const, T>>>> base_type;
129 public:
131 using typename base_type::const_iterator;
132 using typename base_type::iterator;
134 using typename base_type::reverse_iterator;
135 public:
137 {
138 }
139 mutable_set(std::initializer_list<T> aElements)
140 {
141 for (auto& e : aElements)
142 insert(e);
143 }
144 template <typename InputIter>
145 mutable_set(InputIter aFirst, InputIter aLast)
146 {
147 for (auto e = aFirst; e != aLast; ++e)
148 insert(*e);
149 }
150 public:
151 iterator insert(const typename base_type::value_type& aValue)
152 {
153 return iterator(base_type::insert(std::make_pair(static_cast<key_type>(aValue), aValue)).first);
154 }
156 iterator find(const typename base_type::value_type& aValue)
157 {
158 return iterator(base_type::find(static_cast<key_type>(aValue)));
159 }
160 const_iterator find(const typename base_type::value_type& aValue) const
161 {
162 return const_iterator(base_type::find(static_cast<key_type>(aValue)));
163 }
164 };
165
166 template <typename T, typename Pr = std::less<typename crack_key<T>::key_type>, typename Alloc = std::allocator<std::pair<typename crack_key<T>::key_type const, T> > >
167 class mutable_multiset : public mutable_base<std::multimap<typename crack_key<T>::key_type, T, Pr, typename std::allocator_traits<Alloc>::template rebind_alloc<std::pair<typename crack_key<T>::key_type const, T>>>>
168 {
169 typedef mutable_base<std::multimap<typename crack_key<T>::key_type, T, Pr, typename std::allocator_traits<Alloc>::template rebind_alloc<std::pair<typename crack_key<T>::key_type const, T>>>> base_type;
170 public:
172 using typename base_type::const_iterator;
173 using typename base_type::iterator;
175 using typename base_type::reverse_iterator;
176 public:
178 {
179 }
180 mutable_multiset(std::initializer_list<T> aElements)
181 {
182 for (auto& e : aElements)
183 insert(e);
184 }
185 template <typename InputIter>
186 mutable_multiset(InputIter aFirst, InputIter aLast)
187 {
188 for (auto e = aFirst; e != aLast; ++e)
189 insert(*e);
190 }
191 public:
192 iterator insert(const typename base_type::value_type& aValue)
193 {
194 return iterator(base_type::insert(std::make_pair(static_cast<key_type>(aValue), aValue)));
195 }
197 iterator find(const typename base_type::value_type& aValue)
198 {
199 return iterator(base_type::find(static_cast<key_type>(aValue)));
200 }
201 const_iterator find(const typename base_type::value_type& aValue) const
202 {
203 return const_iterator(base_type::find(static_cast<key_type>(aValue)));
204 }
205 };
206}
const_iterator(typename std_type::const_iterator aIterator)
const_iterator(typename std_type::iterator aIterator)
mutable_base::value_type value_type
const_reverse_iterator(typename std_type::reverse_iterator aIterator)
const_reverse_iterator(typename std_type::const_reverse_iterator aIterator)
mutable_base::value_type value_type
iterator(typename std_type::iterator aIterator)
mutable_base::value_type value_type
reverse_iterator(typename std_type::reverse_iterator aIterator)
const_iterator find(const key_type &aKey) const
reverse_iterator rbegin()
iterator find(const key_type &aKey)
const_reverse_iterator rend() const
const_reverse_iterator rbegin() const
std_type::mapped_type value_type
const_reverse_iterator crend() const
const_iterator cbegin() const
const_iterator end() const
std_type::key_type key_type
reverse_iterator rend()
const_iterator begin() const
const_reverse_iterator crbegin() const
const_iterator cend() const
iterator insert(const typename base_type::value_type &aValue)
mutable_multiset(std::initializer_list< T > aElements)
iterator find(const typename base_type::value_type &aValue)
const_iterator find(const typename base_type::value_type &aValue) const
mutable_multiset(InputIter aFirst, InputIter aLast)
crack_key< T >::key_type key_type
iterator find(const typename base_type::value_type &aValue)
mutable_set(std::initializer_list< T > aElements)
iterator insert(const typename base_type::value_type &aValue)
mutable_set(InputIter aFirst, InputIter aLast)
crack_key< T >::key_type key_type
const_iterator find(const typename base_type::value_type &aValue) const
Definition plf_hive.h:79