vector_map.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  *
00003  * This file is a part of LEMON, a generic C++ optimization library
00004  *
00005  * Copyright (C) 2003-2006
00006  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00007  * (Egervary Research Group on Combinatorial Optimization, EGRES).
00008  *
00009  * Permission to use, modify and distribute this software is granted
00010  * provided that this copyright notice appears in all copies. For
00011  * precise terms see the accompanying LICENSE file.
00012  *
00013  * This software is provided "AS IS" with no warranty of any kind,
00014  * express or implied, and with no claim as to its suitability for any
00015  * purpose.
00016  *
00017  */
00018 
00019 #ifndef LEMON_VECTOR_MAP_H
00020 #define LEMON_VECTOR_MAP_H
00021 
00022 #include <vector>
00023 #include <algorithm>
00024 
00025 #include <lemon/utility.h>
00026 #include <lemon/bits/map_extender.h>
00027 #include <lemon/bits/alteration_notifier.h>
00028 #include <lemon/concept_check.h>
00029 #include <lemon/concept/maps.h>
00030 
00035 
00036 namespace lemon {
00037 
00053         
00054   template <
00055     typename _Graph, 
00056     typename _Item,    
00057     typename _Value
00058     >
00059   class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
00060   private:
00061                 
00063     typedef std::vector<_Value> Container;      
00064 
00065   public:
00066 
00068     typedef _Graph Graph;
00070     typedef True ReferenceMapTag;
00071 
00073     typedef _Item Key;
00075     typedef _Value Value;
00076 
00077     typedef AlterationNotifier<_Item> Registry;
00078 
00080     typedef VectorMap Map;
00082     typedef typename Registry::ObserverBase Parent;
00083 
00085     typedef typename Container::reference Reference;
00087     typedef typename Container::pointer Pointer;
00088 
00090     typedef const Value ConstValue;
00092     typedef typename Container::const_reference ConstReference;
00094     typedef typename Container::const_pointer ConstPointer;
00095 
00096 
00101     VectorMap(const Graph& _g) : graph(&_g) {
00102       attach(_g.getNotifier(_Item()));
00103       build();
00104     }
00105 
00110     VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { 
00111       attach(_g.getNotifier(_Item()));
00112       container.resize(graph->maxId(_Item()) + 1, _v);
00113     }
00114 
00118     VectorMap(const VectorMap& _copy) 
00119       : Parent(), graph(_copy.getGraph()) {
00120       if (_copy.attached()) {
00121         attach(*_copy.getRegistry());
00122         container = _copy.container;
00123       }
00124     }
00125 
00129     virtual ~VectorMap() {
00130       if (attached()) {
00131         detach();
00132       }
00133     }
00134 
00135 
00136   private:
00137 
00138     VectorMap& operator=(const VectorMap&);
00139 
00140   protected:
00141 
00142     using Parent::attach;
00143     using Parent::detach;
00144     using Parent::attached;
00145 
00146     const Graph* getGraph() const {
00147       return graph;
00148     }
00149 
00150   public:
00151 
00156     Reference operator[](const Key& key) {
00157       return container[graph->id(key)];
00158     } 
00159                 
00164     ConstReference operator[](const Key& key) const {
00165       return container[graph->id(key)];
00166     }
00167 
00168 
00172     void set(const Key& key, const Value& value) {
00173       (*this)[key] = value;
00174     }
00175 
00176   protected:
00177 
00182     virtual void add(const Key& key) {
00183       int id = graph->id(key);
00184       if (id >= (int)container.size()) {
00185         container.resize(id + 1);
00186       }
00187     }
00188 
00193     virtual void add(const std::vector<Key>& keys) {
00194       for (int i = 0; i < (int)keys.size(); ++i) {
00195         add(keys[i]);
00196       }
00197     }
00198 
00203     virtual void erase(const Key&) {}
00204 
00209     virtual void erase(const std::vector<Key>&) {}
00210     
00215     virtual void build() { 
00216       container.resize(graph->maxId(_Item()) + 1);
00217     }
00218 
00223     virtual void clear() { 
00224       container.clear();
00225     }
00226     
00227   private:
00228                 
00229     Container container;
00230     const Graph *graph;
00231 
00232   };
00233 
00234 }
00235 
00236 #endif

Generated on Fri Feb 3 18:35:55 2006 for LEMON by  doxygen 1.4.6