-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForceContainer.h
More file actions
89 lines (64 loc) · 2.13 KB
/
Copy pathForceContainer.h
File metadata and controls
89 lines (64 loc) · 2.13 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
/*=========================================================================
Name: ForceContainer.h
Author: David Borland, The Renaissance Computing Institute (RENCI)
Copyright: The Renaissance Computing Institute (RENCI)
Description: Class to contain and keep track of force effects and force
effect ids.
=========================================================================*/
#ifndef FORCECONTAINER_H
#define FORCECONTAINER_H
#include <unordered_map>
#include <forward_list>
template <class T>
class ForceContainer {
public:
ForceContainer() {}
// Add a force effect. Return id for this effect
int Add(T forceEffect) {
int id;
if (availableIds.empty()) {
// Ids are all used, so add a new one
id = forceEffects.size();
}
else {
// At least one id has been used and returned, so take one from the list
id = availableIds.front();
availableIds.pop_front();
}
// Set the force effect
forceEffects[id] = forceEffect;
// Return the id
return id;
}
// Return a pointer to the force effect with the given id
T* Get(int id) {
return &forceEffects[id];
}
// Iterator at the front of the force effects
typename std::unordered_map<int, T>::iterator Begin() {
return forceEffects.begin();
}
// Iterator at the end of the force effects
typename std::unordered_map<int, T>::iterator End() {
return forceEffects.end();
}
// Remove the force effect with the given id
void Remove(int id) {
int num = forceEffects.erase(id);
if (num > 0) {
// Valid id, so add it to the available id list
availableIds.push_front(id);
}
}
// Remove all force effects
void RemoveAll() {
forceEffects.clear();
availableIds.clear();
}
protected:
// Map with an integer key id for force effects
std::unordered_map<int, T> forceEffects;
// List to keep track of ids that have been used and returned
std::forward_list<int> availableIds;
};
#endif