gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Added missing inheritances and map-creator functions.
0 1 0
default
1 file changed with 60 insertions and 8 deletions:
↑ Collapse diff ↑
Ignore white space 192 line context
... ...
@@ -70,462 +70,514 @@
70 70
  };
71 71

	
72 72
  ///Returns a \c NullMap class
73 73

	
74 74
  ///This function just returns a \c NullMap class.
75 75
  ///\relates NullMap
76 76
  template <typename K, typename V> 
77 77
  NullMap<K, V> nullMap() {
78 78
    return NullMap<K, V>();
79 79
  }
80 80

	
81 81

	
82 82
  /// Constant map.
83 83

	
84 84
  /// This is a readable map which assigns a specified value to each key.
85 85
  /// In other aspects it is equivalent to the \c NullMap.
86 86
  template<typename K, typename T>
87 87
  class ConstMap : public MapBase<K, T> {
88 88
  private:
89 89
    T v;
90 90
  public:
91 91

	
92 92
    typedef MapBase<K, T> Parent;
93 93
    typedef typename Parent::Key Key;
94 94
    typedef typename Parent::Value Value;
95 95

	
96 96
    /// Default constructor
97 97

	
98 98
    /// Default constructor.
99 99
    /// The value of the map will be uninitialized. 
100 100
    /// (More exactly it will be default constructed.)
101 101
    ConstMap() {}
102 102
    
103 103
    /// Constructor with specified initial value
104 104

	
105 105
    /// Constructor with specified initial value.
106 106
    /// \param _v is the initial value of the map.
107 107
    ConstMap(const T &_v) : v(_v) {}
108 108
    
109 109
    ///\e
110 110
    T operator[](const K&) const { return v; }
111 111

	
112 112
    ///\e
113 113
    void setAll(const T &t) {
114 114
      v = t;
115 115
    }    
116 116

	
117 117
    template<typename T1>
118 118
    ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
119 119
  };
120 120

	
121 121
  ///Returns a \c ConstMap class
122 122

	
123 123
  ///This function just returns a \c ConstMap class.
124 124
  ///\relates ConstMap
125 125
  template<typename K, typename V> 
126 126
  inline ConstMap<K, V> constMap(const V &v) {
127 127
    return ConstMap<K, V>(v);
128 128
  }
129 129

	
130 130

	
131 131
  template<typename T, T v>
132 132
  struct Const { };
133 133

	
134 134
  /// Constant map with inlined constant value.
135 135

	
136 136
  /// This is a readable map which assigns a specified value to each key.
137 137
  /// In other aspects it is equivalent to the \c NullMap.
138 138
  template<typename K, typename V, V v>
139 139
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
140 140
  public:
141 141
    typedef MapBase<K, V> Parent;
142 142
    typedef typename Parent::Key Key;
143 143
    typedef typename Parent::Value Value;
144 144

	
145 145
    ConstMap() { }
146 146
    ///\e
147 147
    V operator[](const K&) const { return v; }
148 148
    ///\e
149 149
    void set(const K&, const V&) { }
150 150
  };
151 151

	
152 152
  ///Returns a \c ConstMap class
153 153

	
154 154
  ///This function just returns a \c ConstMap class with inlined value.
155 155
  ///\relates ConstMap
156 156
  template<typename K, typename V, V v> 
157 157
  inline ConstMap<K, Const<V, v> > constMap() {
158 158
    return ConstMap<K, Const<V, v> >();
159 159
  }
160 160

	
161 161
  ///Map based on std::map
162 162

	
163 163
  ///This is essentially a wrapper for \c std::map with addition that
164 164
  ///you can specify a default value different from \c Value().
165 165
  template <typename K, typename T, typename Compare = std::less<K> >
