COIN-OR::LEMON - Graph Library

Changeset 844:9bf990cb066d in lemon-0.x for src/hugo/array_map.h


Ignore:
Timestamp:
09/13/04 22:05:13 (20 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1143
Message:

Bug fix in the symmetric maps.
Faster map initialization.
Iterators and Containers STL compatible.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/hugo/array_map.h

    r830 r844  
    66
    77#include <hugo/map_iterator.h>
     8#include <hugo/map_bits.h>
    89
    910///\ingroup graphmaps
     
    7273      allocate_memory();
    7374      for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    74         int id = MapBase::getGraph()->id(it);
     75        int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
    7576        allocator.construct(&(values[id]), Value());
    7677      }                                                         
     
    8384      allocate_memory();
    8485      for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    85         int id = MapBase::getGraph()->id(it);
     86        int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
    8687        allocator.construct(&(values[id]), v);
    8788      }                                                         
     
    9596      values = allocator.allocate(capacity);
    9697      for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    97         int id = MapBase::getGraph()->id(it);
     98        int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
    9899        allocator.construct(&(values[id]), copy.values[id]);
    99100      }
     
    124125      values = allocator.allocate(capacity);
    125126      for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    126         int id = MapBase::getGraph()->id(it);
     127        int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
    127128        allocator.construct(&(values[id]), copy.values[id]);
    128129      }
     
    133134     */
    134135    template <typename CMap> ArrayMap& operator=(const CMap& copy) {
    135       if (MapBase::getGraph()) {
     136      if (capacity != 0) {
    136137        MapBase::destroy();
    137       }
     138        allocator.deallocate(values, capacity);
     139      }
    138140      MapBase::operator=(copy);
    139141      if (MapBase::getGraph()) {
     
    161163     */
    162164    ReferenceType operator[](const KeyType& key) {
    163       int id = MapBase::getGraph()->id(key);
     165      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
    164166      return values[id];
    165167    }
     
    170172     */
    171173    ConstReferenceType operator[](const KeyType& key) const {
    172       int id = MapBase::getGraph()->id(key);
     174      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
    173175      return values[id];
    174176    }
     
    178180     */
    179181    void set(const KeyType& key, const ValueType& val) {
    180       int id = MapBase::getGraph()->id(key);
     182      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
    181183      values[id] = val;
    182184    }
     
    185187     */
    186188    void add(const KeyType& key) {
    187       int id = MapBase::getGraph()->id(key);
     189      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
    188190      if (id >= capacity) {
    189191        int new_capacity = (capacity == 0 ? 1 : capacity);
     
    193195        Value* new_values = allocator.allocate(new_capacity);;
    194196        for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    195           int jd = MapBase::getGraph()->id(it);
     197          int jd = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
    196198          if (id != jd) {
    197199            allocator.construct(&(new_values[jd]), values[jd]);
     
    209211     */
    210212    void erase(const KeyType& key) {
    211       int id = MapBase::getGraph()->id(key);
     213      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
    212214      allocator.destroy(&(values[id]));
    213215    }
     
    279281     
    280282    void allocate_memory() {
    281       int max_id = -1;
    282       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    283         int id = MapBase::getGraph()->id(it);
    284         if (id > max_id) {
    285           max_id = id;
    286         }                       
    287       }
     283      int max_id = KeyInfo<Graph, KeyIt>::maxId(*MapBase::getGraph());
    288284      if (max_id == -1) {
    289285        capacity = 0;
     
    301297    Value* values;
    302298    Allocator allocator;
     299
     300  public:
     301    // STL  compatibility typedefs.
     302    typedef Iterator iterator;
     303    typedef ConstIterator const_iterator;
     304    typedef typename Iterator::PairValueType value_type;
     305    typedef typename Iterator::KeyType key_type;
     306    typedef typename Iterator::ValueType data_type;
     307    typedef typename Iterator::PairReferenceType reference;
     308    typedef typename Iterator::PairPointerType pointer;
     309    typedef typename ConstIterator::PairReferenceType const_reference;
     310    typedef typename ConstIterator::PairPointerType const_pointer;
     311    typedef int difference_type;               
    303312  };           
    304313
Note: See TracChangeset for help on using the changeset viewer.