lemon/matrix_maps.h
author convert-repo
Thu, 04 Mar 2010 19:34:55 +0000
changeset 2659 611ced85018b
parent 2391 14a343be7a5a
permissions -rw-r--r--
update tags
deba@1720
     1
/* -*- C++ -*-
deba@1720
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1720
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1720
     8
 *
deba@1720
     9
 * Permission to use, modify and distribute this software is granted
deba@1720
    10
 * provided that this copyright notice appears in all copies. For
deba@1720
    11
 * precise terms see the accompanying LICENSE file.
deba@1720
    12
 *
deba@1720
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1720
    14
 * express or implied, and with no claim as to its suitability for any
deba@1720
    15
 * purpose.
deba@1720
    16
 *
deba@1720
    17
 */
deba@1720
    18
deba@1720
    19
#ifndef LEMON_MATRIX_MAPS_H
deba@1720
    20
#define LEMON_MATRIX_MAPS_H
deba@1720
    21
deba@1720
    22
deba@1720
    23
#include <vector>
deba@1993
    24
#include <lemon/bits/utility.h>
alpar@2088
    25
#include <lemon/bits/invalid.h>
deba@1720
    26
#include <lemon/maps.h>
deba@1720
    27
alpar@2260
    28
#include <lemon/concepts/matrix_maps.h>
deba@1720
    29
deba@1720
    30
/// \file
alpar@2072
    31
/// \ingroup matrices
deba@1720
    32
/// \brief Maps indexed with pairs of items.
deba@1720
    33
///
alpar@2260
    34
/// \todo This file has the same name as the concept file in concepts/,
deba@1720
    35
///  and this is not easily detectable in docs...
deba@1720
    36
namespace lemon {
deba@1720
    37
deba@1720
    38
  /// \brief Map for the coloumn view of the matrix
deba@1720
    39
  ///
alpar@2072
    40
  /// \ingroup matrices
deba@1720
    41
  /// Map for the coloumn view of the matrix.
alpar@2072
    42
  ///
deba@1720
    43
  template <typename _MatrixMap>
deba@2039
    44
  class MatrixRowMap : public MatrixMapTraits<_MatrixMap> {
deba@1720
    45
  public:
deba@1720
    46
    typedef _MatrixMap MatrixMap;
deba@1720
    47
    typedef typename MatrixMap::SecondKey Key;
deba@1720
    48
    typedef typename MatrixMap::Value Value;
deba@1720
    49
deba@1720
    50
deba@2084
    51
    /// \brief Constructor of the row map
deba@2084
    52
    ///
deba@2084
    53
    /// Constructor of the row map.
deba@1751
    54
    MatrixRowMap(MatrixMap& _matrix, typename MatrixMap::FirstKey _row) 
deba@1720
    55
      : matrix(_matrix), row(_row) {}
deba@1720
    56
deba@1720
    57
    /// \brief Subscription operator
deba@1720
    58
    ///
deba@1720
    59
    /// Subscription operator.
deba@2039
    60
    typename MatrixMapTraits<MatrixMap>::ReturnValue
deba@1720
    61
    operator[](Key col) {
deba@1751
    62
      return matrix(row, col);
deba@1720
    63
    }
deba@1720
    64
deba@1720
    65
    /// \brief Setter function
deba@1720
    66
    ///
deba@1720
    67
    /// Setter function.
deba@1720
    68
    void set(Key col, const Value& val) {
deba@1751
    69
      matrix.set(row, col, val);
deba@1720
    70
    }
deba@1720
    71
      
deba@1720
    72
    /// \brief Subscription operator
deba@1720
    73
    ///
deba@1720
    74
    /// Subscription operator.
deba@2039
    75
    typename MatrixMapTraits<MatrixMap>::ConstReturnValue
deba@1720
    76
    operator[](Key col) const {
deba@1751
    77
      return matrix(row, col);
deba@1720
    78
    }
deba@1720
    79
deba@1720
    80
  private:
deba@1720
    81
    MatrixMap& matrix;
deba@1751
    82
    typename MatrixMap::FirstKey row;
deba@1720
    83
  };
deba@1720
    84
deba@1720
    85
  /// \brief Map for the row view of the matrix
deba@1720
    86
  ///
alpar@2072
    87
  /// \ingroup matrices
deba@1720
    88
  /// Map for the row view of the matrix.
alpar@2072
    89
  ///
deba@1720
    90
  template <typename _MatrixMap>
deba@2039
    91
  class ConstMatrixRowMap : public MatrixMapTraits<_MatrixMap> {
deba@1720
    92
  public:
deba@1720
    93
    typedef _MatrixMap MatrixMap;
deba@1751
    94
    typedef typename MatrixMap::SecondKey Key;
deba@1720
    95
    typedef typename MatrixMap::Value Value;
deba@1720
    96
deba@1751
    97
deba@2084
    98
    /// \brief Constructor of the row map
deba@2084
    99
    ///
deba@2084
   100
    /// Constructor of the row map.
deba@1720
   101
    ConstMatrixRowMap(const MatrixMap& _matrix, 
deba@1751
   102
		      typename MatrixMap::FirstKey _row) 
deba@1720
   103
      : matrix(_matrix), row(_row) {}
deba@1720
   104
deba@1720
   105
    /// \brief Subscription operator
deba@1720
   106
    ///
deba@1720
   107
    /// Subscription operator.
deba@2039
   108
    typename MatrixMapTraits<MatrixMap>::ConstReturnValue
deba@1720
   109
    operator[](Key col) const {
deba@1751
   110
      return matrix(row, col);
deba@1720
   111
    }
deba@1720
   112
deba@1720
   113
  private:
deba@1720
   114
    const MatrixMap& matrix;
deba@1751
   115
    typename MatrixMap::FirstKey row;
deba@1720
   116
  };
deba@1720
   117
deba@2084
   118
  /// \ingroup matrices
deba@2084
   119
  ///
deba@1720
   120
  /// \brief Gives back a row view of the matrix map
deba@1720
   121
  ///
deba@1720
   122
  /// Gives back a row view of the matrix map.
alpar@2072
   123
  ///
deba@2084
   124
  /// \sa MatrixRowMap
deba@2084
   125
  /// \sa ConstMatrixRowMap
deba@1720
   126
  template <typename MatrixMap>
deba@1720
   127
  MatrixRowMap<MatrixMap> matrixRowMap(MatrixMap& matrixMap,
deba@1751
   128
				       typename MatrixMap::FirstKey row) {
deba@1720
   129
    return MatrixRowMap<MatrixMap>(matrixMap, row);
deba@1720
   130
  }
deba@1720
   131
deba@1720
   132
  template <typename MatrixMap>
deba@1751
   133
  ConstMatrixRowMap<MatrixMap>
deba@1751
   134
  matrixRowMap(const MatrixMap& matrixMap, typename MatrixMap::FirstKey row) {
deba@1720
   135
    return ConstMatrixRowMap<MatrixMap>(matrixMap, row);
deba@1720
   136
  }
deba@1720
   137
alpar@2072
   138
  /// \brief Map for the column view of the matrix
deba@1751
   139
  ///
alpar@2072
   140
  /// \ingroup matrices
alpar@2072
   141
  /// Map for the column view of the matrix.
alpar@2072
   142
  ///
deba@1751
   143
  template <typename _MatrixMap>
deba@2039
   144
  class MatrixColMap : public MatrixMapTraits<_MatrixMap> {
deba@1751
   145
  public:
deba@1751
   146
    typedef _MatrixMap MatrixMap;
deba@1751
   147
    typedef typename MatrixMap::FirstKey Key;
deba@1751
   148
    typedef typename MatrixMap::Value Value;
deba@1751
   149
deba@2084
   150
    /// \brief Constructor of the column map
deba@2084
   151
    ///
deba@2084
   152
    /// Constructor of the column map.
deba@1751
   153
    MatrixColMap(MatrixMap& _matrix, typename MatrixMap::SecondKey _col) 
deba@1751
   154
      : matrix(_matrix), col(_col) {}
deba@1751
   155
deba@1751
   156
    /// \brief Subscription operator
deba@1751
   157
    ///
deba@1751
   158
    /// Subscription operator.
deba@2039
   159
    typename MatrixMapTraits<MatrixMap>::ReturnValue
deba@1751
   160
    operator[](Key row) {
deba@1751
   161
      return matrix(row, col);
deba@1751
   162
    }
deba@1751
   163
deba@1751
   164
    /// \brief Setter function
deba@1751
   165
    ///
deba@1751
   166
    /// Setter function.
deba@1751
   167
    void set(Key row, const Value& val) {
deba@1751
   168
      matrix.set(row, col, val);
deba@1751
   169
    }
deba@1751
   170
      
deba@1751
   171
    /// \brief Subscription operator
deba@1751
   172
    ///
deba@1751
   173
    /// Subscription operator.
deba@2039
   174
    typename MatrixMapTraits<MatrixMap>::ConstReturnValue
deba@1751
   175
    operator[](Key row) const {
deba@1751
   176
      return matrix(row, col);
deba@1751
   177
    }
deba@1751
   178
deba@1751
   179
  private:
deba@1751
   180
    MatrixMap& matrix;
deba@1751
   181
    typename MatrixMap::SecondKey col;
deba@1751
   182
  };
deba@1751
   183
alpar@2072
   184
  /// \brief Map for the column view of the matrix
deba@1751
   185
  ///
alpar@2072
   186
  /// \ingroup matrices
alpar@2072
   187
  /// Map for the column view of the matrix.
alpar@2072
   188
  ///
deba@1751
   189
  template <typename _MatrixMap>
deba@2039
   190
  class ConstMatrixColMap : public MatrixMapTraits<_MatrixMap> {
deba@1751
   191
  public:
deba@1751
   192
    typedef _MatrixMap MatrixMap;
deba@1751
   193
    typedef typename MatrixMap::FirstKey Key;
deba@1751
   194
    typedef typename MatrixMap::Value Value;
deba@1751
   195
deba@2084
   196
    /// \brief Constructor of the column map
deba@2084
   197
    ///
deba@2084
   198
    /// Constructor of the column map.
deba@1751
   199
    ConstMatrixColMap(const MatrixMap& _matrix, 
deba@1751
   200
		      typename MatrixMap::SecondKey _col) 
deba@1751
   201
      : matrix(_matrix), col(_col) {}
deba@1751
   202
deba@1751
   203
    /// \brief Subscription operator
deba@1751
   204
    ///
deba@1751
   205
    /// Subscription operator.
deba@2039
   206
    typename MatrixMapTraits<MatrixMap>::ConstReturnValue
deba@1751
   207
    operator[](Key row) const {
deba@1751
   208
      return matrix(row, col);
deba@1751
   209
    }
deba@1751
   210
deba@1751
   211
  private:
deba@1751
   212
    const MatrixMap& matrix;
deba@1751
   213
    typename MatrixMap::SecondKey col;
deba@1751
   214
  };
deba@1751
   215
deba@2084
   216
  /// \ingroup matrices
deba@2084
   217
  ///
alpar@2072
   218
  /// \brief Gives back a column view of the matrix map
deba@1751
   219
  ///
alpar@2072
   220
  /// Gives back a column view of the matrix map.
alpar@2072
   221
  ///
deba@2084
   222
  /// \sa MatrixColMap
deba@2084
   223
  /// \sa ConstMatrixColMap
deba@1751
   224
  template <typename MatrixMap>
deba@1751
   225
  MatrixColMap<MatrixMap> matrixColMap(MatrixMap& matrixMap,
deba@1751
   226
				       typename MatrixMap::SecondKey col) {
deba@1751
   227
    return MatrixColMap<MatrixMap>(matrixMap, col);
deba@1751
   228
  }
deba@1751
   229
deba@1751
   230
  template <typename MatrixMap>
deba@1751
   231
  ConstMatrixColMap<MatrixMap> 
deba@1751
   232
  matrixColMap(const MatrixMap& matrixMap, typename MatrixMap::SecondKey col) {
deba@1751
   233
    return ConstMatrixColMap<MatrixMap>(matrixMap, col);
deba@1751
   234
  }
deba@1751
   235
deba@1720
   236
  /// \brief Container for store values for each ordered pair of graph items
deba@1720
   237
  ///
alpar@2072
   238
  /// \ingroup matrices
alpar@1757
   239
  /// This data structure can strore for each pair of the same item
deba@1720
   240
  /// type a value. It increase the size of the container when the 
deba@1720
   241
  /// associated graph modified, so it updated automaticly whenever
deba@1720
   242
  /// it is needed.
deba@1720
   243
  template <typename _Graph, typename _Item, typename _Value>
deba@1720
   244
  class DynamicMatrixMap 
deba@1999
   245
    : protected ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
deba@1720
   246
  public:
deba@1999
   247
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase 
deba@1999
   248
    Parent;
deba@1720
   249
deba@1720
   250
    typedef _Graph Graph;
deba@1720
   251
    typedef _Item Key;
deba@1720
   252
deba@1720
   253
    typedef _Item FirstKey;
deba@1720
   254
    typedef _Item SecondKey;
deba@1720
   255
    typedef _Value Value;
deba@1720
   256
deba@1720
   257
    typedef True ReferenceMapTag;
deba@1720
   258
deba@1720
   259
  private:
deba@1720
   260
		
deba@1720
   261
    typedef std::vector<Value> Container;
deba@1720
   262
deba@1720
   263
  public:
deba@1720
   264
deba@1720
   265
    typedef typename Container::reference Reference;
deba@1720
   266
    typedef typename Container::const_reference ConstReference;
deba@1720
   267
deba@1720
   268
    /// \brief Creates an item matrix for the given graph
deba@1720
   269
    ///
deba@1720
   270
    /// Creates an item matrix for the given graph.
deba@1720
   271
    DynamicMatrixMap(const Graph& _graph) 
deba@1999
   272
      : values(size(_graph.maxId(Key()) + 1)) {
deba@2384
   273
      Parent::attach(_graph.notifier(Key()));
deba@1720
   274
    }
deba@1720
   275
deba@1720
   276
    /// \brief Creates an item matrix for the given graph
deba@1720
   277
    ///
deba@1720
   278
    /// Creates an item matrix for the given graph and assigns for each
deba@1720
   279
    /// pairs of keys the given parameter.
deba@1720
   280
    DynamicMatrixMap(const Graph& _graph, const Value& _val) 
deba@1999
   281
      : values(size(_graph.maxId(Key()) + 1), _val) {
deba@2384
   282
      Parent::attach(_graph.notifier(Key()));
deba@1720
   283
    }
deba@1720
   284
deba@2039
   285
    ///\brief The assignement operator.
deba@2039
   286
    ///
deba@2039
   287
    ///It allow to assign a map to an other.
deba@2039
   288
    DynamicMatrixMap& operator=(const DynamicMatrixMap& _cmap){
deba@2039
   289
      return operator=<DynamicMatrixMap>(_cmap);
deba@2039
   290
    }
deba@2039
   291
      
deba@2039
   292
    ///\brief Template assignement operator.
deba@2039
   293
    ///
deba@2039
   294
    ///It copy the element of the given map to its own container.  The
deba@2039
   295
    ///type of the two map shall be the same.
deba@2039
   296
    template <typename CMap>
deba@2039
   297
    DynamicMatrixMap& operator=(const CMap& _cmap){
alpar@2260
   298
      checkConcept<concepts::ReadMatrixMap<FirstKey, SecondKey, Value>, CMap>();
deba@2384
   299
      typename Parent::Notifier* notifier = Parent::notifier();
deba@2039
   300
      Key first, second;
deba@2039
   301
      for(notifier->first(first); first != INVALID; 
deba@2039
   302
          notifier->next(first)){
deba@2039
   303
        for(notifier->first(second); second != INVALID; 
deba@2039
   304
            notifier->next(second)){
deba@2039
   305
          set(first, second, _cmap(first, second));
deba@2039
   306
        }
deba@2039
   307
      }
deba@2039
   308
      return *this;
deba@2039
   309
    }
deba@2039
   310
deba@1720
   311
    /// \brief Gives back the value assigned to the \c first - \c second
deba@1720
   312
    /// ordered pair.
deba@1720
   313
    ///
deba@1720
   314
    /// Gives back the value assigned to the \c first - \c second ordered pair.
deba@1720
   315
    ConstReference operator()(const Key& first, const Key& second) const {
deba@2384
   316
      return values[index(Parent::notifier()->id(first), 
deba@2384
   317
                          Parent::notifier()->id(second))];
deba@1720
   318
    }
deba@1720
   319
    
deba@1720
   320
    /// \brief Gives back the value assigned to the \c first - \c second
deba@1720
   321
    /// ordered pair.
deba@1720
   322
    ///
deba@1720
   323
    /// Gives back the value assigned to the \c first - \c second ordered pair.
deba@1720
   324
    Reference operator()(const Key& first, const Key& second) {
deba@2384
   325
      return values[index(Parent::notifier()->id(first), 
deba@2384
   326
                          Parent::notifier()->id(second))];
deba@1720
   327
    }
deba@1720
   328
deba@1720
   329
    /// \brief Setter function for the matrix map.
deba@1720
   330
    ///
deba@1720
   331
    /// Setter function for the matrix map.
deba@1720
   332
    void set(const Key& first, const Key& second, const Value& val) {
deba@2384
   333
      values[index(Parent::notifier()->id(first), 
deba@2384
   334
                   Parent::notifier()->id(second))] = val;
deba@1720
   335
    }
deba@1720
   336
deba@1720
   337
  protected:
deba@1720
   338
deba@1720
   339
    static int index(int i, int j) {
deba@1720
   340
      if (i < j) {
deba@1720
   341
	return j * j + i;
deba@1720
   342
      } else {
deba@1720
   343
	return i * i + i + j;
deba@1720
   344
      }
deba@1720
   345
    }
deba@1720
   346
deba@1720
   347
    static int size(int s) {
deba@1720
   348
      return s * s;
deba@1720
   349
    }
deba@1720
   350
deba@1720
   351
    virtual void add(const Key& key) {
deba@2386
   352
      if (size(Parent::notifier()->id(key) + 1) >= int(values.size())) {
deba@2384
   353
	values.resize(size(Parent::notifier()->id(key) + 1));	
deba@1720
   354
      }
deba@1720
   355
    }
deba@1720
   356
deba@2305
   357
    virtual void add(const std::vector<Key>& keys) {
deba@2305
   358
      int new_size = 0;
deba@2386
   359
      for (int i = 0; i < int(keys.size()); ++i) {
deba@2384
   360
        if (size(Parent::notifier()->id(keys[i]) + 1) >= new_size) {
deba@2384
   361
          new_size = size(Parent::notifier()->id(keys[i]) + 1);	
deba@2305
   362
        }
deba@2305
   363
      }
deba@2386
   364
      if (new_size > int(values.size())) {
deba@2305
   365
        values.resize(new_size);
deba@2305
   366
      }
deba@2305
   367
    }
deba@2305
   368
deba@1720
   369
    virtual void erase(const Key&) {}
deba@1720
   370
deba@2305
   371
    virtual void erase(const std::vector<Key>&) {}
deba@2305
   372
deba@1720
   373
    virtual void build() {
deba@2384
   374
      values.resize(size(Parent::notifier()->maxId() + 1));
deba@1720
   375
    }
deba@1720
   376
deba@1720
   377
    virtual void clear() {
deba@1720
   378
      values.clear();
deba@1720
   379
    }   
deba@1720
   380
    
deba@1720
   381
  private:
deba@1720
   382
    std::vector<Value> values;
deba@1720
   383
  };
