src/hugo/array_map.h
changeset 893 89d5c283a485
parent 844 9bf990cb066d
child 897 ef09eee53b09
equal deleted inserted replaced
2:ebec0894ab1d 3:700b2fe46828
    63     typedef std::allocator<Value> Allocator;
    63     typedef std::allocator<Value> Allocator;
    64 
    64 
    65 	
    65 	
    66     /** Default constructor for the map.
    66     /** Default constructor for the map.
    67      */
    67      */
    68     ArrayMap() : values(0), capacity(0) {}
    68     ArrayMap() : capacity(0), values(0) {}
    69 			
    69 			
    70     /** Graph and Registry initialized map constructor.
    70     /** Graph and Registry initialized map constructor.
    71      */
    71      */
    72     ArrayMap(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    72     ArrayMap(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    73       allocate_memory();
    73       allocate_memory();
    88       }								
    88       }								
    89     }
    89     }
    90 
    90 
    91     /** Constructor to copy a map of the same map type.
    91     /** Constructor to copy a map of the same map type.
    92      */
    92      */
    93     ArrayMap(const ArrayMap& copy) : MapBase(*copy.graph, *copy.registry) {
    93     ArrayMap(const ArrayMap& copy) : MapBase(copy) {
    94       capacity = copy.capacity;
    94       capacity = copy.capacity;
    95       if (capacity == 0) return;
    95       if (capacity == 0) return;
    96       values = allocator.allocate(capacity);
    96       values = allocator.allocate(capacity);
    97       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    97       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    98 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
    98 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
   100       }
   100       }
   101     }
   101     }
   102 
   102 
   103     /** Constructor to copy a map of an other map type.
   103     /** Constructor to copy a map of an other map type.
   104      */
   104      */
   105     template <typename CMap> ArrayMap(const CMap& copy) 
   105     template <typename TT>
   106       : MapBase(copy), capacity(0), values(0) {
   106     ArrayMap(const ArrayMap<MapRegistry, TT>& copy) 
   107       if (MapBase::getGraph()) {
   107       : MapBase(copy) {
   108 	allocate_memory();
   108       capacity = copy.capacity;
   109 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
   109       if (capacity == 0) return;
   110 	  set(it, copy[it]);
   110       values = allocator.allocate(capacity);
   111 	}
   111       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
       
   112 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
       
   113 	allocator.construct(&(values[id]), copy.values[id]);
   112       }
   114       }
   113     }
   115     }
   114 
   116 
   115     /** Assign operator to copy a map of the same map type.
   117     /** Assign operator to copy a map of the same map type.
   116      */
   118      */
   117     ArrayMap& operator=(const ArrayMap& copy) {
   119     ArrayMap& operator=(const ArrayMap& copy) {
   118       if (&copy == this) return *this;
   120       if (&copy == this) return *this;
       
   121 
   119       if (capacity != 0) {
   122       if (capacity != 0) {
   120 	MapBase::destroy();
   123 	MapBase::destroy();
   121 	allocator.deallocate(values, capacity);
   124 	allocator.deallocate(values, capacity);
   122       }
   125       }
       
   126 
       
   127       MapBase::operator=(copy);
       
   128 
   123       capacity = copy.capacity;
   129       capacity = copy.capacity;
   124       if (capacity == 0) return *this;
   130       if (capacity == 0) return *this;
   125       values = allocator.allocate(capacity);
   131       values = allocator.allocate(capacity);
       
   132 
   126       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
   133       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
   127 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
   134 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
   128 	allocator.construct(&(values[id]), copy.values[id]);
   135 	allocator.construct(&(values[id]), copy.values[id]);
   129       }
   136       }
       
   137 
   130       return *this;
   138       return *this;
   131     }
   139     }
   132 
   140 
   133     /** Assign operator to copy a map an other map type.
   141     /** Assign operator to copy a map of an other map type.
   134      */
   142      */
   135     template <typename CMap> ArrayMap& operator=(const CMap& copy) {
   143     template <typename TT>
       
   144     ArrayMap& operator=(const ArrayMap<MapRegistry, TT>& copy) {
   136       if (capacity != 0) {
   145       if (capacity != 0) {
   137 	MapBase::destroy();
   146 	MapBase::destroy();
   138 	allocator.deallocate(values, capacity);
   147 	allocator.deallocate(values, capacity);
   139       }
   148       }
       
   149 
   140       MapBase::operator=(copy);
   150       MapBase::operator=(copy);
   141       if (MapBase::getGraph()) {
   151 
   142 	allocate_memory();
   152       capacity = copy.capacity;
   143 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
   153       if (capacity == 0) return *this;
   144 	  set(it, copy[it]);
   154       values = allocator.allocate(capacity);
   145 	}
   155 
   146       }
   156       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
       
   157 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
       
   158 	allocator.construct(&(values[id]), copy.values[id]);
       
   159       }
       
   160 
   147       return *this;
   161       return *this;
   148     }
   162     }
   149 				
   163 				
   150     /** The destructor of the map.
   164     /** The destructor of the map.
   151      */
   165      */