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>
36 Single() : m_value( 0 ){
41 Type* insert( const Type& other ){
42 m_value = new Type( other );
50 //ASSERT_MESSAGE(!empty(), "Single: must be initialised before being accessed");
53 const Type& get() const {
54 //ASSERT_MESSAGE(!empty(), "Single: must be initialised before being accessed");
60 /// \brief An adaptor to make std::list into a Unique Sequence - which cannot contain the same value more than once.
61 /// \param Value Uniquely identifies itself. Must provide a copy-constructor and an equality operator.
62 template<typename Value>
65 typedef typename std::list<Value> Values;
68 typedef typename Values::iterator iterator;
69 typedef typename Values::const_iterator const_iterator;
70 typedef typename Values::reverse_iterator reverse_iterator;
71 typedef typename Values::const_reverse_iterator const_reverse_iterator;
74 return m_values.begin();
76 const_iterator begin() const {
77 return m_values.begin();
80 return m_values.end();
82 const_iterator end() const {
83 return m_values.end();
85 reverse_iterator rbegin(){
86 return m_values.rbegin();
88 const_reverse_iterator rbegin() const {
89 return m_values.rbegin();
91 reverse_iterator rend(){
92 return m_values.rend();
94 const_reverse_iterator rend() const {
95 return m_values.rend();
99 return m_values.empty();
101 std::size_t size() const {
102 return m_values.size();
108 void swap( UnsortedSet& other ){
109 std::swap( m_values, other.m_values );
111 iterator insert( const Value& value ){
112 ASSERT_MESSAGE( find( value ) == end(), "UnsortedSet::insert: already added" );
113 m_values.push_back( value );
116 void erase( const Value& value ){
117 iterator i = find( value );
118 ASSERT_MESSAGE( i != end(), "UnsortedSet::erase: not found" );
121 iterator find( const Value& value ){
122 return std::find( begin(), end(), value );
128 /// \brief Swaps the values of \p self and \p other.
129 /// Overloads std::swap.
130 template<typename Value>
131 inline void swap( UnsortedSet<Value>& self, UnsortedSet<Value>& other ){
136 /// An adaptor to make std::list into a Unique Associative Sequence - which cannot contain the same value more than once.
137 /// Key: Uniquely identifies a value. Must provide a copy-constructor and an equality operator.
138 /// Value: Must provide a copy-constructor.
139 template<typename Key, typename Value>
142 typedef typename std::list< std::pair<Key, Value> > Values;
145 typedef typename Values::value_type value_type;
146 typedef typename Values::iterator iterator;
147 typedef typename Values::const_iterator const_iterator;
150 return m_values.begin();
152 const_iterator begin() const {
153 return m_values.begin();
156 return m_values.end();
158 const_iterator end() const {
159 return m_values.end();
163 return m_values.empty();
165 std::size_t size() const {
166 return m_values.size();
172 iterator insert( const value_type& value ){
173 ASSERT_MESSAGE( find( value.first ) == end(), "UnsortedMap::insert: already added" );
174 m_values.push_back( value );
175 return --m_values.end();
177 void erase( const Key& key ){
178 iterator i = find( key );
179 ASSERT_MESSAGE( i != end(), "UnsortedMap::erase: not found" );
182 void erase( iterator i ){
185 iterator find( const Key& key ){
186 for ( iterator i = m_values.begin(); i != m_values.end(); ++i )
188 if ( ( *i ).first == key ) {
192 return m_values.end();
194 const_iterator find( const Key& key ) const {
195 for ( const_iterator i = m_values.begin(); i != m_values.end(); ++i )
197 if ( ( *i ).first == key ) {
201 return m_values.end();
204 Value& operator[]( const Key& key ){
205 iterator i = find( key );
207 return ( *i ).second;
210 m_values.push_back( Values::value_type( key, Value() ) );
211 return m_values.back().second;
215 /// An adaptor to assert when duplicate values are added, or non-existent values removed from a std::set.
216 template<typename Value>
219 typedef std::set<Value> Values;
222 typedef typename Values::iterator iterator;
223 typedef typename Values::const_iterator const_iterator;
224 typedef typename Values::reverse_iterator reverse_iterator;
225 typedef typename Values::const_reverse_iterator const_reverse_iterator;
229 return m_values.begin();
231 const_iterator begin() const {
232 return m_values.begin();
235 return m_values.end();
237 const_iterator end() const {
238 return m_values.end();
240 reverse_iterator rbegin(){
241 return m_values.rbegin();
243 const_reverse_iterator rbegin() const {
244 return m_values.rbegin();
246 reverse_iterator rend(){
247 return m_values.rend();
249 const_reverse_iterator rend() const {
250 return m_values.rend();
254 return m_values.empty();
256 std::size_t size() const {
257 return m_values.size();
263 void swap( UniqueSet& other ){
264 std::swap( m_values, other.m_values );
266 iterator insert( const Value& value ){
267 std::pair<iterator, bool> result = m_values.insert( value );
268 ASSERT_MESSAGE( result.second, "UniqueSet::insert: already added" );
271 void erase( const Value& value ){
272 iterator i = find( value );
273 ASSERT_MESSAGE( i != end(), "UniqueSet::erase: not found" );
276 iterator find( const Value& value ){
277 return std::find( begin(), end(), value );
283 /// \brief Swaps the values of \p self and \p other.
284 /// Overloads std::swap.
285 template<typename Value>
286 inline void swap( UniqueSet<Value>& self, UniqueSet<Value>& other ){
291 template<typename Type>
297 ReferencePair() : m_first( 0 ), m_second( 0 ){
299 void attach( Type& t ){
300 ASSERT_MESSAGE( m_first == 0 || m_second == 0, "ReferencePair::insert: pointer already exists" );
301 if ( m_first == 0 ) {
304 else if ( m_second == 0 ) {
308 void detach( Type& t ){
309 ASSERT_MESSAGE( m_first == &t || m_second == &t, "ReferencePair::erase: pointer not found" );
310 if ( m_first == &t ) {
313 else if ( m_second == &t ) {
317 template<typename Functor>
318 void forEach( const Functor& functor ){
319 if ( m_second != 0 ) {
320 functor( *m_second );
322 if ( m_first != 0 ) {