lemon/bits/static_map.h
author deba
Fri, 24 Feb 2006 11:13:09 +0000
changeset 1984 d4cbd10e1256
parent 1961 8e19ca944727
child 1993 2115143eceea
permissions -rw-r--r--
Bug fix
deba@1703
     1
/* -*- C++ -*-
deba@1703
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1703
     8
 *
deba@1703
     9
 * Permission to use, modify and distribute this software is granted
deba@1703
    10
 * provided that this copyright notice appears in all copies. For
deba@1703
    11
 * precise terms see the accompanying LICENSE file.
deba@1703
    12
 *
deba@1703
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1703
    14
 * express or implied, and with no claim as to its suitability for any
deba@1703
    15
 * purpose.
deba@1703
    16
 *
deba@1703
    17
 */
deba@1703
    18
deba@1703
    19
#ifndef LEMON_STATIC_MAP_H
deba@1703
    20
#define LEMON_STATIC_MAP_H
deba@1703
    21
deba@1703
    22
#include <algorithm>
deba@1703
    23
#include <iostream>
deba@1703
    24
deba@1703
    25
#include <lemon/utility.h>
deba@1810
    26
#include <lemon/bits/map_extender.h>
deba@1703
    27
#include <lemon/bits/alteration_notifier.h>
deba@1703
    28
#include <lemon/error.h>
deba@1703
    29
#include <lemon/concept_check.h>
deba@1703
    30
#include <lemon/concept/maps.h>
deba@1703
    31
alpar@1946
    32
/// \ingroup graphmaps
deba@1703
    33
///
deba@1703
    34
///\file
deba@1703
    35
///\brief Static sized graph maps.
deba@1703
    36
deba@1703
    37
