00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef LEMON_VECTOR_MAP_H
00018 #define LEMON_VECTOR_MAP_H
00019
00020 #include <vector>
00021 #include <algorithm>
00022
00023 #include <lemon/utility.h>
00024 #include <lemon/bits/map_iterator.h>
00025 #include <lemon/bits/alteration_notifier.h>
00026
00030
00031 namespace lemon {
00032
00035
00047
00048
00049 template <
00050 typename _Graph,
00051 typename _Item,
00052 typename _Value
00053 >
00054 class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
00055 public:
00056
00058 typedef _Graph Graph;
00060 typedef _Item Key;
00062 typedef AlterationNotifier<_Item> Registry;
00064 typedef _Value Value;
00065
00067 typedef VectorMap Map;
00069 typedef typename Registry::ObserverBase Parent;
00070
00071 private:
00072
00074 typedef std::vector<Value> Container;
00075
00076 public:
00077
00079 typedef typename Container::reference Reference;
00081 typedef typename Container::pointer Pointer;
00082
00084 typedef const Value ConstValue;
00086 typedef typename Container::const_reference ConstReference;
00088 typedef typename Container::const_pointer ConstPointer;
00089
00090 typedef True FullTypeTag;
00091
00093
00096
00097 VectorMap(const Graph& _g) : graph(&_g) {
00098 attach(_g.getNotifier(_Item()));
00099 build();
00100 }
00101
00103
00106
00107 VectorMap(const Graph& _g, const Value& _v) : graph(&_g) {
00108 attach(_g.getNotifier(_Item()));
00109 container.resize(graph->maxId(_Item()) + 1, _v);
00110 }
00111
00112 VectorMap(const VectorMap& _copy)
00113 : Parent(), graph(_copy.getGraph()) {
00114 if (_copy.attached()) {
00115 attach(*_copy.getRegistry());
00116 container = _copy.container;
00117 }
00118 }
00119
00120 using Parent::attach;
00121 using Parent::detach;
00122 using Parent::attached;
00123
00126 VectorMap& operator=(const VectorMap& copy) {
00127 if (© == this) return *this;
00128
00129 if (graph != copy.graph) {
00130 if (attached()) {
00131 detach();
00132 }
00133 if (copy.attached()) {
00134 attach(*copy.getRegistry());
00135 }
00136 }
00137 container = copy.container;
00138
00139 return *this;
00140 }
00141
00142
00143 virtual ~VectorMap() {
00144 if (attached()) {
00145 detach();
00146 }
00147 }
00148
00149 const Graph* getGraph() const {
00150 return graph;
00151 }
00152
00154
00157
00158 Reference operator[](const Key& key) {
00159 return container[graph->id(key)];
00160 }
00161
00163
00166
00167 ConstReference operator[](const Key& key) const {
00168 return container[graph->id(key)];
00169 }
00170
00171
00173
00176
00177 void set(const Key& key, const Value& value) {
00178 (*this)[key] = value;
00179 }
00180
00182
00185
00186 void add(const Key& key) {
00187 int id = graph->id(key);
00188 if (id >= (int)container.size()) {
00189 container.resize(id + 1);
00190 }
00191 }
00192
00194
00197 void erase(const Key&) {}
00198
00200
00203
00204 void build() {
00205 container.resize(graph->maxId(_Item()) + 1);
00206 }
00207
00209
00212 void clear() {
00213 container.clear();
00214 }
00215
00216 private:
00217
00218 Container container;
00219 const Graph *graph;
00220
00221 };
00222
00223
00224 template <typename _Base>
00225 class VectorMappableGraphExtender : public _Base {
00226 public:
00227
00228 typedef VectorMappableGraphExtender<_Base> Graph;
00229 typedef _Base Parent;
00230
00231 typedef typename Parent::Node Node;
00232 typedef typename Parent::NodeIt NodeIt;
00233 typedef typename Parent::NodeIdMap NodeIdMap;
00234 typedef typename Parent::NodeNotifier NodeObserverRegistry;
00235
00236 typedef typename Parent::Edge Edge;
00237 typedef typename Parent::EdgeIt EdgeIt;
00238 typedef typename Parent::EdgeIdMap EdgeIdMap;
00239 typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
00240
00241
00242 template <typename _Value>
00243 class NodeMap :
00244 public IterableMapExtender<VectorMap<Graph, Node, _Value> > {
00245 public:
00246 typedef VectorMappableGraphExtender<_Base> Graph;
00247
00248 typedef typename Graph::Node Node;
00249
00250 typedef IterableMapExtender<VectorMap<Graph, Node, _Value> > Parent;
00251
00252
00253 typedef typename Parent::Value Value;
00254
00255 NodeMap(const Graph& g)
00256 : Parent(g) {}
00257 NodeMap(const Graph& g, const Value& v)
00258 : Parent(g, v) {}
00259
00260 };
00261
00262 template <typename _Value>
00263 class EdgeMap
00264 : public IterableMapExtender<VectorMap<Graph, Edge, _Value> > {
00265 public:
00266 typedef VectorMappableGraphExtender<_Base> Graph;
00267
00268 typedef typename Graph::Edge Edge;
00269
00270 typedef IterableMapExtender<VectorMap<Graph, Edge, _Value> > Parent;
00271
00272
00273 typedef typename Parent::Value Value;
00274
00275 EdgeMap(const Graph& g)
00276 : Parent(g) {}
00277 EdgeMap(const Graph& g, const Value& v)
00278 : Parent(g, v) {}
00279
00280 };
00281
00282 };
00283
00285
00286 }
00287
00288 #endif