LeviLamina
Loading...
Searching...
No Matches
WeightedRandomList.h
1#pragma once
2
3#include "mc/_HeaderOutputPredefine.h"
4
5template <typename T0>
7public:
8 uint mTotalWeight;
9 std::vector<std::pair<T0, uint>> mData;
10
11 void push(T0 const& item, uint weight) {
12 mData.emplace_back(item, weight);
13 mTotalWeight += weight;
14 }
15 void push(T0&& item, uint weight) {
16 mData.emplace_back(std::move(item), weight);
17 mTotalWeight += weight;
18 }
19 void clear() {
20 mData.clear();
21 mTotalWeight = 0;
22 }
23 uint total() const { return mTotalWeight; }
24
25 size_t size() const { return mData.size(); }
26
27 bool empty() const { return mData.empty(); }
28
29 template <typename Fn>
30 void erase_if(Fn&& fn) {
31 auto it = std::remove_if(mData.begin(), mData.end(), std::forward<Fn>(fn));
32 for (auto i = it; i != mData.end(); ++i) {
33 mTotalWeight -= i->second;
34 }
35 mData.erase(it, mData.end());
36 }
37};
Definition WeightedRandomList.h:6