gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Minor bug fix in maps.h. Fixed the existing two variants of stdMap() and added two new variants.
0 1 0
default
1 file changed with 26 insertions and 5 deletions:
↑ Collapse diff ↑
Ignore white space 256 line context
... ...
@@ -125,270 +125,291 @@
125 125
  ///\relates ConstMap
126 126
  template<typename K, typename V> 
127 127
  inline ConstMap<K, V> constMap(const V &v) {
128 128
    return ConstMap<K, V>(v);
129 129
  }
130 130

	
131 131

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

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

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

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

	
154 154
  ///Returns a \c ConstMap class with inlined value
155 155

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

	
163 163
  ///Map based on \c std::map
164 164

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

	
174 174
    typedef MapBase<K, T> Parent;
175 175
    ///Key type
176 176
    typedef typename Parent::Key Key;
177 177
    ///Value type
178 178
    typedef typename Parent::Value Value;
179 179
    ///Reference Type
180 180
    typedef T& Reference;
181 181
    ///Const reference type
182 182
    typedef const T& ConstReference;
183 183

	
184 184
    typedef True ReferenceMapTag;
185 185

	
186 186
  private:
187 187
    
188 188
    typedef std::map<K, T, Compare> Map;
189 189
    Value _value;
190 190
    Map _map;
191 191

	
192 192
  public:
193 193

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

	
207 207
  private:
208 208

	
209 209
    StdMap& operator=(const StdMap&);
210 210

	
211 211
  public:
212 212

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

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

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

	
240 240
    /// \e
241 241
    void setAll(const T &t) {
242 242
      _value = t;
243 243
      _map.clear();
244 244
    }    
245 245

	
246 246
  };
247 247
  
248 248
  ///Returns a \c StdMap class
249 249

	
250 250
  ///This function just returns a \c StdMap class with specified 
251 251
  ///default value.
252 252
  ///\relates StdMap
253
  template<typename K, typename V, typename Compare = std::less<K> > 
253
  template<typename K, typename V, typename Compare> 
254 254
  inline StdMap<K, V, Compare> stdMap(const V& value = V()) {
255 255
    return StdMap<K, V, Compare>(value);
256 256
  }
257
  
258
  ///Returns a \c StdMap class
259

	
260
  ///This function just returns a \c StdMap class with specified 
261
  ///default value.
262
  ///\relates StdMap
263
  template<typename K, typename V> 
264
  inline StdMap<K, V, std::less<K> > stdMap(const V& value = V()) {
265
    return StdMap<K, V, std::less<K> >(value);
266
  }
267
  
268
  ///Returns a \c StdMap class created from an appropriate std::map
269

	
270
  ///This function just returns a \c StdMap class created from an 
271
  ///appropriate std::map.
272
  ///\relates StdMap
273
  template<typename K, typename V, typename Compare> 
274
  inline StdMap<K, V, Compare> stdMap( const std::map<K, V, Compare> &map, 
275
                                       const V& value = V() ) {
276
    return StdMap<K, V, Compare>(map, value);
277
  }
257 278

	
258 279
  ///Returns a \c StdMap class created from an appropriate std::map
259 280

	
260 281
  ///This function just returns a \c StdMap class created from an 
261 282
  ///appropriate std::map.
262 283
  ///\relates StdMap
263
  template<typename K, typename V, typename Compare = std::less<K> > 
