vector_map.h

Go to the documentation of this file.
00001 /* -*- C++ -*- 00002 * src/lemon/vector_map.h - Part of LEMON, a generic C++ optimization library 00003 * 00004 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport 00005 * (Egervary Combinatorial Optimization Research Group, EGRES). 00006 * 00007 * Permission to use, modify and distribute this software is granted 00008 * provided that this copyright notice appears in all copies. For 00009 * precise terms see the accompanying LICENSE file. 00010 * 00011 * This software is provided "AS IS" with no warranty of any kind, 00012 * express or implied, and with no claim as to its suitability for any 00013 * purpose. 00014 * 00015 */ 00016 00017 #ifndef LEMON_VECTOR_MAP_H 00018 #define LEMON_VECTOR_MAP_H 00019 00020 #include <vector> 00021 00022 #include <lemon/map_iterator.h> 00023 #include <lemon/map_bits.h> 00024 00028 00029 namespace lemon { 00030 00033 00048 template <typename MapRegistry, typename Value> 00049 class VectorMap : public MapRegistry::MapBase { 00050 template <typename MR, typename T> friend class VectorMap; 00051 public: 00052 00054 typedef typename MapRegistry::Graph Graph; 00056 typedef typename MapRegistry::KeyType KeyType; 00058 typedef typename MapRegistry::KeyIt KeyIt; 00059 00061 typedef VectorMap Map; 00063 typedef typename MapRegistry::MapBase MapBase; 00064 00065 private: 00066 00068 typedef std::vector<Value> Container; 00069 00070 public: 00071 00072 00074 typedef Value ValueType; 00076 typedef typename Container::reference ReferenceType; 00078 typedef typename Container::pointer PointerType; 00079 00081 typedef const Value ConstValueType; 00083 typedef typename Container::const_reference ConstReferenceType; 00085 typedef typename Container::const_pointer ConstPointerType; 00086 00089 VectorMap(const Graph& g, MapRegistry& r) 00090 : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {} 00091 00094 VectorMap(const Graph& g, MapRegistry& r, const Value& v) 00095 : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {} 00096 00099 template <typename TT> 00100 VectorMap(const VectorMap<MapRegistry, TT>& c) 00101 : MapBase(c), container(c.container.size()) { 00102 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { 00103 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it); 00104 container[id] = c.container[id]; 00105 } 00106 } 00107 00110 template <typename TT> 00111 VectorMap& operator=(const VectorMap<MapRegistry, TT>& c) { 00112 if (MapBase::getGraph() != c.getGraph()) { 00113 MapBase::operator=(c); 00114 container.resize(c.container.size()); 00115 } 00116 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { 00117 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it); 00118 container[id] = c.container[id]; 00119 } 00120 return *this; 00121 } 00126 ReferenceType operator[](const KeyType& key) { 00127 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); 00128 return container[id]; 00129 } 00130 00135 ConstReferenceType operator[](const KeyType& key) const { 00136 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); 00137 return container[id]; 00138 } 00139 00143 void set(const KeyType& key, const ValueType& val) { 00144 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); 00145 container[id] = val; 00146 } 00147 00150 void add(const KeyType& key) { 00151 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); 00152 if (id >= (int)container.size()) { 00153 container.resize(id + 1); 00154 } 00155 } 00156 00159 void erase(const KeyType& key) {} 00160 00163 void clear() { 00164 container.clear(); 00165 } 00166 00168 typedef MapIterator<VectorMap> Iterator; 00170 typedef MapConstIterator<VectorMap> ConstIterator; 00171 00174 Iterator begin() { 00175 return Iterator(*this, KeyIt(*MapBase::getGraph())); 00176 } 00177 00180 Iterator end() { 00181 return Iterator(*this, INVALID); 00182 } 00183 00186 ConstIterator begin() const { 00187 return ConstIterator(*this, KeyIt(*MapBase::getGraph())); 00188 } 00189 00192 ConstIterator end() const { 00193 return ConstIterator(*this, INVALID); 00194 } 00195 00197 typedef MapConstKeySet<VectorMap> ConstKeySet; 00198 00200 ConstKeySet keySet() const { 00201 return ConstKeySet(*this); 00202 } 00203 00205 typedef MapConstValueSet<VectorMap> ConstValueSet; 00206 00208 ConstValueSet valueSet() const { 00209 return ConstValueSet(*this); 00210 } 00211 00213 typedef MapValueSet<VectorMap> ValueSet; 00214 00216 ValueSet valueSet() { 00217 return ValueSet(*this); 00218 } 00219 00220 00221 private: 00222 00223 Container container; 00224 00225 public: 00226 // STL compatibility typedefs. 00227 typedef Iterator iterator; 00228 typedef ConstIterator const_iterator; 00229 typedef typename Iterator::PairValueType value_type; 00230 typedef typename Iterator::KeyType key_type; 00231 typedef typename Iterator::ValueType data_type; 00232 typedef typename Iterator::PairReferenceType reference; 00233 typedef typename Iterator::PairPointerType pointer; 00234 typedef typename ConstIterator::PairReferenceType const_reference; 00235 typedef typename ConstIterator::PairPointerType const_pointer; 00236 typedef int difference_type; 00237 }; 00238 00240 00241 } 00242 00243 #endif

Generated on Thu Sep 30 12:18:34 2004 for LEMON by doxygen 1.3.8