-
Notifications
You must be signed in to change notification settings - Fork 0
/
multicont.h
102 lines (88 loc) · 1.97 KB
/
multicont.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
#ifndef imp_cont_GUARD
#define imp_cont_GUARD
#include "defines.h"
#include <vector>
#include <assert.h>
#include "errorhandling.h"
/* imp_cont is a class which mimics its template parameter, but provides an additional method which allows access to a vector of 'nondefault' instances of the template parameter.
*
* E.g. this allows as to add multiple classes of edges between graph vertices without having to change anything except one container type.
* */
template <class T>
class imp_cont : public T
{
private:
mutable vector<T> vec;
void fillin(int i) const
{
while(vec.size() < i)
vec.push_back(T());
};
public:
using T::T;
T& get_layer(int i = 0)
{
fillin(i);
return i == 0 ? *this : vec[i-1];
};
const T& get_layer(int i = 0) const
{
fillin(i);
return ((i == 0) ? (*((T*)this)) : (vec[i-1]));
};
int get_layercount()
{
return vec.size()+1;
};
static void self_test()
{
cout << "testing multi container" << endl;
imp_cont<vector<int>> a;
a.push_back(0);
a.push_back(1);
a.get_layer(1).push_back(2);
assert(a[0] == 0);
assert(a[1] == 1);
assert(a.get_layer(1)[0] == 2);
}
};
template <class T>
class imp_contA : public imp_cont<T>
{
public:
using imp_cont<T>::imp_cont;
T& operator[](int i)
{
return this->get_layer(i);
};
};
template <class T>
class imp_contB : public T
{
private:
mutable map<string, T> vec;
public:
using T::T;
T& operator[](const string& k)
{
if(k == "default")
return *this;
else
return vec[k];
};
T const & operator[](const string& k) const
{
if(k == "default")
return *this;
else
{
auto itr = vec.find(k);
if(itr == vec.end())
throw(string("key not found: ") + k);
return
*itr;
}
};
};
typedef imp_cont<vector<int>> imp_cont_default;
#endif