166
  class StdMap {
166
  class StdMap : public MapBase<K, T> {
167 167
    template <typename K1, typename T1, typename C1>
168 168
    friend class StdMap;
169 169
  public:
170 170

	
171
    typedef True ReferenceMapTag;
171
    typedef MapBase<K, T> Parent;
172 172
    ///\e
173
    typedef K Key;
173
    typedef typename Parent::Key Key;
174 174
    ///\e
175
    typedef T Value;
175
    typedef typename Parent::Value Value;
176 176
    ///\e
177 177
    typedef T& Reference;
178 178
    ///\e
179 179
    typedef const T& ConstReference;
180 180

	
181
    typedef True ReferenceMapTag;
182

	
181 183
  private:
182 184
    
183 185
    typedef std::map<K, T, Compare> Map;
184 186
    Value _value;
185 187
    Map _map;
186 188

	
187 189
  public:
188 190

	
189 191
    /// Constructor with specified default value
190 192
    StdMap(const T& value = T()) : _value(value) {}
191 193
    /// \brief Constructs the map from an appropriate std::map, and explicitly
192 194
    /// specifies a default value.
193 195
    template <typename T1, typename Comp1>
194 196
    StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) 
195 197
      : _map(map.begin(), map.end()), _value(value) {}
196 198
    
197 199
    /// \brief Constructs a map from an other StdMap.
198 200
    template<typename T1, typename Comp1>
199 201
    StdMap(const StdMap<Key, T1, Comp1> &c) 
200 202
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
201 203

	
202 204
  private:
203 205

	
204 206
    StdMap& operator=(const StdMap&);
205 207

	
206 208
  public:
207 209

	
208 210
    ///\e
209 211
    Reference operator[](const Key &k) {
210 212
      typename Map::iterator it = _map.lower_bound(k);
211 213
      if (it != _map.end() && !_map.key_comp()(k, it->first))
212 214
	return it->second;
213 215
      else
214 216
	return _map.insert(it, std::make_pair(k, _value))->second;
215 217
    }
216 218

	
217 219
    /// \e 
218 220
    ConstReference operator[](const Key &k) const {
219 221
      typename Map::const_iterator it = _map.find(k);
220 222
      if (it != _map.end())
221 223
	return it->second;
222 224
      else
223 225
	return _value;
224 226
    }
225 227

	
226 228
    /// \e 
227 229
    void set(const Key &k, const T &t) {
228 230
      typename Map::iterator it = _map.lower_bound(k);
229 231
      if (it != _map.end() && !_map.key_comp()(k, it->first))
230 232
	it->second = t;
231 233
      else
232 234
	_map.insert(it, std::make_pair(k, t));
233 235
    }
234 236

	
235 237
    /// \e
236 238
    void setAll(const T &t) {
237 239
      _value = t;
238 240
      _map.clear();
239 241
    }    
240 242

	
241 243
  };
244
  
245
  ///Returns a \ref StdMap class
246

	
247
  ///This function just returns a \ref StdMap class with specified 
248
  ///default value.
249
  ///\relates StdMap
250
  template<typename K, typename V, typename Compare = std::less<K> > 
251
  inline StdMap<K, V, Compare> stdMap(const V& value = V()) {
252
    return StdMap<K, V, Compare>(value);
253
  }
254

	
255
  ///Returns a \ref StdMap class created from an appropriate std::map
256

	
257
  ///This function just returns a \ref StdMap class created from an 
258
  ///appropriate std::map.
259
  ///\relates StdMap
260
  template<typename K, typename V, typename Compare = std::less<K> > 
261
  inline StdMap<K, V, Compare> stdMap( const std::map<K, V, Compare> &map, 
262
                                       const V& value = V() ) {
263
    return StdMap<K, V, Compare>(map, value);
264
  }
242 265

	
243 266
  /// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt>
244 267
  ///
245 268
  /// The current map has the <tt>[0..size-1]</tt> keyset and the values
246 269
  /// are stored in a \c std::vector<T>  container. It can be used with
