-
Notifications
You must be signed in to change notification settings - Fork 6
/
maputils.h
131 lines (105 loc) · 2.75 KB
/
maputils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Utilities for using maps
*/
#ifndef H_MAPUTILS
#define H_MAPUTILS
#include <map>
#include <algorithm>
#include "data.h"
// Compare map keys
template<class T>
class mapKeyCompare
{
public:
bool operator()(const T& a, const T& b)
{
return (a.first < b.first);
}
};
// Compare map values
template<class T>
class mapValueCompare
{
public:
bool operator()(const T& a, const T& b)
{
return (a.second < b.second);
}
};
// Return the value of the minimum key in a map
// Coord c = mapMinValue<Coord, std::pair<double, Coord>>(m.begin(), m.end())
template<class Result, class Type, class Iter>
inline Result mapMinValue(Iter start, Iter end)
{
Iter min = std::min_element(start, end, mapKeyCompare<Type>());
if (min != end)
return min->second;
else
return Result();
}
// Return the key of the max value in a map
// Direction d = mapMaxValueKey<Direction, std::pair<Direction, int>>(m.begin(), m.end())
template<class Result, class Type, class Iter>
inline Result mapMaxValueKey(Iter start, Iter end)
{
Iter max = std::max_element(start, end, mapValueCompare<Type>());
if (max != end)
return max->first;
else
return Result();
}
// Count the number of times a value is in a map
template<class Type, class Value> int mapCountValue(Type m, Value val)
{
typedef typename Type::iterator iter;
int number = 0;
for (iter i = m.begin(); i != m.end(); ++i)
if (i->second == val)
++number;
return number;
}
// A map value const_iterator, so we can easily do the following without messing
// with map->first or map->second.
// for (const CoordPair& p : blobs) ...
//
// Based on: http://stackoverflow.com/a/7667618
template<class Type> class MapValueIterator
{
typedef typename Type::mapped_type Value;
typename Type::const_iterator iter;
public:
//typedef Value value_type;
//typedef Value* pointer;
//typedef Value& reference;
//typedef MapValueIterator<Type> difference_type;
//typedef std::forward_iterator_tag iterator_category;
MapValueIterator(typename Type::const_iterator iter) :iter(iter) { }
MapValueIterator(const MapValueIterator<Type>& i) :iter(i.iter) { }
const Value& operator*() const
{
return iter->second;
}
const Value* operator->() const
{
return &iter->second;
}
MapValueIterator& operator++()
{
++iter;
return *this;
}
MapValueIterator& operator=(const MapValueIterator& i)
{
iter = i.iter;
return *this;
}
bool operator==(const MapValueIterator& i) const
{
return iter == i.iter;
}
bool operator!=(const MapValueIterator& i) const
{
return iter != i.iter;
}
};
#endif