|
36 | 36 | #include "string_formatter.h"
|
37 | 37 | #include "string_id.h"
|
38 | 38 | #include "units.h"
|
| 39 | +#include "weighted_list.h" |
39 | 40 |
|
40 | 41 | class quantity;
|
41 | 42 |
|
@@ -1253,6 +1254,78 @@ struct handler<std::vector<T>> {
|
1253 | 1254 | static constexpr bool is_container = true;
|
1254 | 1255 | };
|
1255 | 1256 |
|
| 1257 | +template<typename T> |
| 1258 | +struct handler<weighted_int_list<T>> { |
| 1259 | + void clear( weighted_int_list<T> &container ) const { |
| 1260 | + container.clear(); |
| 1261 | + } |
| 1262 | + bool insert( weighted_int_list<T> &container, const std::pair<T, int> &data ) const { |
| 1263 | + container.add( data ); |
| 1264 | + return true; |
| 1265 | + } |
| 1266 | + bool relative( weighted_int_list<T> &, const std::pair<T, int> & ) const { |
| 1267 | + return false; |
| 1268 | + } |
| 1269 | + template<typename E> |
| 1270 | + bool erase( weighted_int_list<T> &container, const E &data ) const { |
| 1271 | + const auto pred = [&data]( const std::pair<T, int> &e ) { |
| 1272 | + return e.first == data.first; |
| 1273 | + }; |
| 1274 | + if( !erase_if( container, pred ) ) { |
| 1275 | + debugmsg( "Did not remove %s in delete", data_string( data ) ); |
| 1276 | + return false; |
| 1277 | + } |
| 1278 | + return true; |
| 1279 | + } |
| 1280 | + template<typename P> |
| 1281 | + bool erase_if( weighted_int_list<T> &container, P &predicate ) const { |
| 1282 | + const auto iter = std::find_if( container.begin(), container.end(), predicate ); |
| 1283 | + if( iter != container.end() ) { |
| 1284 | + container.remove( iter->first ); |
| 1285 | + } else { |
| 1286 | + return false; |
| 1287 | + } |
| 1288 | + return true; |
| 1289 | + } |
| 1290 | + static constexpr bool is_container = true; |
| 1291 | +}; |
| 1292 | + |
| 1293 | +template<typename T, typename W> |
| 1294 | +struct handler<weighted_list<T, W>> { |
| 1295 | + void clear( weighted_list<T, W> &container ) const { |
| 1296 | + container.clear(); |
| 1297 | + } |
| 1298 | + bool insert( weighted_list<T, W> &container, const std::pair<T, W> &data ) const { |
| 1299 | + container.add( data ); |
| 1300 | + return true; |
| 1301 | + } |
| 1302 | + bool relative( weighted_list<T, W> &, const std::pair<T, W> & ) const { |
| 1303 | + return false; |
| 1304 | + } |
| 1305 | + template<typename E> |
| 1306 | + bool erase( weighted_list<T, W> &container, const E &data ) const { |
| 1307 | + const auto pred = [&data]( const T & e ) { |
| 1308 | + return e == data; |
| 1309 | + }; |
| 1310 | + if( !erase_if( container, pred ) ) { |
| 1311 | + debugmsg( "Did not remove %s in delete", data_string( data ) ); |
| 1312 | + return false; |
| 1313 | + } |
| 1314 | + return true; |
| 1315 | + } |
| 1316 | + template<typename P> |
| 1317 | + bool erase_if( weighted_list<T, W> &container, P &predicate ) const { |
| 1318 | + const auto iter = std::find_if( container.begin(), container.end(), predicate ); |
| 1319 | + if( iter != container.end() ) { |
| 1320 | + container.remove( *iter.obj ); |
| 1321 | + } else { |
| 1322 | + return false; |
| 1323 | + } |
| 1324 | + return true; |
| 1325 | + } |
| 1326 | + static constexpr bool is_container = true; |
| 1327 | +}; |
| 1328 | + |
1256 | 1329 | template<typename Key, typename Val>
|
1257 | 1330 | struct handler<std::map<Key, Val>> {
|
1258 | 1331 | void clear( std::map<Key, Val> &container ) const {
|
|
0 commit comments