18#ifndef TRINITY_CONTAINERS_H
19#define TRINITY_CONTAINERS_H
57 throw std::out_of_range(
"index");
68 static_assert(std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<typename C::iterator>::iterator_category>::value,
"Invalid container passed to Trinity::Containers::RandomResize");
69 if (std::size(container) <= requestedSize)
71 auto keepIt = std::begin(container), curIt = std::begin(container);
72 uint32 elementsToKeep = requestedSize, elementsToProcess = std::size(container);
73 while (elementsToProcess)
76 if (
urand(1, elementsToProcess) <= elementsToKeep)
79 *keepIt = std::move(*curIt);
86 container.erase(keepIt, std::end(container));
89 template<
class C,
class Predicate>
90 void RandomResize(C& container, Predicate&& predicate, std::size_t requestedSize)
94 std::copy_if(std::begin(container), std::end(container), std::inserter(containerCopy, std::end(containerCopy)), predicate);
99 container = std::move(containerCopy);
110 auto it = std::begin(container);
111 std::advance(it,
urand(0,
uint32(std::size(container)) - 1));
127 auto it = std::begin(container);
128 std::advance(it,
urandweighted(weights.size(), weights.data()));
140 template<
class C,
class Fn>
143 std::vector<double> weights;
144 weights.reserve(std::size(container));
145 double weightSum = 0.0;
146 for (
auto& val : container)
148 double weight = weightExtractor(val);
149 weights.push_back(weight);
152 if (weightSum <= 0.0)
153 weights.assign(std::size(container), 1.0);
166 template<
class Iterator>
197 template<
class Iterator1,
class Iterator2>
198 inline bool Intersects(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
200 while (first1 != last1 && first2 != last2)
202 if (*first1 < *first2)
204 else if (*first2 < *first1)
226 template<
class Iterator1,
class Iterator2,
class Predicate>
227 inline bool Intersects(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, Predicate&& equalPred)
229 while (first1 != last1 && first2 != last2)
231 if (*first1 < *first2)
233 else if (*first2 < *first1)
235 else if (!equalPred(*first1, *first2))
246 template <
typename Container,
typename Predicate>
249 auto wpos = c.begin();
250 for (
auto rpos = c.begin(), end = c.end(); rpos != end; ++rpos)
255 std::swap(*rpos, *wpos);
259 c.erase(wpos, c.end());
262 template <
typename Container,
typename Predicate>
265 for (
auto it = c.begin(); it != c.end();)
275 template <
typename Container,
typename Predicate>
278 if constexpr (std::is_move_assignable_v<
decltype(*c.begin())>)
310 vec.resize(i + 1, resizeDefault);
uint32_t uint32
Definition: Define.h:143
uint32 urandweighted(size_t count, double const *chances)
Definition: Random.cpp:87
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:42
static RandomEngine & Instance()
Definition: Random.cpp:93
Definition: Containers.h:35
CheckedBufferOutputIterator operator++(int)
Definition: Containers.h:47
T & operator*() const
Definition: Containers.h:45
CheckedBufferOutputIterator & operator++()
Definition: Containers.h:46
void check() const
Definition: Containers.h:54
T & reference
Definition: Containers.h:40
std::ptrdiff_t difference_type
Definition: Containers.h:41
size_t remaining() const
Definition: Containers.h:49
std::output_iterator_tag iterator_category
Definition: Containers.h:37
T * _end
Definition: Containers.h:53
CheckedBufferOutputIterator(T *buf, size_t n)
Definition: Containers.h:43
void value_type
Definition: Containers.h:38
T * _buf
Definition: Containers.h:52
T * pointer
Definition: Containers.h:39
void EraseIfMoveAssignable(Container &c, Predicate p)
Definition: Containers.h:247
void EraseIfNotMoveAssignable(Container &c, Predicate p)
Definition: Containers.h:263
auto SelectRandomWeightedContainerElement(C const &container, std::vector< double > const &weights) -> decltype(std::begin(container))
Definition: Containers.h:125
decltype(auto) EnsureWritableVectorIndex(std::vector< T > &vec, typename std::vector< T >::size_type i)
Definition: Containers.h:292
void RandomShuffle(Iterator begin, Iterator end)
Reorder the elements of the iterator range randomly.
Definition: Containers.h:167
auto SelectRandomContainerElement(C const &container) -> typename std::add_const< decltype(*std::begin(container))>::type &
Definition: Containers.h:108
bool Intersects(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
Definition: Containers.h:198
void EraseIf(Container &c, Predicate p)
Definition: Containers.h:276
void RandomResize(C &container, std::size_t requestedSize)
Definition: Containers.h:66
Definition: AsioHacksFwd.h:53