Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
KamiliaBlow committed Jan 1, 2025
2 parents 1b2dfa5 + 151a50d commit cb03e7b
Show file tree
Hide file tree
Showing 59 changed files with 156 additions and 162 deletions.
13 changes: 10 additions & 3 deletions src/common/Containers/Utilities/MapUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@

namespace Trinity::Containers
{
template <typename M>
concept Map = requires (M)
{
typename M::key_type;
typename M::mapped_type;
};

/**
* Returns a pointer to mapped value (or the value itself if map stores pointers)
*/
template<class M>
template <Map M>
inline auto MapGetValuePtr(M& map, typename M::key_type const& key)
{
using mapped_type = typename M::mapped_type;
Expand All @@ -46,8 +53,8 @@ inline auto MapGetValuePtr(M& map, typename M::key_type const& key)
return itr != map.end() ? std::addressof(itr->second) : nullptr; // value
}

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 <Map M>
void MultimapErasePair(M& multimap, typename M::key_type const& key, typename M::mapped_type const& value)
{
auto range = multimap.equal_range(key);
for (auto itr = range.first; itr != range.second;)
Expand Down
74 changes: 39 additions & 35 deletions src/common/Utilities/Containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#ifndef TRINITY_CONTAINERS_H
#define TRINITY_CONTAINERS_H

#include "Concepts.h"
#include "Define.h"
#include "MapUtils.h"
#include "Random.h"
#include <algorithm>
#include <iterator>
Expand Down Expand Up @@ -63,14 +63,15 @@ namespace Trinity
{
// resizes <container> to have at most <requestedSize> elements
// if it has more than <requestedSize> elements, the elements to keep are selected randomly
template<class C>
template <std::ranges::forward_range C>
void RandomResize(C& container, std::size_t requestedSize)
{
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");
if (std::size(container) <= requestedSize)
uint32 elementsToProcess = uint32(std::ranges::size(container));
if (elementsToProcess <= requestedSize)
return;
auto keepIt = std::begin(container), curIt = std::begin(container);
uint32 elementsToKeep = requestedSize, elementsToProcess = std::size(container);

auto keepIt = std::ranges::begin(container), curIt = std::ranges::begin(container);
uint32 elementsToKeep = uint32(requestedSize);
while (elementsToProcess)
{
// this element has chance (elementsToKeep / elementsToProcess) of being kept
Expand All @@ -84,15 +85,15 @@ namespace Trinity
++curIt;
--elementsToProcess;
}
container.erase(keepIt, std::end(container));
container.erase(keepIt, std::ranges::end(container));
}

template<class C, class Predicate>
template <std::ranges::forward_range C, invocable_r<bool, std::ranges::range_reference_t<C>> Predicate>
void RandomResize(C& container, Predicate&& predicate, std::size_t requestedSize)
{
//! First use predicate filter
C containerCopy;
std::copy_if(std::begin(container), std::end(container), std::inserter(containerCopy, std::end(containerCopy)), predicate);
std::ranges::copy_if(container, std::inserter(containerCopy, std::ranges::end(containerCopy)), std::forward<Predicate>(predicate));

if (requestedSize)
RandomResize(containerCopy, requestedSize);
Expand All @@ -105,11 +106,11 @@ namespace Trinity
*
* Note: container cannot be empty
*/
template<class C>
inline auto SelectRandomContainerElement(C const& container) -> typename std::add_const<decltype(*std::begin(container))>::type&
template <std::ranges::input_range C>
inline auto SelectRandomContainerElement(C const& container) -> std::add_const_t<decltype(*std::ranges::begin(container))>&
{
auto it = std::begin(container);
std::advance(it, urand(0, uint32(std::size(container)) - 1));
auto it = std::ranges::begin(container);
std::ranges::advance(it, urand(0, uint32(std::ranges::size(container)) - 1));
return *it;
}

Expand All @@ -122,11 +123,11 @@ namespace Trinity
*
* Note: container cannot be empty
*/
template<class C>
inline auto SelectRandomWeightedContainerElement(C const& container, std::span<double> const& weights) -> decltype(std::begin(container))
template <std::ranges::input_range C>
inline auto SelectRandomWeightedContainerElement(C const& container, std::span<double> const& weights) -> decltype(std::ranges::begin(container))
{
auto it = std::begin(container);
std::advance(it, urandweighted(weights.size(), weights.data()));
auto it = std::ranges::begin(container);
std::ranges::advance(it, urandweighted(weights.size(), weights.data()));
return it;
}

Expand All @@ -138,10 +139,10 @@ namespace Trinity
*
* Note: container cannot be empty
*/
template<class C, class Fn>
inline auto SelectRandomWeightedContainerElement(C const& container, Fn weightExtractor) -> decltype(std::begin(container))
template <std::ranges::input_range C, invocable_r<double, std::ranges::range_reference_t<C>> Fn>
inline auto SelectRandomWeightedContainerElement(C const& container, Fn weightExtractor) -> decltype(std::ranges::begin(container))
{
std::size_t size = std::size(container);
std::size_t size = std::ranges::size(container);
std::size_t i = 0;
double* weights = new double[size];
double weightSum = 0.0;
Expand All @@ -152,8 +153,8 @@ namespace Trinity
weightSum += weight;
}

auto it = std::begin(container);
std::advance(it, weightSum > 0.0 ? urandweighted(size, weights) : urand(0, uint32(std::size(container)) - 1));
auto it = std::ranges::begin(container);
std::ranges::advance(it, weightSum > 0.0 ? urandweighted(size, weights) : urand(0, uint32(std::ranges::size(container)) - 1));
delete[] weights;
return it;
}
Expand All @@ -166,10 +167,10 @@ namespace Trinity
* @param begin Beginning of the range to reorder
* @param end End of the range to reorder
*/
template<class Iterator>
template <std::random_access_iterator Iterator>
inline void RandomShuffle(Iterator begin, Iterator end)
{
std::shuffle(begin, end, RandomEngine::Instance());
std::ranges::shuffle(begin, end, RandomEngine::Instance());
}

/**
Expand All @@ -179,10 +180,10 @@ namespace Trinity
*
* @param container Container to reorder
*/
template<class C>
template <std::ranges::random_access_range C>
inline void RandomShuffle(C& container)
{
RandomShuffle(std::begin(container), std::end(container));
RandomShuffle(std::ranges::begin(container), std::ranges::end(container));
}

/**
Expand All @@ -197,8 +198,9 @@ namespace Trinity
*
* @return true if containers have a common element, false otherwise.
*/
template<class Iterator1, class Iterator2>
inline bool Intersects(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
template <std::input_iterator Iterator1, std::sentinel_for<Iterator1> Sentinel1,
std::input_iterator Iterator2, std::sentinel_for<Iterator2> Sentinel2>
inline constexpr bool Intersects(Iterator1 first1, Sentinel1 last1, Iterator2 first2, Sentinel2 last2)
{
while (first1 != last1 && first2 != last2)
{
Expand Down Expand Up @@ -226,16 +228,18 @@ namespace Trinity
*
* @return true if containers have a common element, false otherwise.
*/
template<class Iterator1, class Iterator2, class Predicate>
inline bool Intersects(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, Predicate&& equalPred)
template <std::input_iterator Iterator1, std::sentinel_for<Iterator1> Sentinel1,
std::input_iterator Iterator2, std::sentinel_for<Iterator2> Sentinel2,
invocable_r<bool, std::iter_reference_t<Iterator1>, std::iter_reference_t<Iterator2>> Predicate>
inline constexpr bool Intersects(Iterator1 first1, Sentinel1 last1, Iterator2 first2, Sentinel2 last2, Predicate&& equalPred)
{
while (first1 != last1 && first2 != last2)
{
if (*first1 < *first2)
++first1;
else if (*first2 < *first1)
++first2;
else if (!equalPred(*first1, *first2))
else if (!std::invoke(std::forward<Predicate>(equalPred), *first1, *first2))
++first1, ++first2;
else
return true;
Expand Down Expand Up @@ -275,8 +279,8 @@ namespace Trinity
}
}

template <typename Container, typename Predicate>
void EraseIf(Container& c, Predicate p)
template <std::ranges::forward_range Container, invocable_r<bool, std::ranges::range_reference_t<Container>> Predicate>
inline void EraseIf(Container& c, Predicate p) requires requires { c.erase(c.begin(), c.end()); }
{
if constexpr (std::is_move_assignable_v<decltype(*c.begin())>)
Impl::EraseIfMoveAssignable(c, std::ref(p));
Expand All @@ -291,7 +295,7 @@ namespace Trinity
* This exists as separate overload instead of one function with default argument to allow using
* with vectors of non-default-constructible classes
*/
template<typename T>
template <typename T>
inline decltype(auto) EnsureWritableVectorIndex(std::vector<T>& vec, typename std::vector<T>::size_type i)
{
if (i >= vec.size())
Expand All @@ -306,7 +310,7 @@ namespace Trinity
*
* This overload allows specifying what value to pad vector with during .resize
*/
template<typename T>
template <typename T>
inline decltype(auto) EnsureWritableVectorIndex(std::vector<T>& vec, typename std::vector<T>::size_type i, T const& resizeDefault)
{
if (i >= vec.size())
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/AI/CreatureAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include "CreatureAI.h"
#include "AreaBoundary.h"
#include "Containers.h"
#include "Creature.h"
#include "CreatureAIImpl.h"
#include "CreatureTextMgr.h"
Expand All @@ -27,6 +26,7 @@
#include "Log.h"
#include "Map.h"
#include "MapReference.h"
#include "MapUtils.h"
#include "MotionMaster.h"
#include "ObjectAccessor.h"
#include "Player.h"
Expand Down
8 changes: 4 additions & 4 deletions src/server/game/Achievements/AchievementMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

#include "AchievementMgr.h"
#include "AchievementPackets.h"
#include "DB2HotfixGenerator.h"
#include "DB2Stores.h"
#include "CellImpl.h"
#include "ChatTextBuilder.h"
#include "Containers.h"
#include "DB2HotfixGenerator.h"
#include "DB2Stores.h"
#include "DatabaseEnv.h"
#include "GameTime.h"
#include "GridNotifiersImpl.h"
Expand All @@ -32,10 +31,11 @@
#include "Language.h"
#include "Log.h"
#include "Mail.h"
#include "MapUtils.h"
#include "ObjectMgr.h"
#include "RBAC.h"
#include "StringConvert.h"
#include "ScriptMgr.h"
#include "StringConvert.h"
#include "World.h"
#include "WorldSession.h"
#ifdef ELUNA
Expand Down
6 changes: 3 additions & 3 deletions src/server/game/Achievements/CriteriaHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
#include "CriteriaHandler.h"
#include "ArenaTeamMgr.h"
#include "AzeriteItem.h"
#include "Battleground.h"
#include "BattlePetMgr.h"
#include "Battleground.h"
#include "CollectionMgr.h"
#include "Containers.h"
#include "Creature.h"
#include "DatabaseEnv.h"
#include "DB2Stores.h"
#include "DatabaseEnv.h"
#include "DisableMgr.h"
#include "GameEventMgr.h"
#include "GameTime.h"
Expand All @@ -37,6 +36,7 @@
#include "Log.h"
#include "Map.h"
#include "MapManager.h"
#include "MapUtils.h"
#include "ObjectMgr.h"
#include "PhasingHandler.h"
#include "Player.h"
Expand Down
31 changes: 7 additions & 24 deletions src/server/game/AuctionHouse/AuctionHouseMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "AuctionHouseBot.h"
#include "AuctionHouseMgr.h"
#include "AuctionHousePackets.h"
#include "AccountMgr.h"
#include "AuctionHouseBot.h"
#include "AuctionHousePackets.h"
#include "Bag.h"
#include "BattlePetMgr.h"
#include "DB2Stores.h"
#include "CharacterCache.h"
#include "CollectionMgr.h"
#include "Common.h"
#include "Containers.h"
#include "DB2Stores.h"
#include "DatabaseEnv.h"
#include "GameTime.h"
#include "Language.h"
#include "Log.h"
#include "Mail.h"
#include "MapUtils.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "Player.h"
Expand All @@ -39,8 +39,8 @@
#include "WorldSession.h"
#include "WowTime.h"
#include <boost/dynamic_bitset.hpp>
#include <fmt/ranges.h>
#include <numeric>
#include <sstream>
#include <vector>

enum eAuctionHouse
Expand Down Expand Up @@ -464,25 +464,8 @@ std::string AuctionHouseMgr::BuildCommodityAuctionMailSubject(AuctionMailType ty
std::string AuctionHouseMgr::BuildAuctionMailSubject(uint32 itemId, AuctionMailType type, uint32 auctionId, uint32 itemCount, uint32 battlePetSpeciesId,
ItemContext context, std::vector<int32> const& bonusListIds)
{
std::ostringstream strm;
strm
<< itemId << ':'
<< "0:" // OLD: itemRandomPropertiesId
<< AsUnderlyingType(type) << ':'
<< auctionId << ':'
<< itemCount << ':'
<< battlePetSpeciesId << ':'
<< "0:"
<< "0:"
<< "0:"
<< "0:"
<< uint32(context) << ':'
<< bonusListIds.size();

for (int32 bonusListId : bonusListIds)
strm << ':' << bonusListId;

return strm.str();
return Trinity::StringFormat("{}:0:{}:{}:{}:{}:0:0:0:0:{}:{}:{}",
itemId, AsUnderlyingType(type), auctionId, itemCount, battlePetSpeciesId, context, bonusListIds.size(), fmt::join(bonusListIds, ":"));
}

std::string AuctionHouseMgr::BuildAuctionWonMailBody(ObjectGuid guid, uint64 bid, uint64 buyout)
Expand Down
3 changes: 2 additions & 1 deletion src/server/game/BattlePets/BattlePetMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
*/

#include "BattlePetMgr.h"
#include "DB2Stores.h"
#include "Containers.h"
#include "Creature.h"
#include "DB2Stores.h"
#include "DatabaseEnv.h"
#include "GameTables.h"
#include "GameTime.h"
#include "Item.h"
#include "Log.h"
#include "MapUtils.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "Player.h"
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Battlefield/BattlefieldMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/

#include "BattlefieldMgr.h"
#include "Containers.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "Map.h"
#include "MapUtils.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "ScriptMgr.h"
Expand Down
Loading

0 comments on commit cb03e7b

Please sign in to comment.