247 270
  /// some data structures, for example \c UnionFind, \c BinHeap, when 
248 271
  /// the used items are small integer numbers. 
249 272
  ///
250 273
  /// \todo Revise its name
251 274
  template <typename T>
252
  class IntegerMap {
275
  class IntegerMap : public MapBase<int, T> {
253 276

	
254 277
    template <typename T1>
255 278
    friend class IntegerMap;
256 279

	
257 280
  public:
258 281

	
259
    typedef True ReferenceMapTag;
282
    typedef MapBase<int, T> Parent;
260 283
    ///\e
261
    typedef int Key;
284
    typedef typename Parent::Key Key;
262 285
    ///\e
263
    typedef T Value;
286
    typedef typename Parent::Value Value;
264 287
    ///\e
265 288
    typedef T& Reference;
266 289
    ///\e
267 290
    typedef const T& ConstReference;
268 291

	
292
    typedef True ReferenceMapTag;
293

	
269 294
  private:
270 295
    
271 296
    typedef std::vector<T> Vector;
272 297
    Vector _vector;
273 298

	
274 299
  public:
275 300

	
276 301
    /// Constructor with specified default value
277 302
    IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
278 303

	
279 304
    /// \brief Constructs the map from an appropriate std::vector.
280 305
    template <typename T1>
281 306
    IntegerMap(const std::vector<T1>& vector) 
282 307
      : _vector(vector.begin(), vector.end()) {}
283 308
    
284 309
    /// \brief Constructs a map from an other IntegerMap.
285 310
    template <typename T1>
286 311
    IntegerMap(const IntegerMap<T1> &c) 
287 312
      : _vector(c._vector.begin(), c._vector.end()) {}
288 313

	
289 314
    /// \brief Resize the container
290 315
    void resize(int size, const T& value = T()) {
291 316
      _vector.resize(size, value);
292 317
    }
293 318

	
294 319
  private:
295 320

	
296 321
    IntegerMap& operator=(const IntegerMap&);
297 322

	
298 323
  public:
299 324

	
300 325
    ///\e
301 326
    Reference operator[](Key k) {
302 327
      return _vector[k];
303 328
    }
304 329

	
305 330
    /// \e 
306 331
    ConstReference operator[](Key k) const {
307 332
      return _vector[k];
308 333
    }
309 334

	
310 335
    /// \e 
311 336
    void set(const Key &k, const T& t) {
312 337
      _vector[k] = t;
313 338
    }
314 339

	
315 340
  };
341
  
342
  ///Returns an \ref IntegerMap class
343

	
344
  ///This function just returns an \ref IntegerMap class.
345
  ///\relates IntegerMap
346
  template<typename T>
347
  inline IntegerMap<T> integerMap(int size = 0, const T& value = T()) {
348
    return IntegerMap<T>(size, value);
349
  }
316 350

	
317 351
  /// @}
318 352

	
319 353
  /// \addtogroup map_adaptors
320 354
  /// @{
321 355

	
322 356
  /// \brief Identity map.
323 357
  ///
324 358
  /// This map gives back the given key as value without any
325 359
  /// modification. 
326 360
  template <typename T>
327 361
  class IdentityMap : public MapBase<T, T> {
328 362
  public:
329 363
    typedef MapBase<T, T> Parent;
330 364
    typedef typename Parent::Key Key;
331 365
    typedef typename Parent::Value Value;
332 366

	
333 367
    /// \e
334 368
    const T& operator[](const T& t) const {
335 369
      return t;
336 370
    }
337 371
  };
338 372

	
339 373
  ///Returns an \c IdentityMap class
340 374

	
341 375
  ///This function just returns an \c IdentityMap class.
342 376
  ///\relates IdentityMap
343 377
  template<typename T>
344 378
  inline IdentityMap<T> identityMap() {
345 379
    return IdentityMap<T>();
346 380
  }
347 381
  
348 382

	
349 383
  ///\brief Convert the \c Value of a map to another type using
350 384
  ///the default conversion.
351 385
  ///
352 386
  ///This \c concepts::ReadMap "read only map"
353 387
  ///converts the \c Value of a map to type \c T.
354 388
  ///Its \c Key is inherited from \c M.
355 389
  template <typename M, typename T> 
356 390
  class ConvertMap : public MapBase<typename M::Key, T> {
357 391
    const M& m;
358 392
  public:
359 393
    typedef MapBase<typename M::Key, T> Parent;
360 394
    typedef typename Parent::Key Key;
361 395
    typedef typename Parent::Value Value;
362 396

	
363 397
    ///Constructor
364 398

	
365 399
    ///Constructor.
366 400
    ///\param _m is the underlying map.
367 401
    ConvertMap(const M &_m) : m(_m) {};
368 402

	
369 403
    /// \brief The subscript operator.
370 404
    ///
371 405
    /// The subscript operator.
372 406
    Value operator[](const Key& k) const {return m[k];}
373 407
  };
374 408
  
375 409
  ///Returns a \c ConvertMap class
376 410

	
377 411
  ///This function just returns a \c ConvertMap class.
378 412
  ///\relates ConvertMap
379 413
  template<typename T, typename M>
380 414
  inline ConvertMap<M, T> convertMap(const M &m) {
381 415
    return ConvertMap<M, T>(m);
382 416
  }
383 417

	
384 418
  ///Simple wrapping of a map
385 419

	
386 420
  ///This \ref concepts::ReadMap "read only map" returns the simple
387 421
  ///wrapping of the given map. Sometimes the reference maps cannot be
388 422
  ///combined with simple read maps. This map adaptor wraps the given
389 423
  ///map to simple read map.
390 424
  ///
391 425
  ///\sa SimpleWriteMap
392 426
  ///
393 427
  /// \todo Revise the misleading name 
394 428
  template<typename M> 
395 429
  class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
396 430
    const M& m;
397 431

	
398 432
  public:
399 433
    typedef MapBase<typename M::Key, typename M::Value> Parent;
400 434
    typedef typename Parent::Key Key;
401 435
    typedef typename Parent::Value Value;
402 436

	
403 437
    ///Constructor
404 438
    SimpleMap(const M &_m) : m(_m) {};
405 439
    ///\e
406 440
    Value operator[](Key k) const {return m[k];}
407 441
  };
