container.h

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2001-2006, William Joseph.
00003 All Rights Reserved.
00004 
00005 This file is part of GtkRadiant.
00006 
00007 GtkRadiant is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 GtkRadiant is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with GtkRadiant; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 */
00021 
00022 #if !defined(INCLUDED_CONTAINER_CONTAINER_H)
00023 #define INCLUDED_CONTAINER_CONTAINER_H
00024 
00025 #include <list>
00026 #include <set>
00027 
00028 #include "generic/static.h"
00029 
00031 template<typename Type>
00032 class Single {
00033     Type* m_value;
00034 public:
00035     Single() : m_value(0) {
00036     }
00037     bool empty() {
00038         return m_value == 0;
00039     }
00040     Type* insert(const Type& other) {
00041         m_value = new Type(other);
00042         return m_value;
00043     }
00044     void clear() {
00045         delete m_value;
00046         m_value = 0;
00047     }
00048     Type& get() {
00049         //ASSERT_MESSAGE(!empty(), "Single: must be initialised before being accessed");
00050         return *m_value;
00051     }
00052     const Type& get() const {
00053         //ASSERT_MESSAGE(!empty(), "Single: must be initialised before being accessed");
00054         return *m_value;
00055     }
00056 };
00057 
00058 
00061 template<typename Value>
00062 class UnsortedSet {
00063     typedef typename std::list<Value> Values;
00064     Values m_values;
00065 public:
00066     typedef typename Values::iterator iterator;
00067     typedef typename Values::const_iterator const_iterator;
00068     typedef typename Values::reverse_iterator reverse_iterator;
00069     typedef typename Values::const_reverse_iterator const_reverse_iterator;
00070 
00071     iterator begin() {
00072         return m_values.begin();
00073     }
00074     const_iterator begin() const {
00075         return m_values.begin();
00076     }
00077     iterator end() {
00078         return m_values.end();
00079     }
00080     const_iterator end() const {
00081         return m_values.end();
00082     }
00083     reverse_iterator rbegin() {
00084         return m_values.rbegin();
00085     }
00086     const_reverse_iterator rbegin() const {
00087         return m_values.rbegin();
00088     }
00089     reverse_iterator rend() {
00090         return m_values.rend();
00091     }
00092     const_reverse_iterator rend() const {
00093         return m_values.rend();
00094     }
00095 
00096     bool empty() const {
00097         return m_values.empty();
00098     }
00099     std::size_t size() const {
00100         return m_values.size();
00101     }
00102     void clear() {
00103         m_values.clear();
00104     }
00105 
00106     void swap(UnsortedSet& other) {
00107         std::swap(m_values, other.m_values);
00108     }
00109     iterator insert(const Value& value) {
00110         ASSERT_MESSAGE(find(value) == end(), "UnsortedSet::insert: already added");
00111         m_values.push_back(value);
00112         return --end();
00113     }
00114     void erase(const Value& value) {
00115         iterator i = find(value);
00116         ASSERT_MESSAGE(i != end(), "UnsortedSet::erase: not found");
00117         m_values.erase(i);
00118     }
00119     iterator find(const Value& value) {
00120         return std::find(begin(), end(), value);
00121     }
00122 };
00123 
00124 namespace std {
00127 template<typename Value>
00128 inline void swap(UnsortedSet<Value>& self, UnsortedSet<Value>& other) {
00129     self.swap(other);
00130 }
00131 }
00132 
00136 template<typename Key, typename Value>
00137 class UnsortedMap {
00138     typedef typename std::list< std::pair<Key, Value> > Values;
00139     Values m_values;
00140 public:
00141     typedef typename Values::value_type value_type;
00142     typedef typename Values::iterator iterator;
00143     typedef typename Values::const_iterator const_iterator;
00144 
00145     iterator begin() {
00146         return m_values.begin();
00147     }
00148     const_iterator begin() const {
00149         return m_values.begin();
00150     }
00151     iterator end() {
00152         return m_values.end();
00153     }
00154     const_iterator end() const {
00155         return m_values.end();
00156     }
00157 
00158     bool empty() const {
00159         return m_values.empty();
00160     }
00161     std::size_t size() const {
00162         return m_values.size();
00163     }
00164     void clear() {
00165         m_values.clear();
00166     }
00167 
00168     iterator insert(const value_type& value) {
00169         ASSERT_MESSAGE(find(value.first) == end(), "UnsortedMap::insert: already added");
00170         m_values.push_back(value);
00171         return --m_values.end();
00172     }
00173     void erase(const Key& key) {
00174         iterator i = find(key);
00175         ASSERT_MESSAGE(i != end(), "UnsortedMap::erase: not found");
00176         erase(i);
00177     }
00178     void erase(iterator i) {
00179         m_values.erase(i);
00180     }
00181     iterator find(const Key& key) {
00182         for (iterator i = m_values.begin(); i != m_values.end(); ++i) {
00183             if ((*i).first == key) {
00184                 return i;
00185             }
00186         }
00187         return m_values.end();
00188     }
00189     const_iterator find(const Key& key) const {
00190         for (const_iterator i = m_values.begin(); i != m_values.end(); ++i) {
00191             if ((*i).first == key) {
00192                 return i;
00193             }
00194         }
00195         return m_values.end();
00196     }
00197 
00198     Value& operator[](const Key& key) {
00199         iterator i = find(key);
00200         if (i != end()) {
00201             return (*i).second;
00202         }
00203 
00204         m_values.push_back(Values::value_type(key, Value()));
00205         return m_values.back().second;
00206     }
00207 };
00208 
00210 template<typename Value>
00211 class UniqueSet {
00212     typedef std::set<Value> Values;
00213     Values m_values;
00214 public:
00215     typedef typename Values::iterator iterator;
00216     typedef typename Values::const_iterator const_iterator;
00217     typedef typename Values::reverse_iterator reverse_iterator;
00218     typedef typename Values::const_reverse_iterator const_reverse_iterator;
00219 
00220 
00221     iterator begin() {
00222         return m_values.begin();
00223     }
00224     const_iterator begin() const {
00225         return m_values.begin();
00226     }
00227     iterator end() {
00228         return m_values.end();
00229     }
00230     const_iterator end() const {
00231         return m_values.end();
00232     }
00233     reverse_iterator rbegin() {
00234         return m_values.rbegin();
00235     }
00236     const_reverse_iterator rbegin() const {
00237         return m_values.rbegin();
00238     }
00239     reverse_iterator rend() {
00240         return m_values.rend();
00241     }
00242     const_reverse_iterator rend() const {
00243         return m_values.rend();
00244     }
00245 
00246     bool empty() const {
00247         return m_values.empty();
00248     }
00249     std::size_t size() const {
00250         return m_values.size();
00251     }
00252     void clear() {
00253         m_values.clear();
00254     }
00255 
00256     void swap(UniqueSet& other) {
00257         std::swap(m_values, other.m_values);
00258     }
00259     iterator insert(const Value& value) {
00260         std::pair<iterator, bool> result = m_values.insert(value);
00261         ASSERT_MESSAGE(result.second, "UniqueSet::insert: already added");
00262         return result.first;
00263     }
00264     void erase(const Value& value) {
00265         iterator i = find(value);
00266         ASSERT_MESSAGE(i != end(), "UniqueSet::erase: not found");
00267         m_values.erase(i);
00268     }
00269     iterator find(const Value& value) {
00270         return std::find(begin(), end(), value);
00271     }
00272 };
00273 
00274 namespace std {
00277 template<typename Value>
00278 inline void swap(UniqueSet<Value>& self, UniqueSet<Value>& other) {
00279     self.swap(other);
00280 }
00281 }
00282 
00283 template<typename Type>
00284 class ReferencePair {
00285     Type* m_first;
00286     Type* m_second;
00287 public:
00288     ReferencePair() : m_first(0), m_second(0) {
00289     }
00290     void attach(Type& t) {
00291         ASSERT_MESSAGE(m_first == 0 || m_second == 0, "ReferencePair::insert: pointer already exists");
00292         if (m_first == 0) {
00293             m_first = &t;
00294         } else if (m_second == 0) {
00295             m_second = &t;
00296         }
00297     }
00298     void detach(Type& t) {
00299         ASSERT_MESSAGE(m_first == &t || m_second == &t, "ReferencePair::erase: pointer not found");
00300         if (m_first == &t) {
00301             m_first = 0;
00302         } else if (m_second == &t) {
00303             m_second = 0;
00304         }
00305     }
00306     template<typename Functor>
00307     void forEach(const Functor& functor) {
00308         if (m_second != 0) {
00309             functor(*m_second);
00310         }
00311         if (m_first != 0) {
00312             functor(*m_first);
00313         }
00314     }
00315 };
00316 
00317 
00318 #endif

Generated by  doxygen 1.6.2