-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstl_util.h
87 lines (67 loc) · 1.86 KB
/
stl_util.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
/*
* UniversalContainer library.
* Copyright Jason Denton, 2008,2010.
* Made available under the new BSD license, as described in LICENSE
*
* Send comments and bug reports to [email protected]
* http://www.greatpanic.com/code.html
*/
#ifndef _STD_UTIL_H_
#define _STD_UTIL_H_
#include <map>
#include <vector>
#include <typeinfo>
template <typename A, typename B>
std::vector<A> keys_for_map(const std::map<A,B>& map)
{
std::vector<A> list;
typename std::map<A,B>::const_iterator iter = map.begin();
typename std::map<A,B>::const_iterator end = map.end();
for (;iter != end; iter++)
list.push_back(iter->first);
return list;
}
template <typename A, typename B>
std::vector<B> values_in_map(const std::map<A,B>& map)
{
std::vector<B> list;
typename std::map<A,B>::const_iterator iter = map.begin();
typename std::map<A,B>::const_iterator end = map.end();
for (;iter != end; iter++)
list.push_back(iter->second);
return list;
}
template <typename A>
std::vector<A> vector_sublist(const std::vector<A>& list, int start, int len)
{
std::vector<A> slist;
if (len == -1)
len = list.size() - start;
for (int i = 0; i < len; i++)
slist.push_back(list[start+i]);
return slist;
}
template<typename A, typename B>
bool compare_map(std::map<A,B>& map1, std::map<A,B>& map2)
{
if (map1.size() != map2.size()) return false;
typename std::map<A,B>::const_iterator iter = map1.begin();
typename std::map<A,B>::const_iterator end = map1.end();
bool same = true;
while (same && iter != end) {
same = (map1[iter->first] == map2[iter->first]);
iter++;
}
return same;
}
template<typename A>
bool compare_vector(std::vector<A>& list1, std::vector<A>& list2)
{
size_t sz = list1.size();
if (sz != list2.size()) return false;
bool same = true;
for (size_t i = 0; same && i < sz; i++)
same = (list1[i] == list2[i]);
return same;
}
#endif