COIN-OR::LEMON - Graph Library

Ignore:
Timestamp:
07/15/04 14:15:58 (20 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@954
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/work/deba/array_map_factory.h

    r702 r703  
     1// -*- c++ -*-
    12#ifndef ARRAY_MAP_H
    23#define ARRAY_MAP_H
     
    45#include <memory>
    56
    6 
    7 #include <iostream>
    8 using namespace std;
     7#include "extended_pair.h"
    98
    109namespace hugo {
     
    2019    typedef typename MapRegistry::MapBase MapBase;
    2120               
    22     template <typename V, typename A = std::allocator<V> > class Map : public MapBase {
     21    template <typename V, typename A = std::allocator<V> >
     22    class Map : public MapBase {
    2323   
    2424      public:
     
    3131                       
    3232      Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    33         int max_id = -1;
    34         for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    35           int id = getGraph()->id(it);
    36           if (id > max_id) {
    37             max_id = id;
    38           }                     
    39         }
    40         if (max_id == -1) {
    41           capacity = 0;
    42           values = 0;
    43           return;
    44         }
    45         capacity = 1;
    46         while (capacity <= max_id) {
    47           capacity <<= 1;
    48         }
    49         values = allocator.allocate(capacity);
     33        allocate_memory();
    5034        for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    5135          int id = getGraph()->id(it);
     
    5539
    5640      Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
    57         int max_id = -1;
    58         for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    59           int id = getGraph()->id(it);
    60           if (id > max_id) {
    61             max_id = id;
    62           }                     
    63         }
    64         if (max_id == -1) {
    65           capacity = 0;
    66           values = 0;
    67           return;
    68         }
    69         capacity = 1;
    70         while (capacity <= max_id) {
    71           capacity <<= 1;
    72         }
    73         values = allocator.allocate(capacity);
     41        allocate_memory();
    7442        for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    7543          int id = getGraph()->id(it);
     
    8856      }
    8957
    90       template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
     58      template <typename CMap> Map(const CMap& copy)
     59        : capacity(0), values(0), MapBase(copy) {
    9160        if (getGraph()) {
    92           init();
     61          allocate_memory();
    9362          for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    9463            set(it, copy[it]);
     
    11887        this->MapBase::operator=(copy);
    11988        if (getGraph()) {
    120           init();
     89          allocate_memory();
    12190          for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    12291            set(it, copy[it]);
     
    12493        }
    12594      }
    126 
    127        
    12895                               
    12996      virtual ~Map() {
     
    182149      }
    183150       
    184       /** Compatible iterator with the stl maps' iterators.
    185        *  It iterates on pairs of a key and a value.
    186        */
    187151      class iterator {
    188152        friend class Map;
    189153        friend class const_iterator;
    190         private:
     154      private:
    191155
    192156        /** Private constructor to initalize the the iterators returned
     
    195159        iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
    196160
    197         public:
     161      public:
    198162
    199163        /** Default constructor.
    200164         */
    201165        iterator() {}
     166
     167        typedef extended_pair<const Key&, const Key&,
     168                              Value&, Value&> Reference;
    202169
    203170        /** Dereference operator for map.
    204171         */     
    205         std::pair<const Key, Value> operator*() {
    206           return std::pair<const Key, Value>(it, (*map)[it]);
    207         }
     172        Reference operator*() {
     173          return Reference(it, (*map)[it]);
     174        }
     175
     176        class Pointer {
     177          friend class iterator;
     178        private:
     179          Reference data;
     180          Pointer(const Key& key, Value& val) : data(key, val) {}
     181        public:
     182          Reference* operator->() {return &data;}
     183        };
    208184
    209185        /** Arrow operator for map.
    210186         */     
    211         std::pair<const Key, Value>* operator->() {
    212           static std::pair<const Key, Value> tmp = operator*();
    213           return &tmp;
     187        Pointer operator->() {
     188          return Pointer(it, ((*map)[it]));
    214189        }
    215190
     
    240215          return !(*this == p_it);
    241216        }
    242        
    243         private:
     217
     218       
     219      private:
    244220        Map* map;
    245221        KeyIt it;
     
    261237        friend class Map;
    262238        friend class iterator;
    263         private:
     239      private:
    264240
    265241        /** Private constructor to initalize the the iterators returned
    266242         *  by the begin() and end().
    267243         */
    268         const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
    269 
    270         public:
     244        const_iterator (const Map& pmap, const KeyIt& pit)
     245          : map(&pmap), it(pit) {}
     246
     247      public:
    271248
    272249        /** Default constructor.
     
    276253        /** Constructor to convert iterator to const_iterator.
    277254         */
    278         const_iterator(iterator p_it) {
    279           it = p_it.it;
    280         }
     255        const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
    281256     
     257        typedef extended_pair<const Key&, const Key&,
     258          const Value&, const Value&> Reference;
     259
    282260        /** Dereference operator for map.
    283261         */     
    284         std::pair<const Key, const Value> operator*() const {
    285           return std::pair<const Key, const Value>(it, (*map)[it]);
    286         }
     262        Reference operator*() {
     263          return Reference(it, (*map)[it]);
     264        }
     265
     266
     267        class Pointer {
     268          friend class const_iterator;
     269        private:
     270          Reference data;
     271          Pointer(const Key& key, const Value& val) : data(key, val) {}
     272        public:
     273          Reference* operator->() {return &data;}
     274        };
    287275
    288276        /** Arrow operator for map.
    289277         */     
    290         std::pair<const Key, const Value>* operator->() const {
    291           static std::pair<const Key, const Value> tmp = operator*();
    292           return &tmp;
     278        Pointer operator->() {
     279          return Pointer(it, ((*map)[it]));
    293280        }
    294281
     
    320307        }
    321308       
    322         private:
     309
     310      private:
    323311        const Map* map;
    324312        KeyIt it;
     
    337325      }
    338326
    339       private:
     327    private:
     328     
     329      void allocate_memory() {
     330        int max_id = -1;
     331        for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
     332          int id = getGraph()->id(it);
     333          if (id > max_id) {
     334            max_id = id;
     335          }                     
     336        }
     337        if (max_id == -1) {
     338          capacity = 0;
     339          values = 0;
     340          return;
     341        }
     342        capacity = 1;
     343        while (capacity <= max_id) {
     344          capacity <<= 1;
     345        }
     346        values = allocator.allocate(capacity); 
     347      }     
    340348      int capacity;
    341349      Value* values;
Note: See TracChangeset for help on using the changeset viewer.