34 namespace OVR {
namespace Alg {
41 { T temp(a); a = b; b = temp; }
47 {
return (a < b) ? a : b; }
50 {
return (b < a) ? a : b; }
53 {
return Max<T>(minVal, Min<T>(v, maxVal)); }
59 {
return (b - a) * f + a; }
70 return (a < b) ? a : b;
75 return (b < a) ? a : b;
80 {
return (v>=0) ? v : -v; }
88 static bool Compare(
const T& a,
const T& b)
101 template<
class Array,
class Less>
109 if(end - start < 2)
return;
118 SPInt len = limit - base;
124 pivot = base + len / 2;
125 Swap(arr[base], arr[pivot]);
131 if(less(arr[j], arr[i]))
Swap(arr[j], arr[i]);
132 if(less(arr[base], arr[i]))
Swap(arr[base], arr[i]);
133 if(less(arr[j], arr[base]))
Swap(arr[j], arr[base]);
137 do i++;
while( less(arr[i], arr[base]) );
138 do j--;
while( less(arr[base], arr[j]) );
145 Swap(arr[i], arr[j]);
148 Swap(arr[base], arr[j]);
151 if(j - base > limit - i)
171 for(; i < limit; j = i, i++)
173 for(; less(arr[j + 1], arr[j]); j--)
175 Swap(arr[j + 1], arr[j]);
203 template<
class Array>
212 template<
class Array,
class Less>
220 if(end - start < 2)
return true;
229 SPInt len = limit - base;
235 pivot = base + len / 2;
236 Swap(arr[base], arr[pivot]);
242 if(less(arr[j], arr[i]))
Swap(arr[j], arr[i]);
243 if(less(arr[base], arr[i]))
Swap(arr[base], arr[i]);
244 if(less(arr[j], arr[base]))
Swap(arr[j], arr[base]);
253 }
while( less(arr[i], arr[base]) );
259 }
while( less(arr[base], arr[j]) );
266 Swap(arr[i], arr[j]);
269 Swap(arr[base], arr[j]);
272 if(j - base > limit - i)
292 for(; i < limit; j = i, i++)
294 for(; less(arr[j + 1], arr[j]); j--)
296 Swap(arr[j + 1], arr[j]);
318 template<
class Array>
331 template<
class Array,
class Less>
338 template<
class Array,
class Less>
351 template<
class Array>
358 template<
class Array>
376 template<
class Array,
class Less>
383 for(; i < limit; j = i, i++)
385 for(; less(arr[j + 1], arr[j]); j--)
387 Swap(arr[j + 1], arr[j]);
403 template<
class Array>
417 template<
class Array,
class Less>
429 template<
class Array>
442 template<
class Array>
446 UPInt mid = (count - 1) / 2;
449 for (
UPInt j = 0; j <= mid; j++)
452 for (
UPInt k = j + 1; k < count; k++)
453 if (arr[k] < arr[min])
455 Swap(arr[j], arr[min]);
463 template<
class Array,
class Value,
class Less>
474 middle = first + half;
475 if(less(arr[middle], val))
478 len = len - half - 1;
492 template<
class Array,
class Value>
501 template<
class Array,
class Value>
510 template<
class Array,
class Value,
class Less>
520 template<
class Array,
class Value>
531 template<
class Array,
class Value,
class Less>
542 middle = first + half;
543 if(less(val, arr[middle]))
550 len = len - half - 1;
560 template<
class Array,
class Value>
570 template<
class Array,
class Value>
580 template<
class Array,
class Value,
class Less>
590 template<
class Array,
class Value>
606 Swap(arr[from], arr[to]);
615 template<
class CDst,
class CSrc>
619 for(i = 0; i < src.GetSize(); i++)
620 dst.PushBack(src[i]);
672 #ifndef OVR_64BIT_POINTERS
674 if (val & 0xFFFF0000)
676 return (val & 0xFF000000) ?
680 return (val & 0xFF00) ?
686 if (val & 0xFFFFFFFF00000000)
688 if (val & 0xFFFF000000000000)
690 return (val & 0xFF00000000000000) ?
694 return (val & 0xFF0000000000) ?
700 if (val & 0xFFFF0000)
702 return (val & 0xFF000000) ?
706 return (val & 0xFF00) ?
717 #ifndef OVR_64BIT_POINTERS
721 return (val & 0xFF) ?
725 return (val & 0xFF0000) ?
731 if (val & 0xFFFFFFFF)
735 return (val & 0xFF) ?
739 return (val & 0xFF0000) ?
745 if (val & 0xFFFF00000000)
747 return (val & 0xFF00000000) ?
751 return (val & 0xFF000000000000) ?
768 static int Cmp (
const void* p1,
const void* p2,
UPInt byteCount) {
return memcmp(p1, p2, byteCount); }
769 static int Cmp16(
const void* p1,
const void* p2,
UPInt int16Count);
770 static int Cmp32(
const void* p1,
const void* p2,
UPInt int32Count);
771 static int Cmp64(
const void* p1,
const void* p2,
UPInt int64Count);
783 while (pa[ic] == pb[ic])
784 if (++ic==int16Count)
786 return pa[ic] > pb[ic] ? 1 : -1;
795 while (pa[ic] == pb[ic])
796 if (++ic==int32Count)
798 return pa[ic] > pb[ic] ? 1 : -1;
807 while (pa[ic] == pb[ic])
808 if (++ic==int64Count)
810 return pa[ic] > pb[ic] ? 1 : -1;
827 for (
int i = 0; i < size>>1; i++)
830 pb[size-1-i] = pb[i];
845 ((v&
UInt64(0x00FF000000000000ULL))>>40) |
846 ((v&
UInt64(0x0000FF0000000000ULL))>>24) |
847 ((v&
UInt64(0x000000FF00000000ULL))>>8) |
848 ((v&
UInt64(0x00000000FF000000ULL))<<8) |
849 ((v&
UInt64(0x0000000000FF0000ULL))<<24) |
850 ((v&
UInt64(0x000000000000FF00ULL))<<40) |
878 #if (OVR_BYTE_ORDER == OVR_LITTLE_ENDIAN)
927 #elif (OVR_BYTE_ORDER == OVR_BIG_ENDIAN)
949 inline float BEToSystem(
float v) {
return v; }
950 inline double BEToSystem(
double v) {
return v; }
973 inline float SystemToBE(
float v) {
return v; }
974 inline double SystemToBE(
double v) {
return v; }
977 #error "OVR_BYTE_ORDER must be defined to OVR_LITTLE_ENDIAN or OVR_BIG_ENDIAN"
1051 UByte digit1 = (byte >> 4) & 0x0f;
1052 UByte digit2 = byte & 0x0f;
1053 int decimal = digit1 * 10 + digit2;
1054 return (
SByte)decimal;
void EncodeFloat(UByte *buffer, float val)
const UByte LowerBitTable[256]
const T & operator[](UPInt i) const
void InsertionSortSliced(Array &arr, UPInt start, UPInt end, Less less)
UByte SystemToBE(UByte v)
void EncodeUInt32(UByte *buffer, UInt32 val)
bool QuickSortSlicedSafe(Array &arr, UPInt start, UPInt end, Less less)
float DecodeFloat(const UByte *buffer)
OVR_FORCE_INLINE const T Max(const T a, const T b)
SInt32 DecodeSInt32(const UByte *buffer)
void EncodeUInt16(UByte *buffer, UInt16 val)
UPInt LowerBoundSliced(const Array &arr, UPInt start, UPInt end, const Value &val, Less less)
UByte BEToSystem(UByte v)
UInt32 DecodeUInt32(const UByte *buffer)
UPInt UpperBoundSized(const Array &arr, UPInt size, const Value &val)
UPInt UpperBound(const Array &arr, const Value &val, Less less)
OVR_FORCE_INLINE void Swap(T &a, T &b)
UInt16 DecodeUInt16(const UByte *buffer)
ConstArrayAdaptor(const T *ptr, UPInt size)
static bool Compare(const T &a, const T &b)
static int Cmp64(const void *p1, const void *p2, UPInt int64Count)
const T & operator[](UPInt i) const
OVR_FORCE_INLINE T Lerp(T a, T b, T f)
int memcmp(const void *__s1, const void *__s2, size_t __n) __THROW __attribute_pure__ __nonnull((1
const UByte UpperBitTable[256]
UByte UpperBit(UPInt val)
UByte LEToSystem(UByte v)
UPInt UpperBoundSliced(const Array &arr, UPInt start, UPInt end, const Value &val, Less less)
__END_DECLS typedef int8_t SByte
OVR_FORCE_INLINE const T PMin(const T a, const T b)
void InsertionSort(Array &arr, Less less)
UByte LowerBit(UPInt val)
OVR_FORCE_INLINE const T Clamp(const T v, const T minVal, const T maxVal)
void AppendArray(CDst &dst, const CSrc &src)
void SwapOrder(void *pv, int size)
SByte DecodeBCD(UByte byte)
bool QuickSortSafe(Array &arr, Less less)
OVR_FORCE_INLINE const T PMax(const T a, const T b)
Array::ValueType & Median(Array &arr)
UByte SystemToLE(UByte v)
static int Cmp32(const void *p1, const void *p2, UPInt int32Count)
static int Cmp(const void *p1, const void *p2, UPInt byteCount)
void EncodeSInt16(UByte *buffer, SInt16 val)
OVR_FORCE_INLINE const T Min(const T a, const T b)
UPInt LowerBound(const Array &arr, const Value &val, Less less)
ArrayAdaptor(T *ptr, UPInt size)
UPInt LowerBoundSized(const Array &arr, UPInt size, const Value &val)
SInt16 DecodeSInt16(const UByte *buffer)
static int Cmp16(const void *p1, const void *p2, UPInt int16Count)
OVR_FORCE_INLINE int Chop(T f)
void EncodeSInt32(UByte *buffer, SInt32 val)
OVR_FORCE_INLINE const T Abs(const T v)
void ReverseArray(Array &arr)
#define OVR_COMPILER_ASSERT(x)
void QuickSortSliced(Array &arr, UPInt start, UPInt end, Less less)
void QuickSort(Array &arr, Less less)