TrinityCore
Trinity::Containers Namespace Reference

Namespaces

namespace  Impl
 
namespace  Lists
 

Classes

class  FlatSet
 

Functions

template<class M >
auto MapGetValuePtr (M &map, typename M::key_type const &key)
 
template<class K , class V , template< class, class, class... > class M, class... Rest>
void MultimapErasePair (M< K, V, Rest... > &multimap, K const &key, V const &value)
 
template<class C >
void RandomResize (C &container, std::size_t requestedSize)
 
template<class C , class Predicate >
void RandomResize (C &container, Predicate &&predicate, std::size_t requestedSize)
 
template<class C >
auto SelectRandomContainerElement (C const &container) -> typename std::add_const< decltype(*std::begin(container))>::type &
 
template<class C >
auto SelectRandomWeightedContainerElement (C const &container, std::span< double > const &weights) -> decltype(std::begin(container))
 
template<class C , class Fn >
auto SelectRandomWeightedContainerElement (C const &container, Fn weightExtractor) -> decltype(std::begin(container))
 
template<class Iterator >
void RandomShuffle (Iterator begin, Iterator end)
 Reorder the elements of the iterator range randomly. More...
 
template<class C >
void RandomShuffle (C &container)
 Reorder the elements of the container randomly. More...
 