deba@1720
   384
deba@1720
   385
  /// \brief Container for store values for each unordered pair of graph items
deba@1720
   386
  ///
alpar@2072
   387
  /// \ingroup matrices
alpar@1757
   388
  /// This data structure can strore for each pair of the same item
deba@1720
   389
  /// type a value. It increase the size of the container when the 
deba@1720
   390
  /// associated graph modified, so it updated automaticly whenever
deba@1720
   391
  /// it is needed. 
deba@1720
   392
  template <typename _Graph, typename _Item, typename _Value>
deba@1720
   393
  class DynamicSymMatrixMap 
deba@1999
   394
    : protected ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
deba@1720
   395
  public:
deba@1999
   396
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase 
deba@1999
   397
    Parent;
deba@1720
   398
deba@1720
   399
    typedef _Graph Graph;
deba@1720
   400
    typedef _Item Key;
deba@1720
   401
deba@1720
   402
    typedef _Item FirstKey;
deba@1720
   403
    typedef _Item SecondKey;
deba@1720
   404
    typedef _Value Value;
deba@1720
   405
deba@1720
   406
    typedef True ReferenceMapTag;
deba@1720
   407
deba@1720
   408
  private:
deba@1720
   409
		
deba@1720
   410
    typedef std::vector<Value> Container;
deba@1720
   411
deba@1720
   412
  public:
deba@1720
   413
deba@1720
   414
    typedef typename Container::reference Reference;
deba@1720
   415
    typedef typename Container::const_reference ConstReference;
deba@1720
   416
deba@1720
   417
    /// \brief Creates an item matrix for the given graph
deba@1720
   418
    ///
deba@1720
   419
    /// Creates an item matrix for the given graph.
deba@1720
   420
    DynamicSymMatrixMap(const Graph& _graph) 
deba@1999
   421
      : values(size(_graph.maxId(Key()) + 1)) {
deba@2384
   422
      Parent::attach(_graph.notifier(Key()));
deba@1720
   423
    }
deba@1720
   424
deba@1720
   425
    /// \brief Creates an item matrix for the given graph
deba@1720
   426
    ///
deba@1720
   427
    /// Creates an item matrix for the given graph and assigns for each
deba@1720
   428
    /// pairs of keys the given parameter.
deba@1720
   429
    DynamicSymMatrixMap(const Graph& _graph, const Value& _val) 
deba@1999
   430
      : values(size(_graph.maxId(Key()) + 1), _val) {
deba@2384
   431
      Parent::attach(_graph.notifier(Key()));
deba@1720
   432
    }
deba@1720
   433
deba@2039
   434
deba@2039
   435
    ///\brief The assignement operator.
deba@2039
   436
    ///
deba@2039
   437
    ///It allow to assign a map to an other.
alpar@2072
   438
    ///
deba@2039
   439
    DynamicSymMatrixMap& operator=(const DynamicSymMatrixMap& _cmap){
deba@2039
   440
      return operator=<DynamicSymMatrixMap>(_cmap);
deba@2039
   441
    }
deba@2039
   442
      
deba@2039
   443
    ///\brief Template assignement operator.
deba@2039
   444
    ///
deba@2039
   445
    ///It copy the element of the given map to its own container.  The
deba@2039
   446
    ///type of the two map shall be the same.
deba@2039
   447
    template <typename CMap>
deba@2039
   448
    DynamicSymMatrixMap& operator=(const CMap& _cmap){
alpar@2260
   449
      checkConcept<concepts::ReadMatrixMap<FirstKey, SecondKey, Value>, CMap>();
deba@2384
   450
      typename Parent::Notifier* notifier = Parent::notifier();
deba@2039
   451
      Key first, second;
deba@2039
   452
      for(notifier->first(first); first != INVALID; 
deba@2039
   453
          notifier->next(first)){
deba@2039
   454
        for(notifier->first(second); second != first; 
deba@2039
   455
            notifier->next(second)){
deba@2039
   456
          set(first, second, _cmap(first, second));
deba@2039
   457
        }
deba@2039
   458
        set(first, first, _cmap(first, first));        
deba@2039
   459
      }
deba@2039
   460
      return *this;
deba@2039
   461
    }
deba@2039
   462
deba@1720
   463
    /// \brief Gives back the value assigned to the \c first - \c second
deba@1720
   464
    /// unordered pair.
deba@1720
   465
    ///
deba@1720
   466
    /// Gives back the value assigned to the \c first - \c second unordered 
deba@1720
   467
    /// pair.
deba@1720
   468
    ConstReference operator()(const Key& first, const Key& second) const {
deba@2384
   469
      return values[index(Parent::notifier()->id(first), 
deba@2384
   470
                          Parent::notifier()->id(second))];
deba@1720
   471
    }
deba@1720
   472
    
deba@1720
   473
    /// \brief Gives back the value assigned to the \c first - \c second
deba@1720
   474
    /// unordered pair.
deba@1720
   475
    ///
deba@1720
   476
    /// Gives back the value assigned to the \c first - \c second unordered 
deba@1720
   477
    /// pair.
deba@1720
   478
    Reference operator()(const Key& first, const Key& second) {
deba@2384
   479
      return values[index(Parent::notifier()->id(first), 
deba@2384
   480
                          Parent::notifier()->id(second))];
deba@1720
   481
    }
deba@1720
   482
deba@1720
   483
    /// \brief Setter function for the matrix map.
deba@1720
   484
    ///
deba@1720
   485
    /// Setter function for the matrix map.
alpar@2072
   486
    ///
deba@1720
   487
    void set(const Key& first, const Key& second, const Value& val) {
deba@2384
   488
      values[index(Parent::notifier()->id(first), 
deba@2384
   489
                   Parent::notifier()->id(second))] = val;
deba@1720
   490
    }
deba@1720
   491
deba@1720
   492
  protected:
deba@1720
   493
deba@1720
   494
    static int index(int i, int j) {
deba@1720
   495
      if (i < j) {
deba@1720
   496
	return j * (j + 1) / 2 + i;
deba@1720
   497
      } else {
deba@1720
   498
	return i * (i + 1) / 2 + j;
deba@1720
   499
      }
deba@1720
   500
    }
deba@1720
   501
deba@1720
   502
    static int size(int s) {
deba@1720
   503
      return s * (s + 1) / 2;
deba@1720
   504
    }
deba@1720
   505
deba@1720
   506
    virtual void add(const Key& key) {
deba@2386
   507
      if (size(Parent::notifier()->id(key) + 1) >= int(values.size())) {
deba@2384
   508
	values.resize(size(Parent::notifier()->id(key) + 1));	
deba@1720
   509
      }
deba@1720
   510
    }
deba@1720
   511
deba@2305
   512
    virtual void add(const std::vector<Key>& keys) {
deba@2305
   513
      int new_size = 0;
deba@2386
   514
      for (int i = 0; i < int(keys.size()); ++i) {
deba@2384
   515
        if (size(Parent::notifier()->id(keys[i]) + 1) >= new_size) {
deba@2384
   516
          new_size = size(Parent::notifier()->id(keys[i]) + 1);	
deba@2305
   517
        }
deba@2305
   518
      }
deba@2386
   519
      if (new_size > int(values.size())) {
deba@2305
   520
        values.resize(new_size);
deba@2305
   521
      }
deba@2305
   522
    }
deba@2305
   523
deba@1720
   524
    virtual void erase(const Key&) {}
deba@1720
   525
deba@2305
   526
    virtual void erase(const std::vector<Key>&) {}
deba@2305
   527
deba@1720
   528
    virtual void build() {
deba@2384
   529
      values.resize(size(Parent::notifier()->maxId() + 1));
deba@1720
   530
    }
deba@1720
   531
deba@1720
   532
    virtual void clear() {
deba@1720
   533
      values.clear();
deba@1720
   534
    }   
deba@1720
   535
    
deba@1720
   536
  private:
deba@1720
   537
    std::vector<Value> values;
deba@1720
   538
  };
deba@2039
   539
  
deba@2039
   540
  ///\brief Dynamic Asymmetric Matrix Map.
deba@2039
   541
  ///
alpar@2072
   542
  ///\ingroup matrices
deba@2039
   543
  ///Dynamic Asymmetric Matrix Map.  Container for store values for each
deba@2039
   544
  ///ordered pair of containers items.  This data structure can store
deba@2039
   545
  ///data with different key types from different container types. It
deba@2039
   546
  ///increases the size of the container if the linked containers
deba@2039
   547
  ///content change, so it is updated automaticly whenever it is
deba@2039
   548
  ///needed.
deba@2039
   549
  ///
alpar@2260
   550
  ///This map meet with the concepts::ReferenceMatrixMap<typename K1,
deba@2039
   551
  ///typename K2, typename V, typename R, typename CR> called as
deba@2039
   552
  ///"ReferenceMatrixMap".
deba@2039
   553
  ///
deba@2376
   554
  ///\warning Do not use this map type when the two item sets are
deba@2376
   555
  ///equal or based on the same item set.
deba@2376
   556
  ///
deba@2039
   557
  ///\param _FirstContainer the desired type of first container. It is
deba@2039
   558
  ///ususally a Graph type, but can be any type with alteration
deba@2039
   559
  ///property.
deba@2039
   560
  ///  
deba@2039
   561
  ///\param _FirstContainerItem the nested type of the
deba@2039
   562
  ///FirstContainer. It is usually a graph item as Node, Edge,
deba@2039
   563
  ///etc. This type will be the FirstKey type.
deba@2039
   564
  ///
deba@2039
   565
  ///\param _SecondContainer the desired type of the second
deba@2039
   566
  ///container. It is usualy a Graph type, but can be any type with
deba@2039
   567
  ///alteration property.
deba@2039
   568
  ///
deba@2039
   569
  ///\param _SecondContainerItem the nested type of the
deba@2039
   570
  ///SecondContainer. It is usually a graph item such as Node, Edge,
deba@2039
   571
  ///UEdge, etc. This type will be the SecondKey type.
deba@2039
   572
  ///
deba@2039
   573
  ///\param _Value the type of the strored values in the container.
deba@2039
   574
  ///
deba@2039
   575
  /// \author Janos Nagy
deba@2039
   576
  template <typename _FirstContainer, typename _FirstContainerItem, 
deba@2039
   577
            typename _SecondContainer, typename _SecondContainerItem, 
deba@2039
   578
            typename _Value>
deba@2039
   579
  class DynamicAsymMatrixMap{
deba@2039
   580
  public:
deba@2039
   581
deba@2039
   582
    ///The first key type.
deba@2039
   583
    typedef _FirstContainerItem FirstKey;
deba@2039
   584
      
deba@2039
   585
    ///The second key type.
deba@2039
   586
    typedef _SecondContainerItem SecondKey;
deba@2039
   587
      
deba@2039
   588
    ///The value type of the map.
deba@2039
   589
    typedef _Value Value;
deba@2039
   590
      
deba@2039
   591
    ///Indicates it is a reference map.
deba@2039
   592
    typedef True ReferenceMapTag;
deba@2039
   593
    
deba@2039
   594
  protected:
deba@2039
   595
      
deba@2039
   596
    ///\brief Proxy class for the first key type.
deba@2039
   597
    ///
deba@2039
   598
    ///The proxy class belongs to the FirstKey type. It is necessary because
deba@2039
   599
    ///if one want use the same conatainer types and same nested types but on
deba@2039
   600
    ///other instances of containers than due to the type equiality of nested
deba@2039
   601
    ///types it requires a proxy mechanism. 
deba@2039
   602
    class FirstKeyProxy 
deba@2039
   603
      : protected 
deba@2039
   604
    ItemSetTraits<_FirstContainer,_FirstContainerItem>::
deba@2039
   605
    ItemNotifier::ObserverBase 
deba@2039
   606
    {
deba@2039
   607
        
deba@2039
   608
    public:
deba@2039
   609
deba@2039
   610
      friend class DynamicAsymMatrixMap;
deba@2039
   611
          
deba@2039
   612
      ///Constructor.
deba@2039
   613
      FirstKeyProxy(DynamicAsymMatrixMap& _map) : _owner(_map) { }
deba@2039
   614
    protected:
deba@2039
   615
deba@2039
   616
      ///\brief Add a new FirstKey to the map.
deba@2039
   617
      ///
deba@2039
   618
      ///It adds a new FirstKey to the map. It is called by the
deba@2039
   619
      ///observer notifier and it is ovverride the add() virtual
deba@2039
   620
      ///member function in the observer base. It will call the
deba@2039
   621
      ///maps addFirstKey() function.
deba@2039
   622
      virtual void add(const FirstKey& _firstKey){
deba@2039
   623
        _owner.addFirstKey(_firstKey);
deba@2039
   624
      }
deba@2039
   625
          
deba@2039
   626
      ///\brief Add more new FirstKey to the map.
deba@2039
   627
      ///
deba@2039
   628
      ///It adds more new FirstKey to the map. It is called by the
deba@2039
   629
      ///observer notifier and it is ovverride the add() virtual
deba@2039
   630
      ///member function in the observer base. It will call the
deba@2039
   631
      ///map's addFirstKeys() function.
deba@2039
   632
      virtual void add(const std::vector<FirstKey>& _firstKeys){
deba@2039
   633
        _owner.addFirstKeys(_firstKeys);
deba@2039
   634
      }
deba@2039
   635
          
deba@2039
   636
      ///\brief Erase a FirstKey from the map.
deba@2039
   637
      ///
deba@2039
   638
      ///Erase a FirstKey from the map. It called by the observer
deba@2039
   639
      ///notifier and it overrides the erase() virtual member
deba@2039
   640
      ///function of the observer base. It will call the map's
deba@2039
   641
      ///eraseFirstKey() function.
deba@2039
   642
      virtual void erase(const FirstKey& _firstKey){
deba@2039
   643
        _owner.eraseFirstKey(_firstKey);
deba@2039
   644
      }
deba@2039
   645
          
deba@2039
   646
      ///\brief Erase more FirstKey from the map.
deba@2039
   647
      ///
deba@2039
   648
      ///Erase more FirstKey from the map. It called by the
deba@2039
   649
      ///observer notifier and it overrides the erase() virtual
deba@2039
   650
      ///member function of the observer base. It will call the
deba@2039
   651
      ///map's eraseFirstKeys() function.
deba@2039
   652
      virtual void erase(const std::vector<FirstKey>& _firstKeys){
deba@2039
   653
        _owner.eraseFirstKeys(_firstKeys);
deba@2039
   654
      }
deba@2039
   655
          
deba@2039
   656
      ///\brief Builds the map.
deba@2039
   657
      ///
deba@2039
   658
      ///It buildes the map. It called by the observer notifier
deba@2039
   659
      ///and it overrides the build() virtual member function of
deba@2039
   660
      ///the observer base.  It will call the map's build()
deba@2039
   661
      ///function.
deba@2039
   662
      virtual void build() {
deba@2039
   663
        _owner.build();
deba@2039
   664
        //_owner.buildFirst();
deba@2039
   665
      }
deba@2039
   666
          
deba@2039
   667
      ///\brief Clear the map.
deba@2039
   668
      ///
deba@2039
   669
      ///It erases all items from the map. It called by the
deba@2039
   670
      ///observer notifier and it overrides the clear() virtual
deba@2039
   671
      ///memeber function of the observer base. It will call the
deba@2039
   672
      ///map's clear() function.
deba@2039
   673
      virtual void clear() {
deba@2039
   674
        _owner.clear();
deba@2039
   675
        //_owner.clearFirst();
deba@2039
   676
      }
deba@2039
   677
    private:
deba@2039
   678
          
deba@2039
   679
      ///The map type for it is linked.
deba@2039
   680
      DynamicAsymMatrixMap& _owner;
alpar@2072
   681
    };//END OF FIRSTKEYPROXY
deba@2039
   682
      
deba@2039
   683
      ///\brief Proxy class for the second key type.
deba@2039
   684
      ///
ladanyi@2047
   685
      ///The proxy class belongs to the SecondKey type. It is
deba@2039
   686
      ///necessary because if one want use the same conatainer types
deba@2039
   687
      ///and same nested types but on other instances of containers
deba@2039
   688
      ///than due to the type equiality of nested types it requires a
deba@2039
   689
      ///proxy mechanism.
deba@2039
   690
    class SecondKeyProxy
deba@2039
   691
      : protected 
deba@2039
   692
    ItemSetTraits<_SecondContainer, _SecondContainerItem>::
deba@2039
   693
    ItemNotifier::ObserverBase {
deba@2039
   694
        
deba@2039
   695
    public:
deba@2039
   696
deba@2039
   697
      friend class DynamicAsymMatrixMap;
deba@2039
   698
      ///Constructor.
deba@2039
   699
      SecondKeyProxy(DynamicAsymMatrixMap& _map) : _owner(_map) { }
deba@2039
   700
deba@2039
   701
    protected:
deba@2039
   702
          
deba@2039
   703
      ///\brief Add a new SecondKey to the map.
deba@2039
   704
      ///
deba@2039
   705
      ///It adds a new SecondKey to the map. It is called by the
deba@2039
   706
      ///observer notifier and it is ovverride the add() virtual
deba@2039
   707
      ///member function in the observer base. It will call the
deba@2039
   708
      ///maps addSecondKey() function.
deba@2039
   709
      virtual void add(const SecondKey& _secondKey){
deba@2039
   710
        _owner.addSecondKey(_secondKey);
deba@2039
   711
      }
deba@2039
   712
    
deba@2039
   713
      ///\brief Add more new SecondKey to the map.
deba@2039
   714
      ///
deba@2039
   715
      ///It adds more new SecondKey to the map. It is called by
deba@2039
   716
      ///the observer notifier and it is ovverride the add()
deba@2039
   717
      ///virtual member function in the observer base. It will
deba@2039
   718
      ///call the maps addSecondKeys() function.
deba@2039
   719
      virtual void add(const std::vector<SecondKey>& _secondKeys){
deba@2039
   720
        _owner.addSecondKeys(_secondKeys);
deba@2039
   721
      }
deba@2039
   722
          
deba@2039
   723
      ///\brief Erase a SecondKey from the map.
deba@2039
   724
      ///
deba@2039
   725
      ///Erase a SecondKey from the map. It called by the observer
deba@2039
   726
      ///notifier and it overrides the erase() virtual member
deba@2039
   727
      ///function of the observer base. It will call the map's
deba@2039
   728
      ///eraseSecondKey() function.
deba@2039
   729
      virtual void erase(const SecondKey& _secondKey){
deba@2039
   730
        _owner.eraseSecondKey(_secondKey);
deba@2039
   731
      }
deba@2039
   732
          
deba@2039
   733
      ///\brief Erase more SecondKeys from the map.
deba@2039
   734
      ///
deba@2039
   735
      ///Erase more SecondKey from the map. It called by the
deba@2039
   736
      ///observer notifier and it overrides the erase() virtual
deba@2039
   737
      ///member function of the observer base. It will call the
deba@2039
   738
      ///map's eraseSecondKeys() function.
deba@2039
   739
      virtual void erase(const std::vector<SecondKey>& _secondKeys){
deba@2039
   740
        _owner.eraseSecondKeys(_secondKeys);
deba@2039
   741
      }
deba@2039
   742
          
deba@2039
   743
      ///\brief Builds the map.
deba@2039
   744
      ///
deba@2039
   745
      ///It buildes the map. It called by the observer notifier
deba@2039
   746
      ///and it overrides the build() virtual member function of
deba@2039
   747
      ///the observer base.  It will call the map's build()
deba@2039
   748
      ///function.
deba@2039
   749
      virtual void build() {
deba@2039
   750
        _owner.build();
deba@2039
   751
      }
deba@2039
   752
          
deba@2039
   753
      ///\brief Clear the map.
deba@2039
   754
      ///
deba@2039
   755
      ///It erases all items from the map. It called by the
deba@2039
   756
      ///observer notifier and it overrides the clear() virtual
deba@2039
   757
      ///memeber function of the observer base. It will call the
deba@2039
   758
      ///map's clear() function.
deba@2039
   759
      virtual void clear() {
deba@2039
   760
        _owner.clear();
deba@2039
   761
        //_owner.clearFirst();
deba@2039
   762
      }
deba@2039
   763
    private:
deba@2039
   764
          
deba@2039
   765
      ///The type of map for which it is attached.
deba@2039
   766
      DynamicAsymMatrixMap& _owner;
alpar@2072
   767
    };//END OF SECONDKEYPROXY
deba@2039
   768
      
deba@2039
   769
  private:
deba@2039
   770
    
deba@2039
   771
    /// \e
deba@2039
   772
    typedef std::vector<Value> Container;
deba@2039
   773
      
deba@2039
   774
    ///The type of constainer which stores the values of the map.
deba@2039
   775
    typedef std::vector<Container> DContainer;
deba@2039
   776
deba@2039
   777
    ///The std:vector type which contains the data
deba@2039
   778
    DContainer values;
deba@2039
   779
      
deba@2039
   780
    ///Member for the first proxy class
deba@2039
   781
    FirstKeyProxy _first_key_proxy;
deba@2039
   782
      
deba@2039
   783
    ///Member for the second proxy class
deba@2039
   784
    SecondKeyProxy _second_key_proxy;
deba@2039
   785
deba@2039
   786
  public:
deba@2039
   787
    
deba@2039
   788
    ///The refernce type of the map.
deba@2039
   789
    typedef typename Container::reference Reference;
deba@2039
   790
      
deba@2039
   791
    ///The const reference type of the constainer.
deba@2039
   792
    typedef typename Container::const_reference ConstReference;
deba@2039
   793
deba@2039
   794
    ///\brief Constructor what create the map for the two containers type.
deba@2039
   795
    ///
deba@2039
   796
    ///Creates the matrix map and initialize the values with Value()
deba@2039
   797
    DynamicAsymMatrixMap(const _FirstContainer& _firstContainer, 
deba@2039
   798
                  const _SecondContainer& _secondContainer)
deba@2039
   799
      : values(DContainer(_firstContainer.maxId(FirstKey())+1,
deba@2039
   800
                          Container(_secondContainer.maxId(SecondKey())+1))),
deba@2039
   801
        _first_key_proxy(*this),
deba@2039
   802
        _second_key_proxy(*this)
deba@2039
   803
    {
deba@2384
   804
      _first_key_proxy.attach(_firstContainer.notifier(FirstKey()));
deba@2384
   805
      _second_key_proxy.attach(_secondContainer.notifier(SecondKey()));
deba@2039
   806
    }
deba@2039
   807
deba@2039
   808
    ///\brief Constructor what create the map for the two containers type.
deba@2039
   809
    ///
deba@2039
   810
    ///Creates the matrix map and initialize the values with the given _value
deba@2039
   811
    DynamicAsymMatrixMap(const _FirstContainer& _firstContainer, 
deba@2039
   812
                  const _SecondContainer& _secondContainer, 
deba@2039
   813
                  const Value& _value)
deba@2039
   814
      : values(DContainer(_firstContainer.maxId(FirstKey())+1,
deba@2039
   815
                          Container(_secondContainer.maxId(SecondKey())+1,
deba@2039
   816
                                    _value))),
deba@2039
   817
        _first_key_proxy(*this),
deba@2039
   818
        _second_key_proxy(*this)
deba@2039
   819
    {
deba@2384
   820
      _first_key_proxy.attach(_firstContainer.notifier(FirstKey()));
deba@2384
   821
      _second_key_proxy.attach(_secondContainer.notifier(SecondKey()));
deba@2039
   822
    }
deba@2039
   823
      
deba@2039
   824
    ///\brief Copy constructor.
deba@2039
   825
    ///
deba@2039
   826
    ///The copy constructor of the map.
deba@2039
   827
    DynamicAsymMatrixMap(const DynamicAsymMatrixMap& _copy) 
deba@2039
   828
      : _first_key_proxy(*this), _second_key_proxy(*this) {
deba@2039
   829
      if(_copy._first_key_proxy.attached() && 
deba@2039
   830
         _copy._second_key_proxy.attached()){
deba@2384
   831
        _first_key_proxy.attach(*_copy._first_key_proxy.notifier());
deba@2384
   832
        _second_key_proxy.attach(*_copy._second_key_proxy.notifier());
deba@2039
   833
        values = _copy.values;
deba@2039
   834
      }
deba@2039
   835
    }
deba@2039
   836
      
deba@2039
   837
    ///\brief Destructor
deba@2039
   838
    ///
deba@2039
   839
    ///Destructor what detach() from the attached objects.  May this
deba@2039
   840
    ///function is not necessary because the destructor of
deba@2039
   841
    ///ObserverBase do the same.
deba@2039
   842
    ~DynamicAsymMatrixMap() {
deba@2039
   843
      if(_first_key_proxy.attached()){
deba@2039
   844
        _first_key_proxy.detach();
deba@2039
   845
      }
deba@2039
   846
      if(_second_key_proxy.attached()){
deba@2039
   847
        _second_key_proxy.detach();
deba@2039
   848
      }
deba@2039
   849
    }
deba@2039
   850
      
deba@2039
   851
    ///\brief Gives back the value assigned to the \c first - \c
deba@2039
   852
    ///second ordered pair.
deba@2039
   853
    ///
deba@2039
   854
    ///Gives back the value assigned to the \c first - \c second
deba@2039
   855
    ///ordered pair.
deba@2039
   856
    Reference operator()(const FirstKey& _first, const SecondKey& _second) {
deba@2384
   857
      return values[_first_key_proxy.notifier()->id(_first)]
deba@2384
   858
        [_second_key_proxy.notifier()->id(_second)];
deba@2039
   859
    }
deba@2039
   860
deba@2039
   861
    ///\brief Gives back the value assigned to the \c first - \c
deba@2039
   862
    ///second ordered pair.
deba@2039
   863
    ///
deba@2039
   864
    ///Gives back the value assigned to the \c first - \c second
deba@2039
   865
    ///ordered pair.
deba@2039
   866
    ConstReference operator()(const FirstKey& _first, 
deba@2039
   867
                              const SecondKey& _second) const {
deba@2384
   868
      return values[_first_key_proxy.notifier()->id(_first)]
deba@2384
   869
        [_second_key_proxy.notifier()->id(_second)];
deba@2039
   870
    }
deba@2039
   871
deba@2039
   872
    ///\brief Setter function for this matrix map.
deba@2039
   873
    ///
deba@2039
   874
    ///Setter function for this matrix map.
deba@2039
   875
    void set(const FirstKey& first, const SecondKey& second, 
deba@2039
   876
             const Value& value){
deba@2384
   877
      values[_first_key_proxy.notifier()->id(first)]
deba@2384
   878
        [_second_key_proxy.notifier()->id(second)] = value;
deba@2039
   879
    }
deba@2039
   880
deba@2039
   881
    ///\brief The assignement operator.
deba@2039
   882
    ///
deba@2039
   883
    ///It allow to assign a map to an other. It
deba@2039
   884
    DynamicAsymMatrixMap& operator=(const DynamicAsymMatrixMap& _cmap){
deba@2039
   885
      return operator=<DynamicAsymMatrixMap>(_cmap);
deba@2039
   886
    }
deba@2039
   887
      
deba@2039
   888
    ///\brief Template assignement operator.
deba@2039
   889
    ///
deba@2039
   890
    ///It copy the element of the given map to its own container.  The
deba@2039
   891
    ///type of the two map shall be the same.
deba@2039
   892
    template <typename CMap>
deba@2039
   893
    DynamicAsymMatrixMap& operator=(const CMap& _cdmap){
alpar@2260
   894
      checkConcept<concepts::ReadMatrixMap<FirstKey, SecondKey, Value>, CMap>();
deba@2039
   895
      const typename FirstKeyProxy::Notifier* notifierFirstKey = 
deba@2384
   896
        _first_key_proxy.notifier();
deba@2039
   897
      const typename SecondKeyProxy::Notifier* notifierSecondKey = 
deba@2384
   898
        _second_key_proxy.notifier();
deba@2039
   899
      FirstKey itemFirst;
deba@2039
   900
      SecondKey itemSecond;
deba@2039
   901
      for(notifierFirstKey->first(itemFirst); itemFirst != INVALID; 
deba@2039
   902
          notifierFirstKey->next(itemFirst)){
deba@2039
   903
        for(notifierSecondKey->first(itemSecond); itemSecond != INVALID; 
deba@2039
   904
            notifierSecondKey->next(itemSecond)){
deba@2039
   905
          set(itemFirst, itemSecond, _cdmap(itemFirst,itemSecond));
deba@2039
   906
        }
deba@2039
   907
      }
deba@2039
   908
      return *this;
deba@2039
   909
    }
deba@2039
   910
      
deba@2039
   911
  protected:
deba@2039
   912
    
deba@2039
   913
    ///\brief Add a new FirstKey to the map.
deba@2039
   914
    ///
deba@2039
   915
    ///It adds a new FirstKey to the map. It is called by the observer
deba@2039
   916
    ///class belongs to the FirstKey type.
deba@2039
   917
    void addFirstKey(const FirstKey& firstKey) {
deba@2386
   918
      int size = int(values.size());
deba@2384
   919
      if( _first_key_proxy.notifier()->id(firstKey)+1 >= size ){
deba@2384
   920
        values.resize(_first_key_proxy.notifier()->id(firstKey)+1);
deba@2386
   921
        if( int(values[0].size()) != 0 ){
deba@2386
   922
          int innersize = int(values[0].size());
deba@2386
   923
          for(int i = size; i < int(values.size());++i){
deba@2039
   924
            (values[i]).resize(innersize);
deba@2039
   925
          }
deba@2384
   926
        }else if(_second_key_proxy.notifier()->maxId() >= 0){
deba@2384
   927
          int innersize = _second_key_proxy.notifier()->maxId();
deba@2386
   928
          for(int i = 0; i < int(values.size()); ++i){
deba@2039
   929
            values[0].resize(innersize);
deba@2039
   930
          }
deba@2039
   931
        }
deba@2039
   932
      }
deba@2039
   933
    }
deba@2039
   934
deba@2039
   935
    ///\brief Adds more new FirstKeys to the map.
deba@2039
   936
    ///
deba@2039
   937
    ///It adds more new FirstKeys to the map. It called by the
deba@2039
   938
    ///observer class belongs to the FirstKey type.
deba@2039
   939
    void addFirstKeys(const std::vector<FirstKey>& firstKeys){
deba@2039
   940
      int max = values.size() - 1;
deba@2386
   941
      for(int i = 0; i < int(firstKeys.size()); ++i){
deba@2384
   942
        int id = _first_key_proxy.notifier()->id(firstKeys[i]);
deba@2039
   943
        if(max < id){
deba@2039
   944
          max = id;
deba@2039
   945
        }
deba@2039
   946
      }
deba@2386
   947
      int size = int(values.size());
deba@2039
   948
      if(max >= size){
deba@2039
   949
        values.resize(max + 1);
deba@2386
   950
        if( int(values[0].size()) != 0){
deba@2386
   951
          int innersize = int(values[0].size());
deba@2386
   952
          for(int i = size; i < (max + 1); ++i){
deba@2039
   953
            values[i].resize(innersize);
deba@2039
   954
          }
deba@2384
   955
        }else if(_second_key_proxy.notifier()->maxId() >= 0){
deba@2384
   956
          int innersize = _second_key_proxy.notifier()->maxId();
deba@2386
   957
          for(int i = 0; i < int(values.size()); ++i){
deba@2039
   958
            values[i].resize(innersize);
deba@2039
   959
          }
deba@2039
   960
        }
deba@2039
   961
      }
deba@2039
   962
    }
deba@2039
   963
deba@2039
   964
    ///\brief Add a new SecondKey to the map.
deba@2039
   965
    ///
deba@2039
   966
    ///It adds a new SecondKey to the map. It is called by the
deba@2039
   967
    ///observer class belongs to the SecondKey type.
deba@2039
   968
    void addSecondKey(const SecondKey& secondKey) {
deba@2039
   969
      if(values.size() == 0){
deba@2039
   970
        return;
deba@2039
   971
      }
deba@2384
   972
      int id = _second_key_proxy.notifier()->id(secondKey);
deba@2386
   973
      if(id >= int(values[0].size())){
deba@2386
   974
        for(int i = 0; i < int(values.size());++i){
deba@2039
   975
          values[i].resize(id+1);
deba@2039
   976
        }
deba@2039
   977
      }
deba@2039
   978
    }
deba@2039
   979
        
deba@2039
   980
    ///\brief Adds more new SecondKeys to the map.
deba@2039
   981
    ///
deba@2039
   982
    ///It adds more new SecondKeys to the map. It called by the
deba@2039
   983
    ///observer class belongs to the SecondKey type.
deba@2039
   984
    void addSecondKeys(const std::vector<SecondKey>& secondKeys){
deba@2039
   985
      if(values.size() == 0){
deba@2039
   986
        return;
deba@2039
   987
      }
deba@2039
   988
      int max = values[0].size();
deba@2386
   989
      for(int i = 0; i < int(secondKeys.size()); ++i){
deba@2384
   990
        int id = _second_key_proxy.notifier()->id(secondKeys[i]);
deba@2039
   991
        if(max < id){
deba@2039
   992
          max = id;
deba@2039
   993
        }
deba@2039
   994
      }
deba@2386
   995
      if(max > int(values[0].size())){
deba@2386
   996
        for(int i = 0; i < int(values.size()); ++i){
deba@2039
   997
          values[i].resize(max + 1);
deba@2039
   998
        }
deba@2039
   999
      }
deba@2039
  1000
    }
deba@2039
  1001
    
deba@2039
  1002
    ///\brief Erase a FirstKey from the map.
deba@2039
  1003
    ///
deba@2039
  1004
    ///Erase a FirstKey from the map. It called by the observer
deba@2039
  1005
    ///class belongs to the FirstKey type.
deba@2039
  1006
    void eraseFirstKey(const FirstKey& first) {
deba@2384
  1007
      int id = _first_key_proxy.notifier()->id(first);
deba@2386
  1008
      for(int i = 0; i < int(values[id].size()); ++i){
deba@2039
  1009
        values[id][i] = Value();
deba@2039
  1010
      }
deba@2039
  1011
    }
deba@2039
  1012
        
deba@2039
  1013
    ///\brief Erase more FirstKey from the map.
deba@2039
  1014
    ///
deba@2039
  1015
    ///Erase more FirstKey from the map. It called by the observer
deba@2039
  1016
    ///class belongs to the FirstKey type.
deba@2039
  1017
    void eraseFirstKeys(const std::vector<FirstKey>& firstKeys) {
deba@2386
  1018
      for(int j = 0; j < int(firstKeys.size()); ++j){
deba@2384
  1019
        int id = _first_key_proxy.notifier()->id(firstKeys[j]);
deba@2386
  1020
        for(int i = 0; i < int(values[id].size()); ++i){
deba@2039
  1021
          values[id][i] = Value();
deba@2039
  1022
        }
deba@2039
  1023
      }
deba@2039
  1024
    }
deba@2039
  1025
deba@2039
  1026
    ///\brief Erase a SecondKey from the map.
deba@2039
  1027
    ///
deba@2039
  1028
    ///Erase a SecondKey from the map. It called by the observer class
deba@2039
  1029
    ///belongs to the SecondKey type.
deba@2039
  1030
    void eraseSecondKey(const SecondKey& second) {
deba@2039
  1031
      if(values.size() == 0){
deba@2039
  1032
        return;
deba@2039
  1033
      }
deba@2384
  1034
      int id = _second_key_proxy.notifier()->id(second);
deba@2386
  1035
      for(int i = 0; i < int(values.size()); ++i){
deba@2039
  1036
        values[i][id] = Value();
deba@2039
  1037
      }
deba@2039
  1038
    }
deba@2039
  1039
        
deba@2039
  1040
    ///\brief Erase more SecondKey from the map.
deba@2039
  1041
    ///
deba@2039
  1042
    ///Erase more SecondKey from the map. It called by the observer
deba@2039
  1043
    ///class belongs to the SecondKey type.
deba@2039
  1044
    void eraseSecondKeys(const std::vector<SecondKey>& secondKeys) {
deba@2039
  1045
      if(values.size() == 0){
deba@2039
  1046
        return;
deba@2039
  1047
      }
deba@2386
  1048
      for(int j = 0; j < int(secondKeys.size()); ++j){
deba@2384
  1049
        int id = _second_key_proxy.notifier()->id(secondKeys[j]);
deba@2386
  1050
        for(int i = 0; i < int(values.size()); ++i){
deba@2039
  1051
          values[i][id] = Value();
deba@2039
  1052
        }
deba@2039
  1053
      }
deba@2039
  1054
    }
deba@2039
  1055
deba@2039
  1056
    ///\brief Builds the map.
deba@2039
  1057
    ///
deba@2039
  1058
    ///It buildes the map. It is called by the observer class belongs
deba@2039
  1059
    ///to the FirstKey or SecondKey type.
deba@2039
  1060
    void build() {
deba@2384
  1061
      values.resize(_first_key_proxy.notifier()->maxId());
deba@2386
  1062
      for(int i = 0; i< int(values.size()); ++i){
deba@2384
  1063
        values[i].resize(_second_key_proxy.notifier()->maxId());
deba@2039
  1064
      }
deba@2039
  1065
    }
deba@2039
  1066
    
deba@2039
  1067
    ///\brief Clear the map.
deba@2039
  1068
    ///
deba@2039
  1069
    ///It erases all items from the map. It is called by the observer class
deba@2039
  1070
    ///belongs to the FirstKey or SecondKey type.
deba@2039
  1071
    void clear() {
deba@2386
  1072
      for(int i = 0; i < int(values.size()); ++i) {
deba@2039
  1073
        values[i].clear();
deba@2039
  1074
      }
deba@2039
  1075
      values.clear();
deba@2039
  1076
    }
deba@2039
  1077
 
deba@2039
  1078
  };
deba@2039
  1079
deba@2039
  1080
deba@1720
  1081
deba@1720
  1082
}
deba@1720
  1083
deba@1720
  1084
#endif