-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAction.h
More file actions
209 lines (174 loc) · 4.39 KB
/
Action.h
File metadata and controls
209 lines (174 loc) · 4.39 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright 2014 Saminda Abeyruwan (saminda@cs.miami.edu)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Action.h
*
* Created on: Aug 19, 2012
* Author: sam
*/
#ifndef ACTION_H_
#define ACTION_H_
#include <vector>
#include "Assert.h"
namespace RLLib
{
template<class T>
class Action
{
private:
int actionID;
std::vector<T> values;
public:
explicit Action(const int& actionID) :
actionID(actionID)
{
}
~Action()
{
}
void push_back(const T& value)
{
values.push_back(value);
}
T at(const int& i = 0 /*default to a single action*/)
{
ASSERT(i < static_cast<int>(values.size()));
return values[i];
}
const T at(const int& i = 0 /*default to a single action*/) const
{
ASSERT(i < static_cast<int>(values.size()));
return values[i];
}
int dimension() const
{
return values.size();
}
void update(const int& i, const T& value)
{
ASSERT(i < static_cast<int>(values.size()));
values[i] = value;
}
bool operator==(const Action<T>& that) const
{
return actionID == that.actionID;
}
bool operator!=(const Action<T>& that) const
{
return actionID != that.actionID;
}
// Id within an associated id group
int id() const
{
return actionID;
}
};
template<class T>
class Actions
{
protected:
std::vector<Action<T>*> actions;
public:
Actions()
{
}
virtual ~Actions()
{
}
virtual int dimension() const =0;
virtual const Action<T>* operator[](const int& index) const =0;
virtual const Action<T>* at(const int& index) const =0;
virtual void push_back(const int& index, const T& value) =0;
virtual void erase(const int& index) =0;
virtual void update(const int& actionIndex, const int& vectorIndex, const T& value) =0;
typedef typename std::vector<Action<T>*>::iterator iterator;
typedef typename std::vector<Action<T>*>::const_iterator const_iterator;
iterator begin()
{
return actions.begin();
}
const_iterator begin() const
{
return actions.begin();
}
iterator end()
{
return actions.end();
}
const_iterator end() const
{
return actions.end();
}
};
template<class T>
class ActionArray: public Actions<T>
{
private:
typedef Actions<T> Base;
public:
ActionArray(const int& nbActions) :
Actions<T>()
{
for (int i = 0; i < nbActions; i++)
Base::actions.push_back(new Action<T>(i));
}
virtual ~ActionArray()
{
for (typename std::vector<Action<T>*>::iterator iter = Base::actions.begin();
iter != Base::actions.end(); ++iter)
delete *iter;
Base::actions.clear();
}
const Action<T>* operator[](const int& index) const
{
ASSERT(index < static_cast<int>(Base::actions.size()));
return Base::actions[index];
}
const Action<T>* at(const int& index) const
{
ASSERT(index < static_cast<int>(Base::actions.size()));
return Base::actions[index];
}
void push_back(const int& index, const T& value)
{
ASSERT(index < static_cast<int>(Base::actions.size()));
Base::actions[index]->push_back(value);
}
void erase(const int& index)
{
typename std::vector<Action<T>*>::iterator iter = Base::actions.begin();
while (iter != Base::actions.end())
{
if ((*iter)->id() == index)
{
Base::actions.erase(iter);
break;
}
else
++iter;
}
}
void update(const int& actionIndex, const int& vectorIndex, const T& value)
{
ASSERT(actionIndex < static_cast<int>(Base::actions.size()));
Base::actions[actionIndex]->update(vectorIndex, value);
}
int dimension() const
{
return Base::actions.size();
}
};
} // namespace RLLib
#endif /* ACTION_H_ */