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);
117 template <
typename T>
118 static void sortHighestToFront4(T& value0, T& value1, T& value2, T& value3);
131 template <
typename T,
typename TData>
132 static void sortHighestToFront2(T& value0, T& value1, TData& data0, TData& data1);
147 template <
typename T,
typename TData>
148 static void sortHighestToFront3(T& value0, T& value1, T& value2, TData& data0, TData& data1, TData& data2);
157 template <
typename T>
158 static unsigned int lowestValue(
const T& value0,
const T& value1,
const T& value2);
167 template <
typename T>
168 static unsigned int highestValue(
const T& value0,
const T& value1,
const T& value2);
178 template <
typename T1,
typename T2>
179 static inline bool sortPairFirst(
const std::pair<T1, T2>& firstPair,
const std::pair<T1, T2>& secondPair);
188 template <
typename T>
189 static inline bool sortPairFirst(
const T& firstPair,
const T& secondPair);
199 template <
typename T1,
typename T2>
200 static inline bool sortPairSecond(
const std::pair<T1, T2>& firstPair,
const std::pair<T1, T2>& secondPair);
209 template <
typename T>
210 static inline bool sortPairSecond(
const T& firstPair,
const T& secondPair);
219 template <
typename T>
220 static inline T divideBy2(
const T& value);
227 static inline unsigned char divideBy255(
const unsigned int value);
235 static inline bool isPowerOfTwo(
const unsigned int value);
242 static inline unsigned int smallestPowerOfTwo(
const unsigned int value);
251 static void multiply64(
const uint64_t left,
const uint64_t right, uint64_t& resultHigh, uint64_t& resultLow);
261 static inline int compareProducts(
const uint64_t leftFactor1,
const uint64_t leftFactor2,
const uint64_t rightFactor1,
const uint64_t rightFactor2);
276inline T
minmax(
const T& lowerBoundary,
const T& value,
const T& upperBoundary);
292inline T
modulo(
const T& value,
const T& ring);
304inline T
ringDistance(
const T& value0,
const T& value1,
const T& ring);
312inline unsigned int sqr(
const char value);
320inline unsigned int sqr(
const unsigned char value);
328inline unsigned int sqr(
const short value);
336inline unsigned int sqr(
const unsigned short value);
344inline unsigned int sqr(
const int value);
352inline unsigned int sqr(
const unsigned int value);
360inline unsigned long sqr(
const long value);
368inline unsigned long sqr(
const unsigned long value);
376inline unsigned long long sqr(
const long long value);
384inline unsigned long long sqr(
const unsigned long long value);
392inline float sqr(
const float value);
400inline double sqr(
const double value);
409inline unsigned int sqrDistance(
const char first,
const char second);
418inline unsigned int sqrDistance(
const unsigned char first,
const unsigned char second);
427inline unsigned int sqrDistance(
const short first,
const short second);
436inline unsigned int sqrDistance(
const unsigned short first,
const unsigned short second);
445inline unsigned int sqrDistance(
const int first,
const int second);
454inline unsigned int sqrDistance(
const unsigned int first,
const unsigned int second);
463inline unsigned long long sqrDistance(
const long long first,
const long long second);
472inline unsigned long long sqrDistance(
const unsigned long long first,
const unsigned long long second);
481inline float sqrDistance(
const float first,
const float second);
490inline double sqrDistance(
const double first,
const double second);
499inline unsigned char absDistance(
const char first,
const char second);
508inline unsigned char absDistance(
const unsigned char first,
const unsigned char second);
517inline unsigned short absDistance(
const short first,
const short second);
526inline unsigned short absDistance(
const unsigned short first,
const unsigned short second);
535inline unsigned int absDistance(
const int first,
const int second);
544inline unsigned int absDistance(
const unsigned int first,
const unsigned int second);
553inline unsigned long long absDistance(
const long long first,
const long long second);
562inline unsigned long long absDistance(
const unsigned long long first,
const unsigned long long second);
577static inline unsigned int mirrorValue(
const int value,
const unsigned int size);
587static inline size_t countElements(
const std::vector<std::vector<T>>& vectors);
596static inline bool hasElement(
const std::vector<T>& elements,
const T& element);
606static inline std::vector<T>
concatenate(
const std::vector<std::vector<T>>& vectors);
617static inline void createIndices(
const size_t numberIndices,
const T& startIndex, std::vector<T>& indices);
628[[nodiscard]]
static inline std::vector<T>
createIndices(
const size_t numberIndices,
const T& startIndex);
644 template <
typename T1,
typename T2>
647 size_t seed = std::hash<T1>{}(pair.first);
648 seed ^= std::hash<T2>{}(pair.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
657 if (!(value0 < value1))
659 std::swap(value0, value1);
662 ocean_assert(value0 <= value1);
668 if (!(value0 < value1))
670 std::swap(value0, value1);
673 if (!(value0 < value2))
675 std::swap(value0, value2);
678 if (!(value1 < value2))
680 std::swap(value1, value2);
683 ocean_assert(value0 <= value1);
684 ocean_assert(value1 <= value2);
687template <
typename T,
typename TData>
690 if (!(value0 < value1))
692 std::swap(value0, value1);
693 std::swap(data0, data1);
696 ocean_assert(value0 <= value1);
699template <
typename T,
typename TData>
702 if (!(value0 < value1))
704 std::swap(value0, value1);
705 std::swap(data0, data1);
708 if (!(value0 < value2))
710 std::swap(value0, value2);
711 std::swap(data0, data2);
714 if (!(value1 < value2))
716 std::swap(value1, value2);
717 std::swap(data1, data2);
720 ocean_assert(value0 <= value1);
721 ocean_assert(value1 <= value2);
729 std::swap(value0, value1);
732 ocean_assert(value0 >= value1);
740 std::swap(value0, value1);
745 std::swap(value0, value2);
750 std::swap(value1, value2);
753 ocean_assert(value0 >= value1);
754 ocean_assert(value1 >= value2);
762 std::swap(value0, value1);
767 std::swap(value2, value3);
772 std::swap(value0, value2);
777 std::swap(value1, value3);
782 std::swap(value1, value2);
785 ocean_assert(value0 >= value1);
786 ocean_assert(value1 >= value2);
787 ocean_assert(value2 >= value3);
791template <
typename T,
typename TData>
796 std::swap(value0, value1);
797 std::swap(data0, data1);
800 ocean_assert(value0 >= value1);
803template <
typename T,
typename TData>
808 std::swap(value0, value1);
809 std::swap(data0, data1);
814 std::swap(value0, value2);
815 std::swap(data0, data2);
820 std::swap(value1, value2);
821 std::swap(data1, data2);
824 ocean_assert(value0 >= value1);
825 ocean_assert(value1 >= value2);
831 if (value0 <= value1)
833 if (value0 <= value2)
844 if (value1 <= value2)
858 if (value0 >= value1)
860 if (value0 >= value2)
871 if (value1 >= value2)
882template <
typename T1,
typename T2>
885 return firstPair.first < secondPair.first;
891 return firstPair.first < secondPair.first;
894template <
typename T1,
typename T2>
897 return firstPair.second < secondPair.second;
903 return firstPair.second < secondPair.second;
926 ocean_assert(value < 65535u);
927 ocean_assert(value / 255u == (value + 1u + (value >> 8u)) >> 8u);
929 return (
unsigned char)((value + 1u + (value >> 8u)) >> 8u);
945 ocean_assert((value == 1u || value == 2u || value == 4u || value == 8u || value == 16u || value == 32u || value == 64u
946 || value == 128u || value == 256u || value == 512u || value == 1024u || value == 2048u
947 || value == 4096u || value == 8192 || value == 16384u || value == 32768u || value == 65536u
948 || value == 131072 || value == 262144u || value == 524288u || value == 1048576u || value == 2097152u
949 || value == 4194304u || value == 8388608u || value == 16777216u || value == 33554432u || value == 67108864u
950 || value == 134217728u || value == 268435456u || value == 536870912u || value == 1073741824u || value == 2147483648u) == (value != 0u && (value & (value - 1u)) == 0u));
952 return value != 0u && (value & (value - 1u)) == 0u;
957 ocean_assert(value <= 0x80000000u);
959 unsigned int powerOfTwoValue = 0x80000000u;
961 while (powerOfTwoValue != 0u && (powerOfTwoValue >> 1) >= value)
963 powerOfTwoValue >>= 1u;
966 return powerOfTwoValue;
973inline T
minmax(
const T& lowerBoundary,
const T& value,
const T& upperBoundary)
975 ocean_assert(lowerBoundary <= upperBoundary);
977 ocean_assert((value < lowerBoundary ? lowerBoundary : (value > upperBoundary ? upperBoundary : value)) == (max(lowerBoundary, min(value, upperBoundary))));
978 return value < lowerBoundary ? lowerBoundary : (value > upperBoundary ? upperBoundary : value);
985inline T
minmax(
const T& lowerBoundary,
const T& value,
const T& upperBoundary)
987 ocean_assert(lowerBoundary <= upperBoundary);
988 return max(lowerBoundary, min(upperBoundary, value));
994inline T
modulo(
const T& value,
const T& ring)
996 ocean_assert(ring > 0);
1005 ocean_assert(value % ring < ring);
1006 return value % ring;
1010 const T result = (ring - 1) - (-(value + 1) % ring);
1011 ocean_assert(result >= 0 && result < ring);
1018inline unsigned char modulo(
const unsigned char& value,
const unsigned char& ring)
1020 return value % ring;
1024inline unsigned short modulo(
const unsigned short& value,
const unsigned short& ring)
1026 return value % ring;
1030inline unsigned int modulo(
const unsigned int& value,
const unsigned int& ring)
1032 return value % ring;
1036inline unsigned long long modulo(
const unsigned long long& value,
const unsigned long long& ring)
1038 return value % ring;
1042inline double modulo(
const double& value,
const double& ring)
1044 ocean_assert(ring > 0);
1053 return fmod(value, ring);
1057 const double intermediate = ring - (fmod(-value, ring));
1058 ocean_assert(intermediate >= 0.0 && intermediate <= ring);
1060 return intermediate == ring ? 0.0 : intermediate;
1065inline float modulo(
const float& value,
const float& ring)
1067 ocean_assert(ring > 0);
1076 return fmod(value, ring);
1080 const float intermediate = ring - (fmod(-value, ring));
1081 ocean_assert(intermediate >= 0.0f && intermediate <= ring);
1083 return intermediate == ring ? 0.0f : intermediate;
1087template <
typename T>
1090 ocean_assert(value0 >= 0 && value0 < ring);
1091 ocean_assert(value1 >= 0 && value1 < ring);
1093 const T low = min(value0, value1);
1094 const T high = max(value0, value1);
1096 return min(high - low, low + ring - high);
1099inline unsigned int sqr(
const char value)
1101 return value * value;
1104inline unsigned int sqr(
const unsigned char value)
1106 return value * value;
1109inline unsigned int sqr(
const short value)
1111 return value * value;
1114inline unsigned int sqr(
const unsigned short value)
1116 return value * value;
1119inline unsigned int sqr(
const int value)
1121 return value * value;
1124inline unsigned int sqr(
const unsigned int value)
1126 return value * value;
1129inline unsigned long sqr(
const long value)
1131 return value * value;
1134inline unsigned long sqr(
const unsigned long value)
1136 return value * value;
1139inline unsigned long long sqr(
const long long value)
1141 return value * value;
1144inline unsigned long long sqr(
const unsigned long long value)
1146 return value * value;
1149inline float sqr(
const float value)
1151 return value * value;
1154inline double sqr(
const double value)
1156 return value * value;
1161 const int distance = int(first) - int(second);
1162 return distance * distance;
1165inline unsigned int sqrDistance(
const unsigned char first,
const unsigned char second)
1167 const int distance = int(first) - int(second);
1168 return distance * distance;
1173 const int distance = int(first) - int(second);
1174 return distance * distance;
1177inline unsigned int sqrDistance(
const unsigned short first,
const unsigned short second)
1179 const int distance = int(first) - int(second);
1180 return distance * distance;
1185 const int distance = first - second;
1186 return distance * distance;
1189inline unsigned int sqrDistance(
const unsigned int first,
const unsigned int second)
1191 ocean_assert(first <= (
unsigned int)INT_MAX);
1192 ocean_assert(second <= (
unsigned int)INT_MAX);
1194 const int distance = int(first) - int(second);
1195 return distance * distance;
1198inline unsigned long long sqrDistance(
const long long first,
const long long second)
1200 const long long distance = first - second;
1201 return distance * distance;
1204inline unsigned long long sqrDistance(
const unsigned long long first,
const unsigned long long second)
1206 ocean_assert(first <= (
unsigned long long)LLONG_MAX);
1207 ocean_assert(second <= (
unsigned long long)LLONG_MAX);
1209 const long long distance = (
long long)first - (
long long)second;
1210 return distance * distance;
1215 const float distance = first - second;
1216 return distance * distance;
1221 const double distance = first - second;
1222 return distance * distance;
1227 return (
unsigned char)(::abs(
int(first) -
int(second)));
1230inline unsigned char absDistance(
const unsigned char first,
const unsigned char second)
1232 return (
unsigned char)(::abs(
int(first - second)));
1235inline unsigned short absDistance(
const short first,
const short second)
1237 return (
unsigned short)(::abs(
int(first) -
int(second)));
1240inline unsigned short absDistance(
const unsigned short first,
const unsigned short second)
1242 return (
unsigned short)(::abs(
int(first - second)));
1247 return (
unsigned int)(::abs(first - second));
1250inline unsigned int absDistance(
const unsigned int first,
const unsigned int second)
1252 return (
unsigned int)(::abs(
int(first - second)));
1255inline unsigned long long absDistance(
const long long first,
const long long second)
1257#if defined(__clang__)
1258 return (first > second) ? (first - second) : (second - first);
1259#elif (defined(OCEAN_COMPILER_MSC) && OCEAN_MSC_VERSION <= 1500)
1260 return (first > second) ? (first - second) : (second - first);
1262 return ::abs(first - second);
1266inline unsigned long long absDistance(
const unsigned long long first,
const unsigned long long second)
1268#if defined(__clang__)
1269 return (first > second) ? (first - second) : (second - first);
1270#elif (defined(OCEAN_COMPILER_MSC) && OCEAN_MSC_VERSION <= 1500)
1271 return (first > second) ? (first - second) : (second - first);
1273 return ::abs((
long long)(first - second));
1277inline unsigned int mirrorValue(
const int value,
const unsigned int size)
1279 ocean_assert(size != 0u);
1280 ocean_assert(size <= 2147483647);
1281 ocean_assert(value >= -
int(size) && value <=
int(size * 2));
1284 const int result = (value < 0) ? (-value - 1) : (value >= int(size) ? (int(size + size) - value - 1) : value);
1285 ocean_assert(result >= 0 && result <
int(size));
1288 return (value < 0) ? (-value - 1) : (value >= int(size) ? (int(size + size) - value - 1) : value);
1291template <
typename T>
1296 for (
size_t n = 0; n < vectors.size(); ++n)
1298 size += vectors[n].size();
1304template <
typename T>
1305inline bool hasElement(
const std::vector<T>& elements,
const T& element)
1307 for (
const T& value : elements)
1309 if (value == element)
1318template <
typename T>
1319inline std::vector<T>
concatenate(
const std::vector<std::vector<T>>& vectors)
1323 std::vector<T> result;
1324 result.reserve(size);
1326 for (
size_t n = 0; n < vectors.size(); ++n)
1328 result.insert(result.end(), vectors[n].begin(), vectors[n].end());
1334template <
typename T>
1335inline void createIndices(
const size_t numberIndices,
const T& startIndex, std::vector<T>& indices)
1337 ocean_assert(numberIndices > 0);
1339 indices.reserve(numberIndices);
1342 T index = startIndex;
1344 for (
size_t n = 0; n < numberIndices; ++n)
1346 indices.emplace_back(index++);
1350template <
typename T>
1351inline std::vector<T>
createIndices(
const size_t numberIndices,
const T& startIndex)
1353 std::vector<T> result;
1361 uint64_t leftProductHigh, leftProductLow;
1362 multiply64(leftFactor1, leftFactor2, leftProductHigh, leftProductLow);
1364 uint64_t rightProductHigh, rightProductLow;
1365 multiply64(rightFactor1, rightFactor2, rightProductHigh, rightProductLow);
1367 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:895
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:655
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:955
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 void sortHighestToFront4(T &value0, T &value1, T &value2, T &value3)
Sorts four values so that the highest value will finally be the first value.
Definition base/Utilities.h:758
static bool isPowerOfTwo(const unsigned int value)
Returns whether a given value is a power of two.
Definition base/Utilities.h:932
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:856
static T divideBy2(const T &value)
This function divides a given value by two.
Definition base/Utilities.h:907
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:1359
static unsigned char divideBy255(const unsigned int value)
This function applies a fast division by 255 for unsigned integer values.
Definition base/Utilities.h:924
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:829
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:666
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:736
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:883
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:725
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:973
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition base/Utilities.h:1159
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:1319
unsigned char absDistance(const char first, const char second)
Returns the absolute distance between two values.
Definition base/Utilities.h:1225
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:1335
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:994
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:1292
T ringDistance(const T &value0, const T &value1, const T &ring)
Returns the distance between two values inside defined ring.
Definition base/Utilities.h:1088
unsigned int sqr(const char value)
Returns the square value of a given value.
Definition base/Utilities.h:1099
static unsigned int mirrorValue(const int value, const unsigned int size)
Mirrors a given value if necessary.
Definition base/Utilities.h:1277
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:1305
Helper struct allowing to determine a hash value for a std::pair.
Definition base/Utilities.h:636
std::size_t operator()(const std::pair< T1, T2 > &pair) const
Returns the hash value for a std::pair.
Definition base/Utilities.h:645