lemon/bits/array_map.h
changeset 1705 3f63d9db307b
parent 1669 66ae78d29f1e
child 1719 674182524bd9
equal deleted inserted replaced
3:feb81931085a 4:81821d2b5640
    46 	    typename _Value>
    46 	    typename _Value>
    47   class ArrayMap : public AlterationNotifier<_Item>::ObserverBase {
    47   class ArrayMap : public AlterationNotifier<_Item>::ObserverBase {
    48 
    48 
    49     typedef _Item Item;
    49     typedef _Item Item;
    50   public:
    50   public:
       
    51     typedef True AdaptibleTag;
    51 		
    52 		
    52     /// The graph type of the maps. 
    53     /// The graph type of the maps. 
    53     typedef _Graph Graph;
    54     typedef _Graph Graph;
    54     /// The key type of the maps.
    55     /// The key type of the maps.
    55     typedef _Item Key;
    56     typedef _Item Key;
    67     typedef std::allocator<Value> Allocator;
    68     typedef std::allocator<Value> Allocator;
    68 
    69 
    69 
    70 
    70   public:
    71   public:
    71 
    72 
       
    73     /// \brief Graph and Registry initialized map constructor.
       
    74     ///
    72     /// Graph and Registry initialized map constructor.
    75     /// Graph and Registry initialized map constructor.
    73     ArrayMap(const Graph& _g) : graph(&_g) {
    76     ArrayMap(const Graph& _g) : graph(&_g) {
    74       Item it;
    77       Item it;
    75       attach(_g.getNotifier(Item()));
    78       attach(_g.getNotifier(Item()));
    76       allocate_memory();
    79       allocate_memory();
    78 	int id = graph->id(it);;
    81 	int id = graph->id(it);;
    79 	allocator.construct(&(values[id]), Value());
    82 	allocator.construct(&(values[id]), Value());
    80       }								
    83       }								
    81     }
    84     }
    82 
    85 
    83     /// Constructor to use default value to initialize the map. 
    86     /// \brief Constructor to use default value to initialize the map. 
    84 
    87     ///
    85     /// It constrates a map and initialize all of the the map. 
    88     /// It constructs a map and initialize all of the the map. 
    86 
       
    87     ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) {
    89     ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) {
    88       Item it;
    90       Item it;
    89       attach(_g.getNotifier(_Item()));
    91       attach(_g.getNotifier(_Item()));
    90       allocate_memory();
    92       allocate_memory();
    91       for (graph->first(it); it != INVALID; graph->next(it)) {
    93       for (graph->first(it); it != INVALID; graph->next(it)) {
    92 	int id = graph->id(it);;
    94 	int id = graph->id(it);;
    93 	allocator.construct(&(values[id]), _v);
    95 	allocator.construct(&(values[id]), _v);
    94       }								
    96       }								
    95     }
    97     }
    96 
    98 
    97     /// Constructor to copy a map of the same map type.
    99     /// \brief Constructor to copy a map of the same map type.
    98      
   100     ///
       
   101     /// Constructor to copy a map of the same map type.     
    99     ArrayMap(const ArrayMap& copy) : Parent(), graph(copy.graph) {
   102     ArrayMap(const ArrayMap& copy) : Parent(), graph(copy.graph) {
   100       if (copy.attached()) {
   103       if (copy.attached()) {
   101 	attach(*copy.getRegistry());
   104 	attach(*copy.getRegistry());
   102       }
   105       }
   103       capacity = copy.capacity;
   106       capacity = copy.capacity;
   135     }
   138     }
   136 
   139 
   137 
   140 
   138   public:
   141   public:
   139 
   142 
   140     ///The subscript operator. The map can be subscripted by the
   143     /// \brief The subscript operator. 
   141     ///actual keys of the graph. 
   144     ///
   142      
   145     /// The subscript operator. The map can be subscripted by the
       
   146     /// actual keys of the graph. 
   143     Value& operator[](const Key& key) {
   147     Value& operator[](const Key& key) {
   144       int id = graph->id(key);
   148       int id = graph->id(key);
   145       return values[id];
   149       return values[id];
   146     } 
   150     } 
   147 		
   151 		
   148 
   152     /// \brief The const subscript operator.
   149     ///The const subscript operator. The map can be subscripted by the
   153     ///
   150     ///actual keys of the graph. 
   154     /// The const subscript operator. The map can be subscripted by the
   151      
   155     /// actual keys of the graph. 
   152     const Value& operator[](const Key& key) const {
   156     const Value& operator[](const Key& key) const {
   153       int id = graph->id(key);
   157       int id = graph->id(key);
   154       return values[id];
   158       return values[id];
   155     }
   159     }
   156 	
   160 
       
   161     /// \brief Setter function of the map.
       
   162     ///	
   157     /// Setter function of the map. Equivalent with map[key] = val.
   163     /// Setter function of the map. Equivalent with map[key] = val.
   158     /// This is a compatibility feature with the not dereferable maps.
   164     /// This is a compatibility feature with the not dereferable maps.
   159      
       
   160     void set(const Key& key, const Value& val) {
   165     void set(const Key& key, const Value& val) {
   161       (*this)[key] = val;
   166       (*this)[key] = val;
   162     }
   167     }
   163 
   168 
   164   protected:
   169   protected:
   165     
   170 
   166     /// Add a new key to the map. It called by the map registry.
   171     /// Add a new key to the map. It called by the map registry.
   167      
   172          
   168     void add(const Key& key) {
   173     virtual void add(const Key& key) {
   169       int id = graph->id(key);
   174       int id = graph->id(key);
   170       if (id >= capacity) {
   175       if (id >= capacity) {
   171 	int new_capacity = (capacity == 0 ? 1 : capacity);
   176 	int new_capacity = (capacity == 0 ? 1 : capacity);
   172 	while (new_capacity <= id) {
   177 	while (new_capacity <= id) {
   173 	  new_capacity <<= 1;
   178 	  new_capacity <<= 1;
   186 	capacity = new_capacity;
   191 	capacity = new_capacity;
   187       }
   192       }
   188       allocator.construct(&(values[id]), Value());
   193       allocator.construct(&(values[id]), Value());
   189     }
   194     }
   190 
   195 
   191     void add(const std::vector<Key>& keys) {
   196     virtual void add(const std::vector<Key>& keys) {
   192       int max_id = -1;
   197       int max_id = -1;
   193       for (int i = 0; i < (int)keys.size(); ++i) {
   198       for (int i = 0; i < (int)keys.size(); ++i) {
   194 	int id = graph->id(keys[i]);
   199 	int id = graph->id(keys[i]);
   195 	if (id > max_id) {
   200 	if (id > max_id) {
   196 	  max_id = id;
   201 	  max_id = id;
   227       }
   232       }
   228     }
   233     }
   229 		
   234 		
   230     /// Erase a key from the map. It called by the map registry.
   235     /// Erase a key from the map. It called by the map registry.
   231      
   236      
   232     void erase(const Key& key) {
   237     virtual void erase(const Key& key) {
   233       int id = graph->id(key);
   238       int id = graph->id(key);
   234       allocator.destroy(&(values[id]));
   239       allocator.destroy(&(values[id]));
   235     }
   240     }
   236 
   241 
   237     void erase(const std::vector<Key>& keys) {
   242     virtual void erase(const std::vector<Key>& keys) {
   238       for (int i = 0; i < (int)keys.size(); ++i) {
   243       for (int i = 0; i < (int)keys.size(); ++i) {
   239 	int id = graph->id(keys[i]);
   244 	int id = graph->id(keys[i]);
   240 	allocator.destroy(&(values[id]));
   245 	allocator.destroy(&(values[id]));
   241       }
   246       }
   242     }
   247     }
   243 
   248 
   244     void build() {
   249     virtual void build() {
   245       allocate_memory();
   250       allocate_memory();
   246       Item it;
   251       Item it;
   247       for (graph->first(it); it != INVALID; graph->next(it)) {
   252       for (graph->first(it); it != INVALID; graph->next(it)) {
   248 	int id = graph->id(it);;
   253 	int id = graph->id(it);;
   249 	allocator.construct(&(values[id]), Value());
   254 	allocator.construct(&(values[id]), Value());
   250       }								
   255       }								
   251     }
   256     }
   252 
   257 
   253     void clear() {	
   258     virtual void clear() {	
   254       if (capacity != 0) {
   259       if (capacity != 0) {
   255 	Item it;
   260 	Item it;
   256 	for (graph->first(it); it != INVALID; graph->next(it)) {
   261 	for (graph->first(it); it != INVALID; graph->next(it)) {
   257 	  int id = graph->id(it);
   262 	  int id = graph->id(it);
   258 	  allocator.destroy(&(values[id]));
   263 	  allocator.destroy(&(values[id]));