DDD 1.9.0.20250409152518
AdditiveMap.hpp
Go to the documentation of this file.
1#ifndef __ADDITIVEMAP_HH__
2#define __ADDITIVEMAP_HH__
3
4#include <vector>
6
7template<typename K, typename V, typename EqualKey = d3::util::equal<K> >
9
10 typedef std::vector<std::pair<K,V> > mapType;
11
13
14public:
15 typedef typename mapType::value_type value_type;
16 typedef typename mapType::const_iterator const_iterator;
17 typedef typename mapType::iterator iterator;
19
20 // delegate iterator operations to map
21 const_iterator end() const { return map.end(); }
22 const_iterator begin() const { return map.begin();}
23
24 iterator find (const K & key) {
25 iterator res = map.begin();
26 while (res != map.end()) {
27 if (EqualKey () (res->first,key))
28 return res;
29 ++res;
30 }
31 return res;
32 }
33
34 int addAll (const AdditiveMap<K,V> & other) {
35 return addAll(other.begin(),other.end());
36 }
37
38 // adds a set of mappings
39 // returns the number of sums computed
41 int count = 0;
42 for ( ; begin != end ; ++ begin ) {
43 const value_type & val = *begin;
44 if (add (val.first,val.second) )
45 ++count;
46 }
47 return count;
48 }
49
50 // adds value to the value mapped to key
51 // returns true if sum was actually used, false if normal insertion performed
52 bool add (const K & key, const V & value) {
53 typename mapType::iterator it = find(key);
54 if ( it != map.end() ) {
55 // found it
56 it->second = it->second + value ;
57 return true;
58 } else {
59 map.push_back(std::make_pair(key,value));
60 return false;
61 }
62 }
63 // removes value to the value mapped to key
64 // returns true if difference - was actually used, false if nothing performed
65 bool remove (const K & key, const V & value) {
66 typename mapType::iterator it = find(key);
67 if ( it != map.end() ) {
68 // found it
69 it->second = it->second - value ;
70 return true;
71 } else {
72 return false;
73 }
74 }
75};
76
77#endif
Definition AdditiveMap.hpp:8
mapType map
Definition AdditiveMap.hpp:12
int addAll(const AdditiveMap< K, V > &other)
Definition AdditiveMap.hpp:34
mapType::const_iterator const_iterator
Definition AdditiveMap.hpp:16
AdditiveMap()
Definition AdditiveMap.hpp:18
bool remove(const K &key, const V &value)
Definition AdditiveMap.hpp:65
const_iterator begin() const
Definition AdditiveMap.hpp:22
std::vector< std::pair< K, V > > mapType
Definition AdditiveMap.hpp:10
bool add(const K &key, const V &value)
Definition AdditiveMap.hpp:52
iterator find(const K &key)
Definition AdditiveMap.hpp:24
const_iterator end() const
Definition AdditiveMap.hpp:21
mapType::iterator iterator
Definition AdditiveMap.hpp:17
int addAll(const_iterator begin, const_iterator end)
Definition AdditiveMap.hpp:40
mapType::value_type value_type
Definition AdditiveMap.hpp:15

Please comment this page and report errors about it on the RefDocComments page.
Generated on Wed Apr 9 2025 15:27:42 for DDD by doxygen 1.9.8