template<class Iterator1 , class Iterator2 >
bool Intersects (Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
 
template<class Iterator1 , class Iterator2 , class Predicate >
bool Intersects (Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, Predicate &&equalPred)
 
template<typename Container , typename Predicate >
void EraseIf (Container &c, Predicate p)
 
template<typename T >
decltype(auto) EnsureWritableVectorIndex (std::vector< T > &vec, typename std::vector< T >::size_type i)
 
template<typename T >
decltype(auto) EnsureWritableVectorIndex (std::vector< T > &vec, typename std::vector< T >::size_type i, T const &resizeDefault)
 
template<typename Container , typename NeedleContainer , typename ContainsOperator = bool(std::string const&, std::string const&), typename T = void>
auto FuzzyFindIn (Container const &container, NeedleContainer const &needles, ContainsOperator const &contains=StringContainsStringI, int(*bonus)(decltype((*std::begin(std::declval< Container >()))))=nullptr)
 
template<typename iterator , class end_iterator = iterator>
constexpr IteratorPair< iterator, end_iterator > MakeIteratorPair (iterator first, end_iterator second)
 
template<typename iterator , class end_iterator = iterator>
constexpr IteratorPair< iterator, end_iterator > MakeIteratorPair (std::pair< iterator, end_iterator > iterators)
 
template<class M >
auto MapEqualRange (M &map, typename M::key_type const &key)
 

Function Documentation

◆ EnsureWritableVectorIndex() [1/2]

template<typename T >
decltype(auto) Trinity::Containers::EnsureWritableVectorIndex ( std::vector< T > &  vec,
typename std::vector< T >::size_type  i 
)
inline

Returns a mutable reference to element at index i Will resize vector if neccessary to ensure element at i can be safely written

This exists as separate overload instead of one function with default argument to allow using with vectors of non-default-constructible classes

Definition at line 295 of file Containers.h.

296 {
297 if (i >= vec.size())
298 vec.resize(i + 1);
299
300 return vec[i];
301 }
+ Here is the caller graph for this function:

◆ EnsureWritableVectorIndex() [2/2]

template<typename T >
decltype(auto) Trinity::Containers::EnsureWritableVectorIndex ( std::vector< T > &  vec,
typename std::vector< T >::size_type  i,
T const &  resizeDefault 
)
inline

Returns a mutable reference to element at index i Will resize vector if neccessary to ensure element at i can be safely written

This overload allows specifying what value to pad vector with during .resize

Definition at line 310 of file Containers.h.

311 {
312 if (i >= vec.size())
313 vec.resize(i + 1, resizeDefault);
314
315 return vec[i];
316 }

◆ EraseIf()

template<typename Container , typename Predicate >
void Trinity::Containers::EraseIf ( Container &  c,
Predicate  p 
)

Definition at line 279 of file Containers.h.

280 {
281 if constexpr (std::is_move_assignable_v<decltype(*c.begin())>)
282 Impl::EraseIfMoveAssignable(c, std::ref(p));
283 else
284 Impl::EraseIfNotMoveAssignable(c, std::ref(p));
285 }
void EraseIfMoveAssignable(Container &c, Predicate p)
Definition: Containers.h:250
void EraseIfNotMoveAssignable(Container &c, Predicate p)
Definition: Containers.h:266
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ FuzzyFindIn()

template<typename Container , typename NeedleContainer , typename ContainsOperator = bool(std::string const&, std::string const&), typename T = void>
auto Trinity::Containers::FuzzyFindIn ( Container const &  container,
NeedleContainer const &  needles,
ContainsOperator const &  contains = StringContainsStringI,
int(*)(decltype((*std::begin(std::declval< Container >()))))  bonus = nullptr 
)

Definition at line 30 of file FuzzyFind.h.

31 {
32 using IteratorResult = decltype((*std::begin(container)));
33 using MappedType = std::conditional_t<std::is_reference_v<IteratorResult>, std::reference_wrapper<std::remove_reference_t<IteratorResult>>, IteratorResult>;
34 std::multimap<size_t, MappedType, std::greater<size_t>> results;
35
36 for (auto outerIt = std::begin(container), outerEnd = std::end(container); outerIt != outerEnd; ++outerIt)
37 {
38 size_t count = 0;
39 for (auto innerIt = std::begin(needles), innerEnd = std::end(needles); innerIt != innerEnd; ++innerIt)
40 if (contains(*outerIt, *innerIt))
41 ++count;
42
43 if (!count)
44 continue;
45
46 if (bonus)
47 count += bonus(*outerIt);
48
49 results.emplace(count, *outerIt);
50 }
51
52 return results;
53 }

◆ Intersects() [1/2]

template<class Iterator1 , class Iterator2 >
bool Trinity::Containers::Intersects ( Iterator1  first1,
Iterator1  last1,
Iterator2  first2,
Iterator2  last2 
)
inline

Definition at line 201 of file Containers.h.

202 {
203 while (first1 != last1 && first2 != last2)
204 {
205 if (*first1 < *first2)
206 ++first1;
207 else if (*first2 < *first1)
208 ++first2;
209 else
210 return true;
211 }
212
213 return false;
214 }
+ Here is the caller graph for this function:

◆ Intersects() [2/2]

template<class Iterator1 , class Iterator2 , class Predicate >
bool Trinity::Containers::Intersects ( Iterator1  first1,
Iterator1  last1,
Iterator2  first2,
Iterator2  last2,
Predicate &&  equalPred 
)
inline

Definition at line 230 of file Containers.h.

231 {
232 while (first1 != last1 && first2 != last2)
233 {
234 if (*first1 < *first2)
235 ++first1;
236 else if (*first2 < *first1)
237 ++first2;
238 else if (!equalPred(*first1, *first2))
239 ++first1, ++first2;
240 else
241 return true;
242 }
243
244 return false;
245 }

◆ MakeIteratorPair() [1/2]

template<typename iterator , class end_iterator = iterator>
constexpr IteratorPair< iterator, end_iterator > Trinity::Containers::MakeIteratorPair ( iterator  first,
end_iterator  second 
)
constexpr

Definition at line 48 of file IteratorPair.h.

49 {
50 return { first, second };
51 }
+ Here is the caller graph for this function:

◆ MakeIteratorPair() [2/2]

template<typename iterator , class end_iterator = iterator>
constexpr IteratorPair< iterator, end_iterator > Trinity::Containers::MakeIteratorPair ( std::pair< iterator, end_iterator >  iterators)
constexpr

Definition at line 54 of file IteratorPair.h.

55 {
56 return iterators;
57 }

◆ MapEqualRange()

template<class M >
auto Trinity::Containers::MapEqualRange ( M &  map,
typename M::key_type const &  key 
)

Definition at line 60 of file IteratorPair.h.

61 {
62 return MakeIteratorPair(map.equal_range(key));
63 }
constexpr IteratorPair< iterator, end_iterator > MakeIteratorPair(std::pair< iterator, end_iterator > iterators)
Definition: IteratorPair.h:54
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MapGetValuePtr()

template<class M >
auto Trinity::Containers::MapGetValuePtr ( M &  map,
typename M::key_type const &  key 
)

Returns a pointer to mapped value (or the value itself if map stores pointers)

Definition at line 29 of file MapUtils.h.

30{
31 auto itr = map.find(key);
32 if constexpr (std::is_pointer_v<typename M::mapped_type>)
33 return itr != map.end() ? itr->second : nullptr;
34 else
35 return itr != map.end() ? &itr->second : nullptr;
36}

◆ MultimapErasePair()

template<class K , class V , template< class, class, class... > class M, class... Rest>
void Trinity::Containers::MultimapErasePair ( M< K, V, Rest... > &  multimap,
K const &  key,
V const &  value 
)

Definition at line 39 of file MapUtils.h.

40{
41 auto range = multimap.equal_range(key);
42 for (auto itr = range.first; itr != range.second;)
43 {
44 if (itr->second == value)
45 itr = multimap.erase(itr);
46 else
47 ++itr;
48 }
49}
+ Here is the caller graph for this function:

◆ RandomResize() [1/2]

template<class C , class Predicate >
void Trinity::Containers::RandomResize ( C &  container,
Predicate &&  predicate,
std::size_t  requestedSize 
)

First use predicate filter

Definition at line 91 of file Containers.h.

92 {
94 C containerCopy;
95 std::copy_if(std::begin(container), std::end(container), std::inserter(containerCopy, std::end(containerCopy)), predicate);
96
97 if (requestedSize)
98 RandomResize(containerCopy, requestedSize);
99
100 container = std::move(containerCopy);
101 }
void RandomResize(C &container, Predicate &&predicate, std::size_t requestedSize)
Definition: Containers.h:91
+ Here is the call graph for this function:

◆ RandomResize() [2/2]

template<class C >
void Trinity::Containers::RandomResize ( C &  container,
std::size_t  requestedSize 
)

Definition at line 67 of file Containers.h.

68 {
69 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");
70 if (std::size(container) <= requestedSize)
71 return;
72 auto keepIt = std::begin(container), curIt = std::begin(container);
73 uint32 elementsToKeep = requestedSize, elementsToProcess = std::size(container);
74 while (elementsToProcess)
75 {
76 // this element has chance (elementsToKeep / elementsToProcess) of being kept
77 if (urand(1, elementsToProcess) <= elementsToKeep)
78 {
79 if (keepIt != curIt)
80 *keepIt = std::move(*curIt);
81 ++keepIt;
82 --elementsToKeep;
83 }
84 ++curIt;
85 --elementsToProcess;
86 }
87 container.erase(keepIt, std::end(container));
88 }
uint32_t uint32
Definition: Define.h:143
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:42
constexpr std::size_t size()
Definition: UpdateField.h:796
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RandomShuffle() [1/2]

template<class C >
void Trinity::Containers::RandomShuffle ( C &  container)
inline

Reorder the elements of the container randomly.

Parameters
containerContainer to reorder

Definition at line 183 of file Containers.h.

184 {
185 RandomShuffle(std::begin(container), std::end(container));
186 }
void RandomShuffle(C &container)
Reorder the elements of the container randomly.
Definition: Containers.h:183
+ Here is the call graph for this function:

◆ RandomShuffle() [2/2]

template<class Iterator >
void Trinity::Containers::RandomShuffle ( Iterator  begin,
Iterator  end 
)
inline

Reorder the elements of the iterator range randomly.

Parameters
beginBeginning of the range to reorder
endEnd of the range to reorder

Definition at line 170 of file Containers.h.

171 {
172 std::shuffle(begin, end, RandomEngine::Instance());
173 }
static RandomEngine & Instance()
Definition: Random.cpp:93
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SelectRandomContainerElement()

template<class C >
auto Trinity::Containers::SelectRandomContainerElement ( C const &  container) -> typename std::add_const<decltype(*std::begin(container))>::type&
inline

Definition at line 109 of file Containers.h.

110 {
111 auto it = std::begin(container);
112 std::advance(it, urand(0, uint32(std::size(container)) - 1));
113 return *it;
114 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SelectRandomWeightedContainerElement() [1/2]

template<class C , class Fn >
auto Trinity::Containers::SelectRandomWeightedContainerElement ( C const &  container,
Fn  weightExtractor 
) -> decltype(std::begin(container))
inline

Definition at line 142 of file Containers.h.

143 {
144 std::size_t size = std::size(container);
145 std::size_t i = 0;
146 double* weights = new double[size];
147 double weightSum = 0.0;
148 for (auto const& val : container)
149 {
150 double weight = weightExtractor(val);
151 weights[i++] = weight;
152 weightSum += weight;
153 }
154
155 auto it = std::begin(container);
156 std::advance(it, weightSum > 0.0 ? urandweighted(size, weights) : urand(0, uint32(std::size(container)) - 1));
157 delete[] weights;
158 return it;
159 }
uint32 urandweighted(size_t count, double const *chances)
Definition: Random.cpp:87
+ Here is the call graph for this function:

◆ SelectRandomWeightedContainerElement() [2/2]

template<class C >
auto Trinity::Containers::SelectRandomWeightedContainerElement ( C const &  container,
std::span< double > const &  weights 
) -> decltype(std::begin(container))
inline

Definition at line 126 of file Containers.h.

127 {
128 auto it = std::begin(container);
129 std::advance(it, urandweighted(weights.size(), weights.data()));
130 return it;
131 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function: