2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_CONTAINER_CONTAINER_H)
23 #define INCLUDED_CONTAINER_CONTAINER_H
28 #include "generic/static.h"
30 /// \brief A single-value container, which can either be empty or full.
31 template<typename Type>
43 Type* insert(const Type& other)
45 m_value = new Type(other);
55 //ASSERT_MESSAGE(!empty(), "Single: must be initialised before being accessed");
58 const Type& get() const
60 //ASSERT_MESSAGE(!empty(), "Single: must be initialised before being accessed");
66 /// \brief An adaptor to make std::list into a Unique Sequence - which cannot contain the same value more than once.
67 /// \param Value Uniquely identifies itself. Must provide a copy-constructor and an equality operator.
68 template<typename Value>
71 typedef typename std::list<Value> Values;
74 typedef typename Values::iterator iterator;
75 typedef typename Values::const_iterator const_iterator;
76 typedef typename Values::reverse_iterator reverse_iterator;
77 typedef typename Values::const_reverse_iterator const_reverse_iterator;
81 return m_values.begin();
83 const_iterator begin() const
85 return m_values.begin();
89 return m_values.end();
91 const_iterator end() const
93 return m_values.end();
95 reverse_iterator rbegin()
97 return m_values.rbegin();
99 const_reverse_iterator rbegin() const
101 return m_values.rbegin();
103 reverse_iterator rend()
105 return m_values.rend();
107 const_reverse_iterator rend() const
109 return m_values.rend();
114 return m_values.empty();
116 std::size_t size() const
118 return m_values.size();
125 void swap(UnsortedSet& other)
127 std::swap(m_values, other.m_values);
129 iterator insert(const Value& value)
131 ASSERT_MESSAGE(find(value) == end(), "UnsortedSet::insert: already added");
132 m_values.push_back(value);
135 void erase(const Value& value)
137 iterator i = find(value);
138 ASSERT_MESSAGE(i != end(), "UnsortedSet::erase: not found");
141 iterator find(const Value& value)
143 return std::find(begin(), end(), value);
149 /// \brief Swaps the values of \p self and \p other.
150 /// Overloads std::swap.
151 template<typename Value>
152 inline void swap(UnsortedSet<Value>& self, UnsortedSet<Value>& other)
158 /// An adaptor to make std::list into a Unique Associative Sequence - which cannot contain the same value more than once.
159 /// Key: Uniquely identifies a value. Must provide a copy-constructor and an equality operator.
160 /// Value: Must provide a copy-constructor.
161 template<typename Key, typename Value>
164 typedef typename std::list< std::pair<Key, Value> > Values;
167 typedef typename Values::value_type value_type;
168 typedef typename Values::iterator iterator;
169 typedef typename Values::const_iterator const_iterator;
173 return m_values.begin();
175 const_iterator begin() const
177 return m_values.begin();
181 return m_values.end();
183 const_iterator end() const
185 return m_values.end();
190 return m_values.empty();
192 std::size_t size() const
194 return m_values.size();
201 iterator insert(const value_type& value)
203 ASSERT_MESSAGE(find(value.first) == end(), "UnsortedMap::insert: already added");
204 m_values.push_back(value);
205 return --m_values.end();
207 void erase(const Key& key)
209 iterator i = find(key);
210 ASSERT_MESSAGE(i != end(), "UnsortedMap::erase: not found");
213 void erase(iterator i)
217 iterator find(const Key& key)
219 for(iterator i = m_values.begin(); i != m_values.end(); ++i)
221 if((*i).first == key)
226 return m_values.end();
228 const_iterator find(const Key& key) const
230 for(const_iterator i = m_values.begin(); i != m_values.end(); ++i)
232 if((*i).first == key)
237 return m_values.end();
240 Value& operator[](const Key& key)
242 iterator i = find(key);
248 m_values.push_back(Values::value_type(key, Value()));
249 return m_values.back().second;
253 /// An adaptor to assert when duplicate values are added, or non-existent values removed from a std::set.
254 template<typename Value>
257 typedef std::set<Value> Values;
260 typedef typename Values::iterator iterator;
261 typedef typename Values::const_iterator const_iterator;
262 typedef typename Values::reverse_iterator reverse_iterator;
263 typedef typename Values::const_reverse_iterator const_reverse_iterator;
268 return m_values.begin();
270 const_iterator begin() const
272 return m_values.begin();
276 return m_values.end();
278 const_iterator end() const
280 return m_values.end();
282 reverse_iterator rbegin()
284 return m_values.rbegin();
286 const_reverse_iterator rbegin() const
288 return m_values.rbegin();
290 reverse_iterator rend()
292 return m_values.rend();
294 const_reverse_iterator rend() const
296 return m_values.rend();
301 return m_values.empty();
303 std::size_t size() const
305 return m_values.size();
312 void swap(UniqueSet& other)
314 std::swap(m_values, other.m_values);
316 iterator insert(const Value& value)
318 std::pair<iterator, bool> result = m_values.insert(value);
319 ASSERT_MESSAGE(result.second, "UniqueSet::insert: already added");
322 void erase(const Value& value)
324 iterator i = find(value);
325 ASSERT_MESSAGE(i != end(), "UniqueSet::erase: not found");
328 iterator find(const Value& value)
330 return std::find(begin(), end(), value);
336 /// \brief Swaps the values of \p self and \p other.
337 /// Overloads std::swap.
338 template<typename Value>
339 inline void swap(UniqueSet<Value>& self, UniqueSet<Value>& other)
345 template<typename Type>
351 ReferencePair() : m_first(0), m_second(0)
356 ASSERT_MESSAGE(m_first == 0 || m_second == 0, "ReferencePair::insert: pointer already exists");
361 else if(m_second == 0)
368 ASSERT_MESSAGE(m_first == &t || m_second == &t, "ReferencePair::erase: pointer not found");
373 else if(m_second == &t)
378 template<typename Functor>
379 void forEach(const Functor& functor)