namespace lemon {
deba@1703
    38
alpar@1946
    39
  /// \ingroup graphmaps
deba@1703
    40
  ///
deba@1703
    41
  /// \brief Graph map with static sized storage.
deba@1703
    42
  ///
deba@1703
    43
  /// The StaticMap template class is graph map structure what
deba@1910
    44
  /// does not indate automatically the map when a key is added to or 
deba@1703
    45
  /// erased from the map rather it throws an exception. This map factory 
deba@1703
    46
  /// uses the allocators to implement the container functionality.
deba@1703
    47
  ///
deba@1703
    48
  /// \param Registry The AlterationNotifier that will notify this map.
deba@1703
    49
  /// \param Item The item type of the graph items.
deba@1703
    50
  /// \param Value The value type of the map.
deba@1703
    51
  /// 
deba@1703
    52
  /// \author Balazs Dezso
deba@1703
    53
  	
deba@1703
    54
deba@1703
    55
  template <typename _Graph, typename _Item, typename _Value>
deba@1703
    56
  class StaticMap : public AlterationNotifier<_Item>::ObserverBase {
deba@1703
    57
  public:
deba@1703
    58
deba@1979
    59
    /// \brief Exception class for unsupported exceptions.
deba@1979
    60
    class UnsupportedOperation : public lemon::LogicError {
deba@1703
    61
    public:
deba@1703
    62
      virtual const char* exceptionName() const {
deba@1979
    63
	return "lemon::StaticMap::UnsupportedOperation";
deba@1703
    64
      }
deba@1703
    65
    };
deba@1703
    66
deba@1719
    67
  private:
deba@1703
    68
		
deba@1719
    69
    typedef std::vector<_Value> Container;	
deba@1719
    70
deba@1719
    71
  public:
deba@1719
    72
deba@1703
    73
    /// The graph type of the map. 
deba@1703
    74
    typedef _Graph Graph;
deba@1719
    75
    /// The reference map tag.
deba@1719
    76
    typedef True ReferenceMapTag;
deba@1719
    77
deba@1703
    78
    /// The key type of the map.
deba@1703
    79
    typedef _Item Key;
deba@1703
    80
    /// The value type of the map.
deba@1703
    81
    typedef _Value Value;
deba@1719
    82
    /// The const reference type of the map.
deba@1719
    83
    typedef typename Container::const_reference ConstReference;
deba@1719
    84
    /// The reference type of the map.
deba@1719
    85
    typedef typename Container::reference Reference;
deba@1719
    86
deba@1719
    87
    typedef const Value ConstValue;
deba@1719
    88
    typedef Value* Pointer;
deba@1719
    89
    typedef const Value* ConstPointer;
deba@1719
    90
deba@1719
    91
    typedef AlterationNotifier<_Item> Registry;
deba@1703
    92
deba@1703
    93
    /// The map type.
deba@1703
    94
    typedef StaticMap Map;
deba@1703
    95
    /// The base class of the map.
deba@1703
    96
    typedef typename Registry::ObserverBase Parent;
deba@1703
    97
deba@1703
    98
    /// \brief Constructor to attach the new map into the registry.
deba@1703
    99
    ///
deba@1703
   100
    /// It constructs a map and attachs it into the registry.
deba@1703
   101
    /// It adds all the items of the graph to the map.
deba@1703
   102
    StaticMap(const Graph& _g) : graph(&_g) {
deba@1703
   103
      attach(_g.getNotifier(_Item()));
deba@1703
   104
      build();
deba@1703
   105
    }
deba@1703
   106
deba@1703
   107
    /// \brief Constructor uses given value to initialize the map. 
deba@1703
   108
    ///
deba@1703
   109
    /// It constructs a map uses a given value to initialize the map. 
deba@1703
   110
    /// It adds all the items of the graph to the map.     
deba@1703
   111
    StaticMap(const Graph& _g, const Value& _v) : graph(&_g) { 
deba@1703
   112
      attach(_g.getNotifier(_Item()));
deba@1703
   113
      unsigned size = graph->maxId(_Item()) + 1;
deba@1703
   114
      container.reserve(size);
deba@1703
   115
      container.resize(size, _v);
deba@1703
   116
    }
deba@1703
   117
deba@1703
   118
    /// \brief Copy constructor
deba@1703
   119
    ///
deba@1703
   120
    /// Copy constructor.
deba@1703
   121
    StaticMap(const StaticMap& _copy) : Parent(), graph(_copy.getGraph()) {
deba@1703
   122
      if (_copy.attached()) {
deba@1703
   123
	attach(*_copy.getRegistry());
deba@1703
   124
	container = _copy.container;
deba@1703
   125
      }
deba@1703
   126
    }
deba@1703
   127
deba@1703
   128
    /// \brief Destrcutor
deba@1703
   129
    ///
deba@1703
   130
    /// Destructor.
deba@1703
   131
    virtual ~StaticMap() {
deba@1703
   132
      clear();
deba@1703
   133
      if (attached()) {
deba@1703
   134
	detach();
deba@1703
   135
      }
deba@1703
   136
    }
deba@1703
   137
deba@1703
   138
deba@1703
   139
  private:
deba@1703
   140
deba@1703
   141
    StaticMap& operator=(const StaticMap&);
deba@1703
   142
deba@1703
   143
  protected:
deba@1703
   144
deba@1703
   145
    using Parent::attach;
deba@1703
   146
    using Parent::detach;
deba@1703
   147
    using Parent::attached;
deba@1703
   148
deba@1703
   149
    const Graph* getGraph() const {
deba@1703
   150
      return graph;
deba@1703
   151
    }
deba@1703
   152
deba@1703
   153
  public:
deba@1703
   154
deba@1703
   155
    /// \brief The subcript operator.
deba@1703
   156
    ///
deba@1703
   157
    /// The subscript operator. The map can be subscripted by the
deba@1703
   158
    /// actual items of the graph. 
deba@1703
   159
     
deba@1703
   160
    Reference operator[](const Key& key) {
deba@1703
   161
      return container[graph->id(key)];
deba@1703
   162
    } 
deba@1703
   163
		
deba@1703
   164
    /// \brief The const subcript operator.
deba@1703
   165
    ///
deba@1703
   166
    /// The const subscript operator. The map can be subscripted by the
deba@1703
   167
    /// actual items of the graph. 
deba@1703
   168
     
deba@1703
   169
    ConstReference operator[](const Key& key) const {
deba@1703
   170
      return container[graph->id(key)];
deba@1703
   171
    }
deba@1703
   172
deba@1703
   173
deba@1703
   174
    /// \brief The setter function of the map.
deba@1703
   175
    ///
deba@1703
   176
    /// It the same as operator[](key) = value expression.
deba@1703
   177
    ///
deba@1703
   178
    void set(const Key& key, const Value& value) {
deba@1703
   179
      (*this)[key] = value;
deba@1703
   180
    }
deba@1703
   181
deba@1703
   182
  protected:
deba@1703
   183
deba@1703
   184
    /// \brief Adds a new key to the map.
deba@1703
   185
    ///		
deba@1703
   186
    /// It adds a new key to the map. It called by the observer registry
deba@1703
   187
    /// and it overrides the add() member function of the observer base.
deba@1703
   188
     
deba@1703
   189
    void add(const Key&) {
deba@1979
   190
      throw UnsupportedOperation();
deba@1703
   191
    }
deba@1703
   192
deba@1703
   193
    /// \brief Erases a key from the map.
deba@1703
   194
    ///
deba@1703
   195
    /// Erase a key from the map. It called by the observer registry
deba@1703
   196
    /// and it overrides the erase() member function of the observer base.     
deba@1703
   197
    void erase(const Key&) {
deba@1979
   198
      throw UnsupportedOperation();
deba@1703
   199
    }
deba@1703
   200
deba@1703
   201
    /// Buildes the map.
deba@1703
   202
		
deba@1703
   203
    /// It buildes the map. It called by the observer registry
deba@1703
   204
    /// and it overrides the build() member function of the observer base.
deba@1703
   205
deba@1703
   206
    void build() { 
deba@1703
   207
      int size = graph->maxId(_Item()) + 1;
deba@1703
   208
      container.reserve(size);
deba@1703
   209
      container.resize(size);
deba@1703
   210
    }
deba@1703
   211
deba@1703
   212
    /// Clear the map.
deba@1703
   213
deba@1703
   214
    /// It erase all items from the map. It called by the observer registry
deba@1703
   215
    /// and it overrides the clear() member function of the observer base.     
deba@1703
   216
    void clear() { 
deba@1703
   217
      container.clear();
deba@1703
   218
    }
deba@1703
   219
    
deba@1703
   220
  private:
deba@1703
   221
		
deba@1703
   222
    Container container;
deba@1703
   223
    const Graph *graph;
deba@1703
   224
deba@1703
   225
  };
deba@1703
   226
deba@1703
   227
}
deba@1703
   228
deba@1703
   229
#endif