-
Notifications
You must be signed in to change notification settings - Fork 1
/
VarList.hpp
106 lines (86 loc) · 2.82 KB
/
VarList.hpp
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
#include <string>
#include <unordered_map>
template <typename T, typename V>
class VarList {
public:
// TODO: add exception handling
// TODO: add check (with traits) on ContainerType, with original var list requirements
// TODO: the varlist example int icon-kernels uses an array of var list, define a container for VarList
VarList(std::string i_list_name)
:m_list_id(getGlobalId())
,m_list_name(i_list_name)
{
}
// TODO: define copy/move ctors behaviour
explicit VarList(const VarList& i_var_list)
:m_list(i_var_list.m_list)
,m_list_id(getGlobalId())
,m_list_name(i_var_list.m_list_name)
{
}
VarList& operator=(const VarList& i_var_list)
{
m_list = i_var_list.m_list;
m_list_name = i_var_list.m_list_name;
return *this;
}
void append(const T& i_new_variable_name, const V& i_new_variable_value)
{
m_list.insert({i_new_variable_name, i_new_variable_value});
}
void finalize(void)
{
m_list.clear();
}
// TODO: is key-based search the only one we need?
// TODO: do not forget that unordered map ordering can change, do we really need a ptr???
const V* search(const T& i_variable_name) const
{
typename std::unordered_map<T,V>::const_iterator item = m_list.find(i_variable_name);
if(item != m_list.end())
return &(item->second);
else
return NULL;
}
unsigned int getId(void) const
{
return m_list_id;
}
std::string getName(void) const
{
return m_list_name;
}
unsigned int getListLength(void) const
{
return m_list.size();
}
// TODO: do not forget that unordered map ordering can change!
std::pair<T,const V*> getFirstVariable(void) const
{
// begin is available in all stl containers
std::pair<T,const V*> first_variable(m_list.begin()->first, &(m_list.begin()->second));
return first_variable;
}
// TODO: do not forget that unordered map ordering can change!
// TODO: why do we need this method?
std::pair<T,const V*> getNextVariable(const T& i_current_element_key) const
{
typename std::unordered_map<T,V>::const_iterator item = m_list.find(i_current_element_key);
item++;
if(item != m_list.end())
return std::pair<T,const V*>(item->first, &(item->second));
else
return std::pair<T,const V*>(item->first, NULL);
}
private:
std::unordered_map<T,V> m_list;
const unsigned int m_list_id;
std::string m_list_name;
// TODO: this ID is template specialization specific, do we need a global one?
// TODO: not thread safe
static unsigned int getGlobalId(void)
{
static unsigned int global_list_id = 0;
return global_list_id++;
}
};