GigaPasjans
Loading...
Searching...
No Matches
Collections.h
1// Plik utworzony przez Piotra ChudziƄskiego w dniu 15.04.2025
2#pragma once
3
4#include <foundation/DataTypes.h>
5#include <foundation/Macro.h>
6#include <algorithm>
7#include <vector>
8
13template<typename T>
14concept Integral = std::is_integral_v<T>;
15
20template<auto Nullable>
22{
23public:
28 template<typename K, typename V>
29 FORCE_INLINE static void RemoveByKey(std::vector<std::pair<K, V>> &vector, const K &key)
30 {
31 std::erase_if(vector, [&key](const std::pair<K, V> &pair) { return pair.first == key; });
32 }
33
38 template<typename K, typename V>
39 FORCE_INLINE static typename std::vector<std::pair<K, V>>::iterator
40 GetIteratorByKey(std::vector<std::pair<K, V>> &vector, const K &key)
41 {
42 return std::find_if(vector.begin(), vector.end(),
43 [&key](const std::pair<K, V> &pair) { return pair.first == key; });
44 }
45
50 template<typename K, typename V>
51 FORCE_INLINE static V GetByKey(std::vector<std::pair<K, V>> &vector, const K &key)
52 {
53 auto iterator = GetIteratorByKey(vector, key);
54
55 if (iterator == vector.end())
56 return Nullable;
57
58 return iterator->second;
59 }
60
65 template<typename K, typename V>
66 FORCE_INLINE static bool MoveToLastIndex(std::vector<std::pair<K, V>> &vector, const K &key)
67 {
68 auto iterator = GetIteratorByKey(vector, key);
69
70 if (iterator == vector.end())
71 return false;
72
73
74 std::pair<K, V> element = *iterator;
75 vector.erase(iterator);
76 vector.push_back(element);
77 return true;
78 }
79};
80
85class Vector
86{
87public:
92 template<typename T>
93 FORCE_INLINE static int32 IndexOf(const std::vector<T> &vector, const T &element)
94 {
95 auto iterator = std::find(vector.begin(), vector.end(), element);
96 if (iterator == vector.end())
97 return -1;
98
99 return static_cast<int32>(iterator - vector.begin());
100 }
101
106 template<Integral R = int64, typename T>
107 FORCE_INLINE static R Size(const std::vector<T> &vector)
108 {
109 return static_cast<R>(vector.size());
110 }
111
116 template<typename T>
117 FORCE_INLINE static bool Remove(std::vector<T> &vector, const T &element)
118 {
119 auto indexOf = IndexOf(vector, element);
120 if (indexOf < 0)
121 return false;
122
123 vector.erase(vector.begin() + indexOf);
124 return true;
125 }
126
131 template<typename T>
132 FORCE_INLINE static bool Contains(const std::vector<T> &cards, const T &element)
133 {
134 return std::ranges::find(cards, element) != std::ranges::end(cards);
135 }
136};
A utility which contains essential operation functions for list of pairs.
Definition Collections.h:22
static FORCE_INLINE bool MoveToLastIndex(std::vector< std::pair< K, V > > &vector, const K &key)
Finds and moves entry to the top of a list for given key.
Definition Collections.h:66
static FORCE_INLINE void RemoveByKey(std::vector< std::pair< K, V > > &vector, const K &key)
Finds and deletes entry for given key.
Definition Collections.h:29
static FORCE_INLINE std::vector< std::pair< K, V > >::iterator GetIteratorByKey(std::vector< std::pair< K, V > > &vector, const K &key)
Finds and returns entry's iterator for given key.
Definition Collections.h:40
static FORCE_INLINE V GetByKey(std::vector< std::pair< K, V > > &vector, const K &key)
Finds and returns entry's value for given key.
Definition Collections.h:51
A utility which contains essential operation functions for list.
Definition Collections.h:86
static FORCE_INLINE bool Remove(std::vector< T > &vector, const T &element)
Finds and deletes given value.
Definition Collections.h:117
static FORCE_INLINE int32 IndexOf(const std::vector< T > &vector, const T &element)
Finds and returns index of given value.
Definition Collections.h:93
static FORCE_INLINE R Size(const std::vector< T > &vector)
Returns size of given list.
Definition Collections.h:107
static FORCE_INLINE bool Contains(const std::vector< T > &cards, const T &element)
Checks whether given value is present in a list.
Definition Collections.h:132
Checks whether T is integral.
Definition Collections.h:14