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);
263 template <
typename T>
264 inline T
minmax(
const T& lowerBoundary,
const T& value,
const T& upperBoundary);
279 template <
typename T>
280 inline T
modulo(
const T& value,
const T& ring);
291 template <
typename T>
292 inline T
ringDistance(
const T& value0,
const T& value1,
const T& ring);
300 inline unsigned int sqr(
const char value);
308 inline unsigned int sqr(
const unsigned char value);
316 inline unsigned int sqr(
const short value);
324 inline unsigned int sqr(
const unsigned short value);
332 inline unsigned int sqr(
const int value);
340 inline unsigned int sqr(
const unsigned int value);
348 inline unsigned long sqr(
const long value);
356 inline unsigned long sqr(
const unsigned long value);
364 inline unsigned long long sqr(
const long long value);
372 inline unsigned long long sqr(
const unsigned long long value);
380 inline float sqr(
const float value);
388 inline double sqr(
const double value);
397 inline unsigned int sqrDistance(
const char first,
const char second);
406 inline unsigned int sqrDistance(
const unsigned char first,
const unsigned char second);
415 inline unsigned int sqrDistance(
const short first,
const short second);
424 inline unsigned int sqrDistance(
const unsigned short first,
const unsigned short second);
433 inline unsigned int sqrDistance(
const int first,
const int second);
442 inline unsigned int sqrDistance(
const unsigned int first,
const unsigned int second);
451 inline unsigned long long sqrDistance(
const long long first,
const long long second);
460 inline unsigned long long sqrDistance(
const unsigned long long first,
const unsigned long long second);
469 inline float sqrDistance(
const float first,
const float second);
478 inline double sqrDistance(
const double first,
const double second);
487 inline unsigned char absDistance(
const char first,
const char second);
496 inline unsigned char absDistance(
const unsigned char first,
const unsigned char second);
505 inline unsigned short absDistance(
const short first,
const short second);
514 inline unsigned short absDistance(
const unsigned short first,
const unsigned short second);
523 inline unsigned int absDistance(
const int first,
const int second);
532 inline unsigned int absDistance(
const unsigned int first,
const unsigned int second);
541 inline unsigned long long absDistance(
const long long first,
const long long second);
550 inline unsigned long long absDistance(
const unsigned long long first,
const unsigned long long second);
565 static inline unsigned int mirrorValue(
const int value,
const unsigned int size);
574 template <
typename T>
575 static inline size_t countElements(
const std::vector<std::vector<T>>& vectors);
583 template <
typename T>
584 static inline bool hasElement(
const std::vector<T>& elements,
const T& element);
593 template <
typename T>
594 static inline std::vector<T>
concatenate(
const std::vector<std::vector<T>>& vectors);
604 template <
typename T>
605 static inline void createIndices(
const size_t numberIndices,
const T& startIndex, std::vector<T>& indices);
615 template <
typename T>
616 [[nodiscard]]
static inline std::vector<T>
createIndices(
const size_t numberIndices,
const T& startIndex);
618 template <
typename T>
621 if (!(value0 < value1))
623 std::swap(value0, value1);
626 ocean_assert(value0 <= value1);
629 template <
typename T>
632 if (!(value0 < value1))
634 std::swap(value0, value1);
637 if (!(value0 < value2))
639 std::swap(value0, value2);
642 if (!(value1 < value2))
644 std::swap(value1, value2);
647 ocean_assert(value0 <= value1);
648 ocean_assert(value1 <= value2);
651 template <
typename T,
typename TData>
654 if (!(value0 < value1))
656 std::swap(value0, value1);
657 std::swap(data0, data1);
660 ocean_assert(value0 <= value1);
663 template <
typename T,
typename TData>
666 if (!(value0 < value1))
668 std::swap(value0, value1);
669 std::swap(data0, data1);
672 if (!(value0 < value2))
674 std::swap(value0, value2);
675 std::swap(data0, data2);
678 if (!(value1 < value2))
680 std::swap(value1, value2);
681 std::swap(data1, data2);
684 ocean_assert(value0 <= value1);
685 ocean_assert(value1 <= value2);
688 template <
typename T>
693 std::swap(value0, value1);
696 ocean_assert(value0 >= value1);
699 template <
typename T>
704 std::swap(value0, value1);
709 std::swap(value0, value2);
714 std::swap(value1, value2);
717 ocean_assert(value0 >= value1);
718 ocean_assert(value1 >= value2);
721 template <
typename T,
typename TData>
726 std::swap(value0, value1);
727 std::swap(data0, data1);
730 ocean_assert(value0 >= value1);
733 template <
typename T,
typename TData>
738 std::swap(value0, value1);
739 std::swap(data0, data1);
744 std::swap(value0, value2);
745 std::swap(data0, data2);
750 std::swap(value1, value2);
751 std::swap(data1, data2);
754 ocean_assert(value0 >= value1);
755 ocean_assert(value1 >= value2);
758 template <
typename T>
761 if (value0 <= value1)
763 if (value0 <= value2)
774 if (value1 <= value2)
785 template <
typename T>
788 if (value0 >= value1)
790 if (value0 >= value2)
801 if (value1 >= value2)
812 template <
typename T1,
typename T2>
815 return firstPair.first < secondPair.first;
818 template <
typename T>
821 return firstPair.first < secondPair.first;
824 template <
typename T1,
typename T2>
827 return firstPair.second < secondPair.second;
830 template <
typename T>
833 return firstPair.second < secondPair.second;
836 template <
typename T>
856 ocean_assert(value < 65535u);
857 ocean_assert(value / 255u == (value + 1u + (value >> 8u)) >> 8u);
859 return (
unsigned char)((value + 1u + (value >> 8u)) >> 8u);
875 ocean_assert((value == 1u || value == 2u || value == 4u || value == 8u || value == 16u || value == 32u || value == 64u
876 || value == 128u || value == 256u || value == 512u || value == 1024u || value == 2048u
877 || value == 4096u || value == 8192 || value == 16384u || value == 32768u || value == 65536u
878 || value == 131072 || value == 262144u || value == 524288u || value == 1048576u || value == 2097152u
879 || value == 4194304u || value == 8388608u || value == 16777216u || value == 33554432u || value == 67108864u
880 || value == 134217728u || value == 268435456u || value == 536870912u || value == 1073741824u || value == 2147483648u) == (value != 0u && (value & (value - 1u)) == 0u));
882 return value != 0u && (value & (value - 1u)) == 0u;
887 ocean_assert(value <= 0x80000000u);
889 unsigned int powerOfTwoValue = 0x80000000u;
891 while (powerOfTwoValue != 0u && (powerOfTwoValue >> 1) >= value)
893 powerOfTwoValue >>= 1u;
896 return powerOfTwoValue;
902 template <
typename T>
903 inline T
minmax(
const T& lowerBoundary,
const T& value,
const T& upperBoundary)
905 ocean_assert(lowerBoundary <= upperBoundary);
907 ocean_assert((value < lowerBoundary ? lowerBoundary : (value > upperBoundary ? upperBoundary : value)) == (max(lowerBoundary, min(value, upperBoundary))));
908 return value < lowerBoundary ? lowerBoundary : (value > upperBoundary ? upperBoundary : value);
914 template <
typename T>
915 inline T
minmax(
const T& lowerBoundary,
const T& value,
const T& upperBoundary)
917 ocean_assert(lowerBoundary <= upperBoundary);
918 return max(lowerBoundary, min(upperBoundary, value));
923 template <
typename T>
924 inline T
modulo(
const T& value,
const T& ring)
926 ocean_assert(ring > 0);
935 ocean_assert(value % ring < ring);
940 const T result = (ring - 1) - (-(value + 1) % ring);
941 ocean_assert(result >= 0 && result < ring);
948 inline unsigned char modulo(
const unsigned char& value,
const unsigned char& ring)
954 inline unsigned short modulo(
const unsigned short& value,
const unsigned short& ring)
960 inline unsigned int modulo(
const unsigned int& value,
const unsigned int& ring)
966 inline unsigned long long modulo(
const unsigned long long& value,
const unsigned long long& ring)
972 inline double modulo(
const double& value,
const double& ring)
974 ocean_assert(ring > 0);
983 return fmod(value, ring);
987 const double intermediate = ring - (fmod(-value, ring));
988 ocean_assert(intermediate >= 0.0 && intermediate <= ring);
990 return intermediate == ring ? 0.0 : intermediate;
995 inline float modulo(
const float& value,
const float& ring)
997 ocean_assert(ring > 0);
1006 return fmod(value, ring);
1010 const float intermediate = ring - (fmod(-value, ring));
1011 ocean_assert(intermediate >= 0.0f && intermediate <= ring);
1013 return intermediate == ring ? 0.0f : intermediate;
1017 template <
typename T>
1020 ocean_assert(value0 >= 0 && value0 < ring);
1021 ocean_assert(value1 >= 0 && value1 < ring);
1023 const T low = min(value0, value1);
1024 const T high = max(value0, value1);
1026 return min(high - low, low + ring - high);
1029 inline unsigned int sqr(
const char value)
1031 return value * value;
1034 inline unsigned int sqr(
const unsigned char value)
1036 return value * value;
1039 inline unsigned int sqr(
const short value)
1041 return value * value;
1044 inline unsigned int sqr(
const unsigned short value)
1046 return value * value;
1049 inline unsigned int sqr(
const int value)
1051 return value * value;
1054 inline unsigned int sqr(
const unsigned int value)
1056 return value * value;
1059 inline unsigned long sqr(
const long value)
1061 return value * value;
1064 inline unsigned long sqr(
const unsigned long value)
1066 return value * value;
1069 inline unsigned long long sqr(
const long long value)
1071 return value * value;
1074 inline unsigned long long sqr(
const unsigned long long value)
1076 return value * value;
1079 inline float sqr(
const float value)
1081 return value * value;
1084 inline double sqr(
const double value)
1086 return value * value;
1091 const int distance = int(first) - int(second);
1092 return distance * distance;
1095 inline unsigned int sqrDistance(
const unsigned char first,
const unsigned char second)
1097 const int distance = int(first) - int(second);
1098 return distance * distance;
1101 inline unsigned int sqrDistance(
const short first,
const short second)
1103 const int distance = int(first) - int(second);
1104 return distance * distance;
1107 inline unsigned int sqrDistance(
const unsigned short first,
const unsigned short second)
1109 const int distance = int(first) - int(second);
1110 return distance * distance;
1115 const int distance = first - second;
1116 return distance * distance;
1119 inline unsigned int sqrDistance(
const unsigned int first,
const unsigned int second)
1121 ocean_assert(first <= (
unsigned int)INT_MAX);
1122 ocean_assert(second <= (
unsigned int)INT_MAX);
1124 const int distance = int(first) - int(second);
1125 return distance * distance;
1128 inline unsigned long long sqrDistance(
const long long first,
const long long second)
1130 const long long distance = first - second;
1131 return distance * distance;
1134 inline unsigned long long sqrDistance(
const unsigned long long first,
const unsigned long long second)
1136 ocean_assert(first <= (
unsigned long long)LLONG_MAX);
1137 ocean_assert(second <= (
unsigned long long)LLONG_MAX);
1139 const long long distance = (
long long)first - (
long long)second;
1140 return distance * distance;
1145 const float distance = first - second;
1146 return distance * distance;
1151 const double distance = first - second;
1152 return distance * distance;
1157 return (
unsigned char)(::abs(
int(first) -
int(second)));
1160 inline unsigned char absDistance(
const unsigned char first,
const unsigned char second)
1162 return (
unsigned char)(::abs(
int(first - second)));
1165 inline unsigned short absDistance(
const short first,
const short second)
1167 return (
unsigned short)(::abs(
int(first) -
int(second)));
1170 inline unsigned short absDistance(
const unsigned short first,
const unsigned short second)
1172 return (
unsigned short)(::abs(
int(first - second)));
1177 return (
unsigned int)(::abs(first - second));
1180 inline unsigned int absDistance(
const unsigned int first,
const unsigned int second)
1182 return (
unsigned int)(::abs(
int(first - second)));
1185 inline unsigned long long absDistance(
const long long first,
const long long second)
1187 #if defined(__clang__)
1188 return (first > second) ? (first - second) : (second - first);
1189 #elif (defined(OCEAN_COMPILER_MSC) && OCEAN_MSC_VERSION <= 1500)
1190 return (first > second) ? (first - second) : (second - first);
1192 return ::abs(first - second);
1196 inline unsigned long long absDistance(
const unsigned long long first,
const unsigned long long second)
1198 #if defined(__clang__)
1199 return (first > second) ? (first - second) : (second - first);
1200 #elif (defined(OCEAN_COMPILER_MSC) && OCEAN_MSC_VERSION <= 1500)
1201 return (first > second) ? (first - second) : (second - first);
1203 return ::abs((
long long)(first - second));
1207 inline unsigned int mirrorValue(
const int value,
const unsigned int size)
1209 ocean_assert(size != 0u);
1210 ocean_assert(size <= 2147483647);
1211 ocean_assert(value >= -
int(size) && value <=
int(size * 2));
1214 const int result = (value < 0) ? (-value - 1) : (value >= int(size) ? (int(size + size) - value - 1) : value);
1215 ocean_assert(result >= 0 && result <
int(size));
1218 return (value < 0) ? (-value - 1) : (value >=
int(size) ? (int(size + size) - value - 1) : value);
1221 template <
typename T>
1226 for (
size_t n = 0; n < vectors.size(); ++n)
1228 size += vectors[n].size();
1234 template <
typename T>
1235 inline bool hasElement(
const std::vector<T>& elements,
const T& element)
1237 for (
const T& value : elements)
1239 if (value == element)
1248 template <
typename T>
1249 inline std::vector<T>
concatenate(
const std::vector<std::vector<T>>& vectors)
1253 std::vector<T> result;
1254 result.reserve(size);
1256 for (
size_t n = 0; n < vectors.size(); ++n)
1258 result.insert(result.end(), vectors[n].begin(), vectors[n].end());
1264 template <
typename T>
1265 inline void createIndices(
const size_t numberIndices,
const T& startIndex, std::vector<T>& indices)
1267 ocean_assert(numberIndices > 0);
1269 indices.reserve(numberIndices);
1272 T index = startIndex;
1274 for (
size_t n = 0; n < numberIndices; ++n)
1276 indices.emplace_back(index++);
1280 template <
typename T>
1281 inline std::vector<T>
createIndices(
const size_t numberIndices,
const T& startIndex)
1283 std::vector<T> result;
1291 uint64_t leftProductHigh, leftProductLow;
1292 multiply64(leftFactor1, leftFactor2, leftProductHigh, leftProductLow);
1294 uint64_t rightProductHigh, rightProductLow;
1295 multiply64(rightFactor1, rightFactor2, rightProductHigh, rightProductLow);
1297 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:825
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:619
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:885
static bool isPowerOfTwo(const unsigned int value)
Returns whether a given value is a power of two.
Definition: base/Utilities.h:862
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:786
static T divideBy2(const T &value)
This function divides a given value by two.
Definition: base/Utilities.h:837
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:1289
static unsigned char divideBy255(const unsigned int value)
This function applies a fast division by 255 for unsigned integer values.
Definition: base/Utilities.h:854
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:759
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:630
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:700
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:813
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 sortHighestToFront2(T &value0, T &value1)
Sorts two values so that the highest value will finally be the first value.
Definition: base/Utilities.h:689
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:903
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition: base/Utilities.h:1089
unsigned char absDistance(const char first, const char second)
Returns the absolute distance between two values.
Definition: base/Utilities.h:1155
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:1265
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:924
T ringDistance(const T &value0, const T &value1, const T &ring)
Returns the distance between two values inside defined ring.
Definition: base/Utilities.h:1018
unsigned int sqr(const char value)
Returns the square value of a given value.
Definition: base/Utilities.h:1029
static unsigned int mirrorValue(const int value, const unsigned int size)
Mirrors a given value if necessary.
Definition: base/Utilities.h:1207
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:1249
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:1222
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:1235