264
  inline StdMap<K, V, Compare> stdMap( const std::map<K, V, Compare> &map, 
265
                                       const V& value = V() ) {
266
    return StdMap<K, V, Compare>(map, value);
284
  template<typename K, typename V> 
285
  inline StdMap<K, V, std::less<K> > stdMap( const std::map<K, V, std::less<K> > &map, 
286
                                             const V& value = V() ) {
287
    return StdMap<K, V, std::less<K> >(map, value);
267 288
  }
268 289

	
269 290
  /// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt>
270 291
  ///
271 292
  /// This map has the <tt>[0..size-1]</tt> keyset and the values
272 293
  /// are stored in a \c std::vector<T>  container. It can be used with
273 294
  /// some data structures, for example \c UnionFind, \c BinHeap, when 
274 295
  /// the used items are small integer numbers.
275 296
  /// This map meets the \ref concepts::ReferenceMap "ReferenceMap" concept.
276 297
  ///
277 298
  /// \todo Revise its name
278 299
  template <typename T>
279 300
  class IntegerMap : public MapBase<int, T> {
280 301

	
281 302
    template <typename T1>
282 303
    friend class IntegerMap;
283 304

	
284 305
  public:
285 306

	
286 307
    typedef MapBase<int, T> Parent;
287 308
    ///\e
288 309
    typedef typename Parent::Key Key;
289 310
    ///\e
290 311
    typedef typename Parent::Value Value;
291 312
    ///\e
292 313
    typedef T& Reference;
293 314
    ///\e
294 315
    typedef const T& ConstReference;
295 316

	
296 317
    typedef True ReferenceMapTag;
297 318

	
298 319
  private:
299 320
    
300 321
    typedef std::vector<T> Vector;
301 322
    Vector _vector;
302 323

	
303 324
  public:
304 325

	
305 326
    /// Constructor with specified default value
306 327
    IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
307 328

	
308 329
    /// \brief Constructs the map from an appropriate \c std::vector.
309 330
    template <typename T1>
310 331
    IntegerMap(const std::vector<T1>& vector) 
311 332
      : _vector(vector.begin(), vector.end()) {}
312 333
    
313 334
    /// \brief Constructs a map from an other \ref IntegerMap.
314 335
    template <typename T1>
315 336
    IntegerMap(const IntegerMap<T1> &c) 
316 337
      : _vector(c._vector.begin(), c._vector.end()) {}
317 338

	
318 339
    /// \brief Resize the container
319 340
    void resize(int size, const T& value = T()) {
320 341
      _vector.resize(size, value);
321 342
    }
322 343

	
323 344
  private:
324 345

	
325 346
    IntegerMap& operator=(const IntegerMap&);
326 347

	
327 348
  public:
328 349

	
329 350
    ///\e
330 351
    Reference operator[](Key k) {
331 352
      return _vector[k];
332 353
    }
333 354

	
334 355
    /// \e 
335 356
    ConstReference operator[](Key k) const {
336 357
      return _vector[k];
337 358
    }
338 359

	
339 360
    /// \e 
340 361
    void set(const Key &k, const T& t) {
341 362
      _vector[k] = t;
342 363
    }
343 364

	
344 365
  };
345 366
  
346 367
  ///Returns an \c IntegerMap class
347 368

	
348 369
  ///This function just returns an \c IntegerMap class.
349 370
  ///\relates IntegerMap
350 371
  template<typename T>
351 372
  inline IntegerMap<T> integerMap(int size = 0, const T& value = T()) {
352 373
    return IntegerMap<T>(size, value);
353 374
  }
354 375

	
355 376
  /// @}
356 377

	
357 378
  /// \addtogroup map_adaptors
358 379
  /// @{
359 380

	
360 381
  /// \brief Identity map.
361 382
  ///
362 383
  /// This map gives back the given key as value without any
363 384
  /// modification. 
364 385
  template <typename T>
365 386
  class IdentityMap : public MapBase<T, T> {
366 387
  public:
367 388
    typedef MapBase<T, T> Parent;
368 389
    typedef typename Parent::Key Key;
369 390
    typedef typename Parent::Value Value;
370 391

	
371 392
    /// \e
372 393
    const T& operator[](const T& t) const {
373 394
      return t;
374 395
    }
375 396
  };
376 397

	
377 398
  ///Returns an \c IdentityMap class
378 399

	
379 400
  ///This function just returns an \c IdentityMap class.
380 401
  ///\relates IdentityMap
381 402
  template<typename T>
382 403
  inline IdentityMap<T> identityMap() {
383 404
    return IdentityMap<T>();
384 405
  }
385 406
  
386 407

	
387 408
  ///\brief Convert the \c Value of a map to another type using
388 409
  ///the default conversion.
389 410
  ///
390 411
  ///This \ref concepts::ReadMap "read only map"
391 412
  ///converts the \c Value of a map to type \c T.
392 413
  ///Its \c Key is inherited from \c M.
393 414
  template <typename M, typename T> 
394 415
  class ConvertMap : public MapBase<typename M::Key, T> {
0 comments (0 inline)