Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarWeightedPool.hpp
blob: fbd53af3b09597601f5ce1e2194621496c1c4def (plain)
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
#pragma once

#include "StarRandom.hpp"

namespace Star {

template <typename Item>
struct WeightedPool {
public:
  typedef pair<double, Item> ItemsType;
  typedef List<ItemsType> ItemsList;

  WeightedPool();

  template <typename Container>
  explicit WeightedPool(Container container);

  void add(double weight, Item item);
  void clear();

  ItemsList const& items() const;

  size_t size() const;
  pair<double, Item> const& at(size_t index) const;
  double weight(size_t index) const;
  Item const& item(size_t index) const;
  bool empty() const;

  // Return item using the given randomness source
  Item select(RandomSource& rand) const;
  // Return item using the global randomness source
  Item select() const;
  // Return item using fast static randomness from the given seed
  Item select(uint64_t seed) const;

  // Return a list of n items which are selected uniquely (by index), where
  // n is the lesser of the desiredCount and the size of the pool.
  // This INFLUENCES PROBABILITIES so it should not be used where a
  // correct statistical distribution is required.
  List<Item> selectUniques(size_t desiredCount) const;
  List<Item> selectUniques(size_t desiredCount, uint64_t seed) const;

  size_t selectIndex(RandomSource& rand) const;
  size_t selectIndex() const;
  size_t selectIndex(uint64_t seed) const;

private:
  size_t selectIndex(double target) const;

  ItemsList m_items;
  double m_totalWeight;
};

template <typename Item>
WeightedPool<Item>::WeightedPool()
  : m_totalWeight(0.0) {}

template <typename Item>
template <typename Container>
WeightedPool<Item>::WeightedPool(Container container)
  : WeightedPool() {
  for (auto const& pair : container)
    add(get<0>(pair), get<1>(pair));
}

template <typename Item>
void WeightedPool<Item>::add(double weight, Item item) {
  if (weight <= 0.0)
    return;

  m_items.append({weight, std::move(item)});
  m_totalWeight += weight;
}

template <typename Item>
void WeightedPool<Item>::clear() {
  m_items.clear();
  m_totalWeight = 0.0;
}

template <typename Item>
auto WeightedPool<Item>::items() const -> ItemsList const & {
  return m_items;
}

template <typename Item>
size_t WeightedPool<Item>::size() const {
  return m_items.count();
}

template <typename Item>
pair<double, Item> const& WeightedPool<Item>::at(size_t index) const {
  return m_items.at(index);
}

template <typename Item>
double WeightedPool<Item>::weight(size_t index) const {
  return at(index).first;
}

template <typename Item>
Item const& WeightedPool<Item>::item(size_t index) const {
  return at(index).second;
}

template <typename Item>
bool WeightedPool<Item>::empty() const {
  return m_items.empty();
}

template <typename Item>
Item WeightedPool<Item>::select(RandomSource& rand) const {
  if (m_items.empty())
    return Item();

  return m_items[selectIndex(rand)].second;
}

template <typename Item>
Item WeightedPool<Item>::select() const {
  if (m_items.empty())
    return Item();

  return m_items[selectIndex()].second;
}

template <typename Item>
Item WeightedPool<Item>::select(uint64_t seed) const {
  if (m_items.empty())
    return Item();

  return m_items[selectIndex(seed)].second;
}

template <typename Item>
List<Item> WeightedPool<Item>::selectUniques(size_t desiredCount) const {
  return selectUniques(desiredCount, Random::randu64());
}

template <typename Item>
List<Item> WeightedPool<Item>::selectUniques(size_t desiredCount, uint64_t seed) const {
  size_t targetCount = std::min(desiredCount, size());
  Set<size_t> indices;
  while (indices.size() < targetCount)
    indices.add(selectIndex(++seed));
  List<Item> result;
  for (size_t i : indices)
    result.append(m_items[i].second);
  return result;
}

template <typename Item>
size_t WeightedPool<Item>::selectIndex(RandomSource& rand) const {
  return selectIndex(rand.randd());
}

template <typename Item>
size_t WeightedPool<Item>::selectIndex() const {
  return selectIndex(Random::randd());
}

template <typename Item>
size_t WeightedPool<Item>::selectIndex(uint64_t seed) const {
  return selectIndex(staticRandomDouble(seed));
}

template <typename Item>
size_t WeightedPool<Item>::selectIndex(double target) const {
  if (m_items.empty())
    return NPos;

  // Test a randomly generated target against each weighted item in turn, and
  // see if that weighted item's weight value crosses the target.  This way, a
  // random item is picked from the list, but (roughly) weighted to be
  // proportional to its weight over the weight of all entries.
  //
  // TODO: This is currently O(n), but can easily be made O(log(n)) by using a
  // tree.  If this shows up in performance measurements, this is an obvious
  // improvement.

  double accumulatedWeight = 0.0f;
  for (size_t i = 0; i < m_items.size(); ++i) {
    accumulatedWeight += m_items[i].first / m_totalWeight;
    if (target <= accumulatedWeight)
      return i;
  }

  // If we haven't crossed the target, just assume floating point error has
  // caused us to not quite make it to the last item.
  return m_items.size() - 1;
}

}