8#ifndef META_OCEAN_BASE_UTILITIES_H
9#define META_OCEAN_BASE_UTILITIES_H
36 static std::vector<std::string>
separateValues(
const std::string& values,
const char delimiter =
',',
const bool removeQuotes =
true,
const bool trim =
true);
46 static void sortLowestToFront2(T& value0, T& value1);
57 static void sortLowestToFront3(T& value0, T& value1, T& value2);
69 template <
typename T,
typename TData>
70 static void sortLowestToFront2(T& value0, T& value1, TData& data0, TData& data1);
84 template <
typename T,
typename TData>
85 static void sortLowestToFront3(T& value0, T& value1, T& value2, TData& data0, TData& data1, TData& data2);
95 static void sortHighestToFront2(T& value0, T& value1);
105 template <
typename T>
106 static void sortHighestToFront3(T& value0, T& value1, T& value2);
119 template <
typename T,
typename TData>
120 static void sortHighestToFront2(T& value0, T& value1, TData& data0, TData& data1);
135 template <
typename T,
typename TData>
136 static void sortHighestToFront3(T& value0, T& value1, T& value2, TData& data0, TData& data1, TData& data2);
145 template <
typename T>
146 static unsigned int lowestValue(
const T& value0,
const T& value1,
const T& value2);
155 template <
typename T>
156 static unsigned int highestValue(
const T& value0,
const T& value1,
const T& value2);
166 template <
typename T1,
typename T2>
167 static inline bool sortPairFirst(
const std::pair<T1, T2>& firstPair,
const std::pair<T1, T2>& secondPair);
176 template <
typename T>
177 static inline bool sortPairFirst(
const T& firstPair,
const T& secondPair);
187 template <
typename T1,
typename T2>
188 static inline bool sortPairSecond(
const std::pair<T1, T2>& firstPair,
const std::pair<T1, T2>& secondPair);
197 template <
typename T>
198 static inline bool sortPairSecond(
const T& firstPair,
const T& secondPair);
207 template <
typename T>
208 static inline T divideBy2(
const T& value);
215 static inline unsigned char divideBy255(
const unsigned int value);
223 static inline bool isPowerOfTwo(
const unsigned int value);
230 static inline unsigned int smallestPowerOfTwo(
const unsigned int value);
239 static void multiply64(
const uint64_t left,
const uint64_t right, uint64_t& resultHigh, uint64_t& resultLow);
249 static inline int compareProducts(
const uint64_t leftFactor1,
const uint64_t leftFactor2,
const uint64_t rightFactor1,
const uint64_t rightFactor2);
264inline T
minmax(
const T& lowerBoundary,
const T& value,
const T& upperBoundary);
280inline T
modulo(
const T& value,
const T& ring);
292inline T
ringDistance(
const T& value0,
const T& value1,
const T& ring);
300inline unsigned int sqr(
const char value);
308inline unsigned int sqr(
const unsigned char value);
316inline unsigned int sqr(
const short value);
324inline unsigned int sqr(
const unsigned short value);
332inline unsigned int sqr(
const int value);
340inline unsigned int sqr(
const unsigned int value);
348inline unsigned long sqr(
const long value);
356inline unsigned long sqr(
const unsigned long value);
364inline unsigned long long sqr(
const long long value);
372inline unsigned long long sqr(
const unsigned long long value);
380inline float sqr(
const float value);
388inline double sqr(
const double value);
397inline unsigned int sqrDistance(
const char first,
const char second);
406inline unsigned int sqrDistance(
const unsigned char first,
const unsigned char second);
415inline unsigned int sqrDistance(
const short first,
const short second);
424inline unsigned int sqrDistance(
const unsigned short first,
const unsigned short second);
433inline unsigned int sqrDistance(
const int first,
const int second);
442inline unsigned int sqrDistance(
const unsigned int first,
const unsigned int second);
451inline unsigned long long sqrDistance(
const long long first,
const long long second);
460inline unsigned long long sqrDistance(
const unsigned long long first,
const unsigned long long second);
469inline float sqrDistance(
const float first,
const float second);
478inline double sqrDistance(
const double first,
const double second);
487inline unsigned char absDistance(
const char first,
const char second);
496inline unsigned char absDistance(
const unsigned char first,
const unsigned char second);
505inline unsigned short absDistance(
const short first,
const short second);
514inline unsigned short absDistance(
const unsigned short first,
const unsigned short second);
523inline unsigned int absDistance(
const int first,
const int second);
532inline unsigned int absDistance(
const unsigned int first,
const unsigned int second);
541inline unsigned long long absDistance(
const long long first,
const long long second);
550inline unsigned long long absDistance(
const unsigned long long first,
const unsigned long long second);
565static inline unsigned int mirrorValue(
const int value,
const unsigned int size);
575static inline size_t countElements(
const std::vector<std::vector<T>>& vectors);
584static inline bool hasElement(
const std::vector<T>& elements,
const T& element);
594static inline std::vector<T>
concatenate(
const std::vector<std::vector<T>>& vectors);
605static inline void createIndices(
const size_t numberIndices,
const T& startIndex, std::vector<T>& indices);
616[[nodiscard]]
static inline std::vector<T>
createIndices(
const size_t numberIndices,
const T& startIndex);
632 template <
typename T1,
typename T2>
635 size_t seed = std::hash<T1>{}(pair.first);
636 seed ^= std::hash<T2>{}(pair.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
645 if (!(value0 < value1))
647 std::swap(value0, value1);
650 ocean_assert(value0 <= value1);
656 if (!(value0 < value1))
658 std::swap(value0, value1);
661 if (!(value0 < value2))
663 std::swap(value0, value2);
666 if (!(value1 < value2))
668 std::swap(value1, value2);
671 ocean_assert(value0 <= value1);
672 ocean_assert(value1 <= value2);
675template <
typename T,
typename TData>
678 if (!(value0 < value1))
680 std::swap(value0, value1);
681 std::swap(data0, data1);
684 ocean_assert(value0 <= value1);
687template <
typename T,
typename TData>
690 if (!(value0 < value1))
692 std::swap(value0, value1);
693 std::swap(data0, data1);
696 if (!(value0 < value2))
698 std::swap(value0, value2);
699 std::swap(data0, data2);
702 if (!(value1 < value2))
704 std::swap(value1, value2);
705 std::swap(data1, data2);
708 ocean_assert(value0 <= value1);
709 ocean_assert(value1 <= value2);
717 std::swap(value0, value1);
720 ocean_assert(value0 >= value1);
728 std::swap(value0, value1);
733 std::swap(value0, value2);
738 std::swap(value1, value2);
741 ocean_assert(value0 >= value1);
742 ocean_assert(value1 >= value2);
745template <
typename T,
typename TData>
750 std::swap(value0, value1);
751 std::swap(data0, data1);
754 ocean_assert(value0 >= value1);
757template <
typename T,
typename TData>
762 std::swap(value0, value1);
763 std::swap(data0, data1);
768 std::swap(value0, value2);
769 std::swap(data0, data2);
774 std::swap(value1, value2);
775 std::swap(data1, data2);
778 ocean_assert(value0 >= value1);
779 ocean_assert(value1 >= value2);
785 if (value0 <= value1)
787 if (value0 <= value2)
798 if (value1 <= value2)
812 if (value0 >= value1)
814 if (value0 >= value2)
825 if (value1 >= value2)
836template <
typename T1,
typename T2>
839 return firstPair.first < secondPair.first;
845 return firstPair.first < secondPair.first;
848template <
typename T1,
typename T2>
851 return firstPair.second < secondPair.second;
857 return firstPair.second < secondPair.second;
880 ocean_assert(value < 65535u);
881 ocean_assert(value / 255u == (value + 1u + (value >> 8u)) >> 8u);
883 return (
unsigned char)((value + 1u + (value >> 8u)) >> 8u);
899 ocean_assert((value == 1u || value == 2u || value == 4u || value == 8u || value == 16u || value == 32u || value == 64u
900 || value == 128u || value == 256u || value == 512u || value == 1024u || value == 2048u
901 || value == 4096u || value == 8192 || value == 16384u || value == 32768u || value == 65536u
902 || value == 131072 || value == 262144u || value == 524288u || value == 1048576u || value == 2097152u
903 || value == 4194304u || value == 8388608u || value == 16777216u || value == 33554432u || value == 67108864u
904 || value == 134217728u || value == 268435456u || value == 536870912u || value == 1073741824u || value == 2147483648u) == (value != 0u && (value & (value - 1u)) == 0u));
906 return value != 0u && (value & (value - 1u)) == 0u;
911 ocean_assert(value <= 0x80000000u);
913 unsigned int powerOfTwoValue = 0x80000000u;
915 while (powerOfTwoValue != 0u && (powerOfTwoValue >> 1) >= value)
917 powerOfTwoValue >>= 1u;
920 return powerOfTwoValue;
927inline T
minmax(
const T& lowerBoundary,
const T& value,
const T& upperBoundary)
929 ocean_assert(lowerBoundary <= upperBoundary);
931 ocean_assert((value < lowerBoundary ? lowerBoundary : (value > upperBoundary ? upperBoundary : value)) == (max(lowerBoundary, min(value, upperBoundary))));
932 return value < lowerBoundary ? lowerBoundary : (value > upperBoundary ? upperBoundary : value);
939inline T
minmax(
const T& lowerBoundary,
const T& value,
const T& upperBoundary)
941 ocean_assert(lowerBoundary <= upperBoundary);
942 return max(lowerBoundary, min(upperBoundary, value));
948inline T
modulo(
const T& value,
const T& ring)
950 ocean_assert(ring > 0);
959 ocean_assert(value % ring < ring);
964 const T result = (ring - 1) - (-(value + 1) % ring);
965 ocean_assert(result >= 0 && result < ring);
972inline unsigned char modulo(
const unsigned char& value,
const unsigned char& ring)
978inline unsigned short modulo(
const unsigned short& value,
const unsigned short& ring)
984inline unsigned int modulo(
const unsigned int& value,
const unsigned int& ring)
990inline unsigned long long modulo(
const unsigned long long& value,
const unsigned long long& ring)
996inline double modulo(
const double& value,
const double& ring)
998 ocean_assert(ring > 0);
1007 return fmod(value, ring);
1011 const double intermediate = ring - (fmod(-value, ring));
1012 ocean_assert(intermediate >= 0.0 && intermediate <= ring);
1014 return intermediate == ring ? 0.0 : intermediate;
1019inline float modulo(
const float& value,
const float& ring)
1021 ocean_assert(ring > 0);
1030 return fmod(value, ring);
1034 const float intermediate = ring - (fmod(-value, ring));
1035 ocean_assert(intermediate >= 0.0f && intermediate <= ring);
1037 return intermediate == ring ? 0.0f : intermediate;
1041template <
typename T>
1044 ocean_assert(value0 >= 0 && value0 < ring);
1045 ocean_assert(value1 >= 0 && value1 < ring);
1047 const T low = min(value0, value1);
1048 const T high = max(value0, value1);
1050 return min(high - low, low + ring - high);
1053inline unsigned int sqr(
const char value)
1055 return value * value;
1058inline unsigned int sqr(
const unsigned char value)
1060 return value * value;
1063inline unsigned int sqr(
const short value)
1065 return value * value;
1068inline unsigned int sqr(
const unsigned short value)
1070 return value * value;
1073inline unsigned int sqr(
const int value)
1075 return value * value;
1078inline unsigned int sqr(
const unsigned int value)
1080 return value * value;
1083inline unsigned long sqr(
const long value)
1085 return value * value;
1088inline unsigned long sqr(
const unsigned long value)
1090 return value * value;
1093inline unsigned long long sqr(
const long long value)
1095 return value * value;
1098inline unsigned long long sqr(
const unsigned long long value)
1100 return value * value;
1103inline float sqr(
const float value)
1105 return value * value;
1108inline double sqr(
const double value)
1110 return value * value;
1115 const int distance = int(first) - int(second);
1116 return distance * distance;
1119inline unsigned int sqrDistance(
const unsigned char first,
const unsigned char second)
1121 const int distance = int(first) - int(second);
1122 return distance * distance;
1127 const int distance = int(first) - int(second);
1128 return distance * distance;
1131inline unsigned int sqrDistance(
const unsigned short first,
const unsigned short second)
1133 const int distance = int(first) - int(second);
1134 return distance * distance;
1139 const int distance = first - second;
1140 return distance * distance;
1143inline unsigned int sqrDistance(
const unsigned int first,
const unsigned int second)
1145 ocean_assert(first <= (
unsigned int)INT_MAX);
1146 ocean_assert(second <= (
unsigned int)INT_MAX);
1148 const int distance = int(first) - int(second);
1149 return distance * distance;
1152inline unsigned long long sqrDistance(
const long long first,
const long long second)
1154 const long long distance = first - second;
1155 return distance * distance;
1158inline unsigned long long sqrDistance(
const unsigned long long first,
const unsigned long long second)
1160 ocean_assert(first <= (
unsigned long long)LLONG_MAX);
1161 ocean_assert(second <= (
unsigned long long)LLONG_MAX);
1163 const long long distance = (
long long)first - (
long long)second;
1164 return distance * distance;
1169 const float distance = first - second;
1170 return distance * distance;
1175 const double distance = first - second;
1176 return distance * distance;
1181 return (
unsigned char)(::abs(
int(first) -
int(second)));
1184inline unsigned char absDistance(
const unsigned char first,
const unsigned char second)
1186 return (
unsigned char)(::abs(
int(first - second)));
1189inline unsigned short absDistance(
const short first,
const short second)
1191 return (
unsigned short)(::abs(
int(first) -
int(second)));
1194inline unsigned short absDistance(
const unsigned short first,
const unsigned short second)
1196 return (
unsigned short)(::abs(
int(first - second)));
1201 return (
unsigned int)(::abs(first - second));
1204inline unsigned int absDistance(
const unsigned int first,
const unsigned int second)
1206 return (
unsigned int)(::abs(
int(first - second)));
1209inline unsigned long long absDistance(
const long long first,
const long long second)
1211#if defined(__clang__)
1212 return (first > second) ? (first - second) : (second - first);
1213#elif (defined(OCEAN_COMPILER_MSC) && OCEAN_MSC_VERSION <= 1500)
1214 return (first > second) ? (first - second) : (second - first);
1216 return ::abs(first - second);
1220inline unsigned long long absDistance(
const unsigned long long first,
const unsigned long long second)
1222#if defined(__clang__)
1223 return (first > second) ? (first - second) : (second - first);
1224#elif (defined(OCEAN_COMPILER_MSC) && OCEAN_MSC_VERSION <= 1500)
1225 return (first > second) ? (first - second) : (second - first);
1227 return ::abs((
long long)(first - second));
1231inline unsigned int mirrorValue(
const int value,
const unsigned int size)
1233 ocean_assert(size != 0u);
1234 ocean_assert(size <= 2147483647);
1235 ocean_assert(value >= -
int(size) && value <=
int(size * 2));
1238 const int result = (value < 0) ? (-value - 1) : (value >= int(size) ? (int(size + size) - value - 1) : value);
1239 ocean_assert(result >= 0 && result <
int(size));
1242 return (value < 0) ? (-value - 1) : (value >= int(size) ? (int(size + size) - value - 1) : value);
1245template <
typename T>
1250 for (
size_t n = 0; n < vectors.size(); ++n)
1252 size += vectors[n].size();
1258template <
typename T>
1259inline bool hasElement(
const std::vector<T>& elements,
const T& element)
1261 for (
const T& value : elements)
1263 if (value == element)
1272template <
typename T>
1273inline std::vector<T>
concatenate(
const std::vector<std::vector<T>>& vectors)
1277 std::vector<T> result;
1278 result.reserve(size);
1280 for (
size_t n = 0; n < vectors.size(); ++n)
1282 result.insert(result.end(), vectors[n].begin(), vectors[n].end());
1288template <
typename T>
1289inline void createIndices(
const size_t numberIndices,
const T& startIndex, std::vector<T>& indices)
1291 ocean_assert(numberIndices > 0);
1293 indices.reserve(numberIndices);
1296 T index = startIndex;
1298 for (
size_t n = 0; n < numberIndices; ++n)
1300 indices.emplace_back(index++);
1304template <
typename T>
1305inline std::vector<T>
createIndices(
const size_t numberIndices,
const T& startIndex)
1307 std::vector<T> result;
1315 uint64_t leftProductHigh, leftProductLow;
1316 multiply64(leftFactor1, leftFactor2, leftProductHigh, leftProductLow);
1318 uint64_t rightProductHigh, rightProductLow;
1319 multiply64(rightFactor1, rightFactor2, rightProductHigh, rightProductLow);
1321 return leftProductHigh == rightProductHigh ? (leftProductLow > rightProductLow) - (leftProductLow < rightProductLow) : (leftProductHigh > rightProductHigh) - (leftProductHigh < rightProductHigh);
This class implements basic utility functions.
Definition base/Utilities.h:25
static bool sortPairSecond(const std::pair< T1, T2 > &firstPair, const std::pair< T1, T2 > &secondPair)
Compares two pair objects and returns whether the second value of the first pair is lesser than the s...
Definition base/Utilities.h:849
static void sortLowestToFront2(T &value0, T &value1)
Sorts two values so that the lowest value will finally be the first value.
Definition base/Utilities.h:643
static unsigned int smallestPowerOfTwo(const unsigned int value)
Returns the smallest power of two value that is equal or larger than a given value.
Definition base/Utilities.h:909
static std::vector< std::string > separateValues(const std::string &values, const char delimiter=',', const bool removeQuotes=true, const bool trim=true)
Separates a set of given values.
static bool isPowerOfTwo(const unsigned int value)
Returns whether a given value is a power of two.
Definition base/Utilities.h:886
static unsigned int highestValue(const T &value0, const T &value1, const T &value2)
Returns the index of the highest value, if two or more values have the highest value the smallest ind...
Definition base/Utilities.h:810
static T divideBy2(const T &value)
This function divides a given value by two.
Definition base/Utilities.h:861
static int compareProducts(const uint64_t leftFactor1, const uint64_t leftFactor2, const uint64_t rightFactor1, const uint64_t rightFactor2)
Compares the product specified by the first two factors with the product specified by the last two fa...
Definition base/Utilities.h:1313
static unsigned char divideBy255(const unsigned int value)
This function applies a fast division by 255 for unsigned integer values.
Definition base/Utilities.h:878
static unsigned int lowestValue(const T &value0, const T &value1, const T &value2)
Returns the index of the lowest value, if two or more values have the lowest value the smallest index...
Definition base/Utilities.h:783
static void sortLowestToFront3(T &value0, T &value1, T &value2)
Sorts three values so that the lowest value will finally be the first value.
Definition base/Utilities.h:654
static void sortHighestToFront3(T &value0, T &value1, T &value2)
Sorts three values so that the highest value will finally be the first value.
Definition base/Utilities.h:724
static bool sortPairFirst(const std::pair< T1, T2 > &firstPair, const std::pair< T1, T2 > &secondPair)
Compares two pair objects and returns whether the first value of the first pair is lesser than the fi...
Definition base/Utilities.h:837
static void sortHighestToFront2(T &value0, T &value1)
Sorts two values so that the highest value will finally be the first value.
Definition base/Utilities.h:713
static void multiply64(const uint64_t left, const uint64_t right, uint64_t &resultHigh, uint64_t &resultLow)
Multiplies the specified 64 bit integers and stores the 128 bit integer result in resultHigh (64 most...
T minmax(const T &lowerBoundary, const T &value, const T &upperBoundary)
This function fits a given parameter into a specified value range.
Definition base/Utilities.h:927
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition base/Utilities.h:1113
static std::vector< T > concatenate(const std::vector< std::vector< T > > &vectors)
Concatenates a given set of vectors to one vector.
Definition base/Utilities.h:1273
unsigned char absDistance(const char first, const char second)
Returns the absolute distance between two values.
Definition base/Utilities.h:1179
static void createIndices(const size_t numberIndices, const T &startIndex, std::vector< T > &indices)
Fills a vector with increasing index values.
Definition base/Utilities.h:1289
T modulo(const T &value, const T &ring)
Returns the modulo value of a given parameter within a ring allowing positive and negative parameters...
Definition base/Utilities.h:948
static size_t countElements(const std::vector< std::vector< T > > &vectors)
Determines the entire number of elements that are stored in a vector holding a vector of elements.
Definition base/Utilities.h:1246
T ringDistance(const T &value0, const T &value1, const T &ring)
Returns the distance between two values inside defined ring.
Definition base/Utilities.h:1042
unsigned int sqr(const char value)
Returns the square value of a given value.
Definition base/Utilities.h:1053
static unsigned int mirrorValue(const int value, const unsigned int size)
Mirrors a given value if necessary.
Definition base/Utilities.h:1231
The namespace covering the entire Ocean framework.
Definition Accessor.h:15
static bool hasElement(const std::vector< T > &elements, const T &element)
Returns whether a vector holds a specified element.
Definition base/Utilities.h:1259
Helper struct allowing to determine a hash value for a std::pair.
Definition base/Utilities.h:624
std::size_t operator()(const std::pair< T1, T2 > &pair) const
Returns the hash value for a std::pair.
Definition base/Utilities.h:633