442
  
443
  ///Returns a \ref SimpleMap class
444

	
445
  ///This function just returns a \ref SimpleMap class.
446
  ///\relates SimpleMap
447
  template<typename M>
448
  inline SimpleMap<M> simpleMap(const M &m) {
449
    return SimpleMap<M>(m);
450
  }
408 451

	
409 452
  ///Simple writable wrapping of a map
410 453

	
411 454
  ///This \ref concepts::WriteMap "write map" returns the simple
412 455
  ///wrapping of the given map. Sometimes the reference maps cannot be
413 456
  ///combined with simple read-write maps. This map adaptor wraps the
414 457
  ///given map to simple read-write map.
415 458
  ///
416 459
  ///\sa SimpleMap
417 460
  ///
418 461
  /// \todo Revise the misleading name
419 462
  template<typename M> 
420 463
  class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
421 464
    M& m;
422 465

	
423 466
  public:
424 467
    typedef MapBase<typename M::Key, typename M::Value> Parent;
425 468
    typedef typename Parent::Key Key;
426 469
    typedef typename Parent::Value Value;
427 470

	
428 471
    ///Constructor
429 472
    SimpleWriteMap(M &_m) : m(_m) {};
430 473
    ///\e
431 474
    Value operator[](Key k) const {return m[k];}
432 475
    ///\e
433 476
    void set(Key k, const Value& c) { m.set(k, c); }
434 477
  };
435 478

	
479
  ///Returns a \ref SimpleWriteMap class
480

	
481
  ///This function just returns a \ref SimpleWriteMap class.
482
  ///\relates SimpleWriteMap
483
  template<typename M>
484
  inline SimpleWriteMap<M> simpleWriteMap(M &m) {
485
    return SimpleWriteMap<M>(m);
486
  }
487

	
436 488
  ///Sum of two maps
437 489

	
438 490
  ///This \c concepts::ReadMap "read only map" returns the sum of the two
439 491
  ///given maps.
440 492
  ///Its \c Key and \c Value are inherited from \c M1.
441 493
  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
442 494
  template<typename M1, typename M2> 
443 495
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
444 496
    const M1& m1;
445 497
    const M2& m2;
446 498

	
447 499
  public:
448 500
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
449 501
    typedef typename Parent::Key Key;
450 502
    typedef typename Parent::Value Value;
451 503

	
452 504
    ///Constructor
453 505
    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
454 506
    ///\e
455 507
    Value operator[](Key k) const {return m1[k]+m2[k];}
456 508
  };
457 509
  
458 510
  ///Returns an \c AddMap class
459 511

	
460 512
  ///This function just returns an \c AddMap class.
461 513
  ///\todo How to call these type of functions?
462 514
  ///
463 515
  ///\relates AddMap
464 516
  template<typename M1, typename M2> 
465 517
  inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
466 518
    return AddMap<M1, M2>(m1,m2);
467 519
  }
468 520

	
469 521
  ///Shift a map with a constant.
470 522

	
471 523
  ///This \c concepts::ReadMap "read only map" returns the sum of the
472 524
  ///given map and a constant value.
473 525
  ///Its \c Key and \c Value are inherited from \c M.
474 526
  ///
475 527
  ///Actually,
476 528
  ///\code
477 529
  ///  ShiftMap<X> sh(x,v);
478 530
  ///\endcode
479 531
  ///is equivalent to
480 532
  ///\code
481 533
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
482 534
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
483 535
  ///\endcode
484 536
  ///
485 537
  ///\sa ShiftWriteMap
486 538
  template<typename M, typename C = typename M::Value> 
487 539
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
488 540
    const M& m;
489 541
    C v;
490 542
  public:
491 543
    typedef MapBase<typename M::Key, typename M::Value> Parent;
492 544
    typedef typename Parent::Key Key;
493 545
    typedef typename Parent::Value Value;
494 546

	
495 547
    ///Constructor
496 548

	
497 549
    ///Constructor.
498 550
    ///\param _m is the undelying map.
499 551
    ///\param _v is the shift value.
500 552
    ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
501 553
    ///\e
502 554
    Value operator[](Key k) const {return m[k] + v;}
503 555
  };
504 556

	
505 557
  ///Shift a map with a constant (ReadWrite version).
506 558

	
507 559
  ///This \c concepts::ReadWriteMap "read-write map" returns the sum of the
508 560
  ///given map and a constant value. It makes also possible to write the map.
509 561
  ///Its \c Key and \c Value are inherited from \c M.
510 562
  ///
511 563
  ///\sa ShiftMap
512 564
  template<typename M, typename C = typename M::Value> 
513 565
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
514 566
    M& m;
515 567
    C v;
516 568
  public:
517 569
    typedef MapBase<typename M::Key, typename M::Value> Parent;
518 570
    typedef typename Parent::Key Key;
519 571
    typedef typename Parent::Value Value;
520 572

	
521 573
    ///Constructor
522 574

	
523 575
    ///Constructor.
524 576
    ///\param _m is the undelying map.
525 577
    ///\param _v is the shift value.
526 578
    ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
527 579
    /// \e
528 580
    Value operator[](Key k) const {return m[k] + v;}
529 581
    /// \e
530 582
    void set(Key k, const Value& c) { m.set(k, c - v); }
531 583
  };
0 comments (0 inline)