CYCLUS
Loading...
Searching...
No Matches
res_map.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_TOOLKIT_RES_MAP_H_
2#define CYCLUS_SRC_TOOLKIT_RES_MAP_H_
3
4#include <iomanip>
5#include <limits>
6#include <map>
7#include <set>
8#include <vector>
9
10#include "cyc_arithmetic.h"
11#include "cyc_limits.h"
12#include "error.h"
13#include "product.h"
14#include "material.h"
15#include "resource.h"
16#include "res_manip.h"
17
18namespace cyclus {
19namespace toolkit {
20
21/// ResMap container for the management of resources. It allows you to associate
22/// keys with individual resources. The keys are often strings or ints and the
23/// ResMap enables you to add whatever semantic meaning that you want to these keys.
24///
25/// Typically, a ResMap will be a member variable of an archetype class.
26/// Resources can be added, removed, and retrieved from it as needed.
27///
28/// @code
29/// class HasResMapAgent : public cyclus::Facility {
30/// public:
31/// Tick() {
32/// cyclus::Material::Ptr best = inventory_["best"];
33/// double invsize = inventory_.quantity();
34/// }
35///
36/// private:
37/// cyclus::toolkit::ResMap<std::string, cyclus::Material> inventory_;
38/// };
39/// @endcode
40template <class K, class R>
41class ResMap {
42 public:
43 ResMap() : dirty_quantity_(true), quantity_(0) {
44 Warn<EXPERIMENTAL_WARNING>("ResMap is experimental and its API may be "
45 "subject to change");
46 }
47
48 virtual ~ResMap() {}
49
50 typedef typename std::map<K, typename R::Ptr> map_type;
51 typedef typename std::map<K, typename R::Ptr>::iterator iterator;
52 typedef typename std::map<K, typename R::Ptr>::const_iterator const_iterator;
53
54 typedef typename std::map<K, int> obj_type;
55 typedef typename std::map<K, int>::iterator obj_iterator;
56 typedef typename std::map<K, int>::const_iterator const_obj_iterator;
57
58 //
59 // properties
60 //
61
62 /// Returns the total number of resources in the map.
63 inline int size() const { return resources_.size(); }
64
65 /// Returns the total quantity of resources in the map.
66 inline double quantity() {
67 if (dirty_quantity_)
68 UpdateQuantity();
69 return quantity_;
70 };
71
73 obj_ids_.clear();
74 iterator it = resources_.begin();
75 for (; it != resources_.end(); ++it) {
76 obj_ids_[it->first] = it->second->obj_id();
77 }
78 return obj_ids_;
79 }
80
82 obj_ids_ = oi;
83 dirty_quantity_ = true;
84 }
85
86 /// Returns true if there are no resources in the map.
87 inline bool empty() const { return resources_.empty(); }
88
89 //
90 // std::map interface
91 //
92
93 /// Returns a reference to a resource pointer given a key.
94 typename R::Ptr& operator[](const K& k) {
95 dirty_quantity_ = true;
96 return resources_[k];
97 };
98
99 /// Returns a reference to a resource pointer given a key.
100 const typename R::Ptr& operator[](const K& k) const {
101 dirty_quantity_ = true;
102 return const_cast<map_type&>(resources_)[k];
103 };
104
105 /// Returns an iterator to the begining of the map.
107 dirty_quantity_ = true;
108 return resources_.begin();
109 }
110
111 /// Returns a const iterator to the begining of the map.
112 const_iterator begin() const { return resources_.begin(); }
113
114 /// Returns a const iterator to the begining of the map.
115 const_iterator cbegin() const { return resources_.begin(); }
116
117 /// Returns an iterator to the end of the map.
119 dirty_quantity_ = true;
120 return resources_.end();
121 }
122
123 /// Returns a const iterator to the end of the map.
124 const_iterator end() const { return resources_.end(); }
125
126 /// Returns a const iterator to the end of the map.
127 const_iterator cend() const { return resources_.end(); }
128
129 /// Removes an element at a given position in the map.
131 resources_.erase(position);
132 UpdateQuantity();
133 };
134
135 /// Removes an element from the map, given its key.
136 typename map_type::size_type erase(const K& k) {
137 typename map_type::size_type s = resources_.erase(k);
138 UpdateQuantity();
139 return s;
140 };
141
142 /// Removes elements along a range from the first to last position in the map.
144 resources_.erase(first, last);
145 UpdateQuantity();
146 };
147
148 /// Removes all elements from the map.
149 void clear() {
150 resources_.clear();
151 obj_ids_.clear();
152 dirty_quantity_ = true;
153 };
154
155 //
156 // Non-std::map interface
157 //
158
159 /// Returns a vector of the values in the map
160 std::vector<typename R::Ptr> Values() {
161 int i = 0;
162 int n = resources_.size();
163 std::vector<typename R::Ptr> vals (n);
164 iterator it = resources_.begin();
165 while (it != resources_.end()) {
166 vals[i] = it->second;
167 ++i;
168 ++it;
169 }
170 return vals;
171 }
172
173 /// Returns a vector resource pointers for the values in the map
174 std::vector<Resource::Ptr> ResValues() { return ResCast(Values()); }
175
176 /// Sets the values of map based on their object ids. Thus the objs_ids
177 /// member must be set. This is primarily for restart capabilities and is
178 /// not recomended for day-to-day use.
179 void Values(std::vector<typename R::Ptr> vals) {
180 std::map<int, K> lookup;
181 obj_iterator oit = obj_ids_.begin();
182 for (; oit != obj_ids_.end(); ++oit) {
183 lookup[oit->second] = oit->first;
184 }
185 int i = 0;
186 int n = vals.size();
187 for (; i < n; ++i) {
188 resources_[lookup[vals[i]->obj_id()]] = vals[i];
189 }
190 dirty_quantity_ = true;
191 }
192
193 /// Sets the resource values of map based on their object ids. Thus the objs_ids
194 /// member must be set. This is primarily for restart capabilities and is
195 /// not recomended for day-to-day use.
196 void ResValues(std::vector<Resource::Ptr> vals) {
198 }
199
200 /// Retrieves and removes the value associated with the provided key.
201 typename R::Ptr Pop(const K key) {
202 iterator it = resources_.find(key);
203 if (it == resources_.end()) {
204 std::stringstream ss;
205 ss << "key " << key << " could not be found";
206 throw KeyError(ss.str());
207 }
208 typename R::Ptr val = it->second;
209 resources_.erase(it);
210 dirty_quantity_ = true;
211 return val;
212 }
213
214 private:
215 /// Recomputes the internal quantity variable.
216 void UpdateQuantity() {
217 using std::vector;
218 iterator it = resources_.begin();
219 int i = 0;
220 int n = resources_.size();
221 if (n == 0) {
222 quantity_ = 0.0;
223 } else {
224 vector<double> qtys (n, 0.0);
225 while (it != resources_.end()) {
226 qtys[i] = (*(it->second)).quantity();
227 ++i;
228 ++it;
229 }
230 quantity_ = CycArithmetic::KahanSum(qtys);
231 }
232 dirty_quantity_ = false;
233 }
234
235 /// Whether quantity_ should be recomputed or not.
236 mutable bool dirty_quantity_;
237
238 /// Current total quantity of all resources in the mapping.
239 double quantity_;
240
241 /// Underlying container
242 map_type resources_;
243
244 /// Object ID mapping, primarily used for restart,
245 obj_type obj_ids_;
246};
247
248} // namespace toolkit
249} // namespace cyclus
250
251#endif // CYCLUS_SRC_TOOLKIT_RES_MAP_H_
static double KahanSum(std::vector< double > input)
sums the materials in the vector in an intelligent way, to avoid floating point issues.
For failed retrieval/insertion of key-based data into/from data structures.
Definition error.h:47
ResMap container for the management of resources.
Definition res_map.h:41
iterator end()
Returns an iterator to the end of the map.
Definition res_map.h:118
std::map< K, int >::const_iterator const_obj_iterator
Definition res_map.h:56
std::map< K, int > obj_type
Definition res_map.h:54
std::vector< Resource::Ptr > ResValues()
Returns a vector resource pointers for the values in the map.
Definition res_map.h:174
std::map< K, int >::iterator obj_iterator
Definition res_map.h:55
std::map< K, typenameR::Ptr >::iterator iterator
Definition res_map.h:51
void obj_ids(obj_type oi)
Definition res_map.h:81
std::map< K, typename R::Ptr > map_type
Definition res_map.h:50
const_iterator cbegin() const
Returns a const iterator to the begining of the map.
Definition res_map.h:115
const_iterator begin() const
Returns a const iterator to the begining of the map.
Definition res_map.h:112
void erase(iterator first, iterator last)
Removes elements along a range from the first to last position in the map.
Definition res_map.h:143
const_iterator cend() const
Returns a const iterator to the end of the map.
Definition res_map.h:127
const R::Ptr & operator[](const K &k) const
Returns a reference to a resource pointer given a key.
Definition res_map.h:100
void ResValues(std::vector< Resource::Ptr > vals)
Sets the resource values of map based on their object ids.
Definition res_map.h:196
const_iterator end() const
Returns a const iterator to the end of the map.
Definition res_map.h:124
void clear()
Removes all elements from the map.
Definition res_map.h:149
std::map< K, typenameR::Ptr >::const_iterator const_iterator
Definition res_map.h:52
void Values(std::vector< typename R::Ptr > vals)
Sets the values of map based on their object ids.
Definition res_map.h:179
int size() const
Returns the total number of resources in the map.
Definition res_map.h:63
R::Ptr & operator[](const K &k)
Returns a reference to a resource pointer given a key.
Definition res_map.h:94
void erase(iterator position)
Removes an element at a given position in the map.
Definition res_map.h:130
double quantity()
Returns the total quantity of resources in the map.
Definition res_map.h:66
std::vector< typename R::Ptr > Values()
Returns a vector of the values in the map.
Definition res_map.h:160
map_type::size_type erase(const K &k)
Removes an element from the map, given its key.
Definition res_map.h:136
iterator begin()
Returns an iterator to the begining of the map.
Definition res_map.h:106
obj_type & obj_ids()
Definition res_map.h:72
R::Ptr Pop(const K key)
Retrieves and removes the value associated with the provided key.
Definition res_map.h:201
bool empty() const
Returns true if there are no resources in the map.
Definition res_map.h:87
Declares the CycArithmetic class, which holds arithmetic algorithms.
std::vector< Resource::Ptr > ResCast(std::vector< Material::Ptr > rs)
Casts a vector of Materials into a vector of Resources.
Definition res_manip.cc:51
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters