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
29 #include "generic/static.h"
31 /// \brief A single-value container, which can either be empty or full.
32 template<typename Type>
37 Single() : m_value( 0 ){
42 Type* insert( const Type& other ){
43 m_value = new Type( other );
51 //ASSERT_MESSAGE(!empty(), "Single: must be initialised before being accessed");
54 const Type& get() const {
55 //ASSERT_MESSAGE(!empty(), "Single: must be initialised before being accessed");
61 /// \brief An adaptor to make std::list into a Unique Sequence - which cannot contain the same value more than once.
62 /// \param Value Uniquely identifies itself. Must provide a copy-constructor and an equality operator.
63 template<typename Value>
66 typedef typename std::list<Value> Values;
69 typedef typename Values::iterator iterator;
70 typedef typename Values::const_iterator const_iterator;
71 typedef typename Values::reverse_iterator reverse_iterator;
72 typedef typename Values::const_reverse_iterator const_reverse_iterator;
75 return m_values.begin();
77 const_iterator begin() const {
78 return m_values.begin();
81 return m_values.end();
83 const_iterator end() const {
84 return m_values.end();
86 reverse_iterator rbegin(){
87 return m_values.rbegin();
89 const_reverse_iterator rbegin() const {
90 return m_values.rbegin();
92 reverse_iterator rend(){
93 return m_values.rend();
95 const_reverse_iterator rend() const {
96 return m_values.rend();
100 return m_values.empty();
102 std::size_t size() const {
103 return m_values.size();
109 void swap( UnsortedSet& other ){
110 std::swap( m_values, other.m_values );
112 iterator insert( const Value& value ){
113 ASSERT_MESSAGE( find( value ) == end(), "UnsortedSet::insert: already added" );
114 m_values.push_back( value );
117 void erase( const Value& value ){
118 iterator i = find( value );
119 ASSERT_MESSAGE( i != end(), "UnsortedSet::erase: not found" );
122 iterator find( const Value& value ){
123 return std::find( begin(), end(), value );
129 /// \brief Swaps the values of \p self and \p other.
130 /// Overloads std::swap.
131 template<typename Value>
132 inline void swap( UnsortedSet<Value>& self, UnsortedSet<Value>& other ){
137 /// An adaptor to make std::list into a Unique Associative Sequence - which cannot contain the same value more than once.
138 /// Key: Uniquely identifies a value. Must provide a copy-constructor and an equality operator.
139 /// Value: Must provide a copy-constructor.
140 template<typename Key, typename Value>
143 typedef typename std::list< std::pair<Key, Value> > Values;
146 typedef typename Values::value_type value_type;
147 typedef typename Values::iterator iterator;
148 typedef typename Values::const_iterator const_iterator;
151 return m_values.begin();
153 const_iterator begin() const {
154 return m_values.begin();
157 return m_values.end();
159 const_iterator end() const {
160 return m_values.end();
164 return m_values.empty();
166 std::size_t size() const {
167 return m_values.size();
173 iterator insert( const value_type& value ){
174 ASSERT_MESSAGE( find( value.first ) == end(), "UnsortedMap::insert: already added" );
175 m_values.push_back( value );
176 return --m_values.end();
178 void erase( const Key& key ){
179 iterator i = find( key );
180 ASSERT_MESSAGE( i != end(), "UnsortedMap::erase: not found" );
183 void erase( iterator i ){
186 iterator find( const Key& key ){
187 for ( iterator i = m_values.begin(); i != m_values.end(); ++i )
189 if ( ( *i ).first == key ) {
193 return m_values.end();
195 const_iterator find( const Key& key ) const {
196 for ( const_iterator i = m_values.begin(); i != m_values.end(); ++i )
198 if ( ( *i ).first == key ) {
202 return m_values.end();
205 Value& operator[]( const Key& key ){
206 iterator i = find( key );
208 return ( *i ).second;
211 m_values.push_back( Values::value_type( key, Value() ) );
212 return m_values.back().second;
216 /// An adaptor to assert when duplicate values are added, or non-existent values removed from a std::set.
217 template<typename Value>
220 typedef std::set<Value> Values;
223 typedef typename Values::iterator iterator;
224 typedef typename Values::const_iterator const_iterator;
225 typedef typename Values::reverse_iterator reverse_iterator;
226 typedef typename Values::const_reverse_iterator const_reverse_iterator;
230 return m_values.begin();
232 const_iterator begin() const {
233 return m_values.begin();
236 return m_values.end();
238 const_iterator end() const {
239 return m_values.end();
241 reverse_iterator rbegin(){
242 return m_values.rbegin();
244 const_reverse_iterator rbegin() const {
245 return m_values.rbegin();
247 reverse_iterator rend(){
248 return m_values.rend();
250 const_reverse_iterator rend() const {
251 return m_values.rend();
255 return m_values.empty();
257 std::size_t size() const {
258 return m_values.size();
264 void swap( UniqueSet& other ){
265 std::swap( m_values, other.m_values );
267 iterator insert( const Value& value ){
268 std::pair<iterator, bool> result = m_values.insert( value );
269 ASSERT_MESSAGE( result.second, "UniqueSet::insert: already added" );
272 void erase( const Value& value ){
273 iterator i = find( value );
274 ASSERT_MESSAGE( i != end(), "UniqueSet::erase: not found" );
277 iterator find( const Value& value ){
278 return std::find( begin(), end(), value );
284 /// \brief Swaps the values of \p self and \p other.
285 /// Overloads std::swap.
286 template<typename Value>
287 inline void swap( UniqueSet<Value>& self, UniqueSet<Value>& other ){
292 template<typename Type>
298 ReferencePair() : m_first( 0 ), m_second( 0 ){
300 void attach( Type& t ){
301 ASSERT_MESSAGE( m_first == 0 || m_second == 0, "ReferencePair::insert: pointer already exists" );
302 if ( m_first == 0 ) {
305 else if ( m_second == 0 ) {
309 void detach( Type& t ){
310 ASSERT_MESSAGE( m_first == &t || m_second == &t, "ReferencePair::erase: pointer not found" );
311 if ( m_first == &t ) {
314 else if ( m_second == &t ) {
318 template<typename Functor>
319 void forEach( const Functor& functor ){
320 if ( m_second != 0 ) {
321 functor( *m_second );
323 if ( m_first != 0 ) {