5 #include <libutl/Ordering.h> 59 template <
typename T,
typename... R>
61 min(
const T& a,
const T& b,
const R&... args)
63 return utl::min((b < a) ? b : a, args...);
87 template <
typename T,
typename... R>
89 max(
const T& a,
const T& b,
const R&... args)
91 return utl::max((b < a) ? a : b, args...);
146 return (rem == 0) ? target : target + (x - rem);
162 return (target + x - 1) & ~(x - 1);
178 return (target + x - 1) & ~(x - 1);
193 if (
object ==
nullptr)
return 0;
194 return object->allocatedSize();
209 template <
typename T>
215 const T* defVal =
nullptr)
218 if (minSize ==
size_t_max) minSize = (arraySize + 1);
221 if (arraySize >= minSize)
return;
237 T* newArray =
new T[newSize];
238 if (std::is_trivially_copyable<T>())
240 memcpy(newArray, array, arraySize *
sizeof(T));
244 T* lhsPtr = newArray;
246 T* rhsLim = array + arraySize;
247 for (; rhsPtr != rhsLim; ++lhsPtr, ++rhsPtr)
249 *lhsPtr = std::move(*rhsPtr);
254 if (defVal !=
nullptr)
256 T* lhsPtr = newArray + arraySize;
257 T* lhsLim = newArray + newSize;
258 for (; lhsPtr != lhsLim; ++lhsPtr)
282 template <
typename T>
290 size_t arraySize = arrayLim - array;
291 size_t arrayIdx = arrayPtr - array;
292 arrayGrow(array, arraySize, minSize, growthIncrement, static_cast<T*>(
nullptr));
293 arrayPtr = array + arrayIdx;
294 arrayLim = array + arraySize;
305 template <
typename T>
310 return static_cast<const T&
>(object);
321 template <
typename T>
326 return static_cast<T&
>(object);
337 template <
typename T>
341 ASSERTD((
object ==
nullptr) || object->isA(T));
342 return static_cast<const T*
>(object);
353 template <
typename T>
357 ASSERTD((
object ==
nullptr) || object->isA(T));
358 return static_cast<T*
>(object);
369 template <
typename T>
374 return static_cast<T&
>(
const_cast<utl::Object&
>(object));
385 template <
typename T>
389 ASSERTD((
object ==
nullptr) || object->isA(T));
390 return static_cast<T*
>(
const_cast<utl::Object*
>(object));
402 template <
typename T>
406 if (
object ==
nullptr)
return nullptr;
407 return object->clone();
420 template <
typename T>
424 if (lhs < rhs)
return -1;
425 if (lhs == rhs)
return 0;
446 if (lhs == rhs)
return 0;
447 if (ordering ==
nullptr)
return lhs->
compare(*rhs);
448 return ordering->cmp(lhs, rhs);
468 if (lhs == rhs)
return 0;
469 if ((lhs !=
nullptr) && (rhs !=
nullptr))
471 if (ordering ==
nullptr)
return lhs->
compare(*rhs);
472 return ordering->cmp(lhs, rhs);
474 if (lhs ==
nullptr)
return -1;
490 template <
typename T>
492 compare(
const T* lhs,
const T* rhs,
size_t len)
495 for (i = 0; i < len; i++)
518 template <
typename T>
520 compare(
const T* lhs,
size_t lhsLen,
const T* rhs,
size_t rhsLen)
523 size_t minLen =
utl::min(lhsLen, rhsLen);
524 for (i = 0; i < minLen; i++)
534 else if (rhsLen < minLen)
552 template <
typename InputCont1,
typename InputCont2>
554 compare(
typename InputCont1::const_iterator lhsBegin,
555 typename InputCont1::const_iterator lhsEnd,
556 typename InputCont2::const_iterator rhsBegin,
557 typename InputCont2::const_iterator rhsEnd,
558 std::function<
int(
typename InputCont1::value_type,
typename InputCont2::value_type)> comp)
560 auto lhsIt = lhsBegin;
561 auto rhsIt = rhsBegin;
562 for (;; ++lhsIt, ++rhsIt)
564 bool lhsDone = (lhsIt == lhsEnd);
565 bool rhsDone = (rhsIt == rhsEnd);
566 if (lhsDone && rhsDone)
return 0;
567 if (lhsDone)
return -1;
568 if (rhsDone)
return 1;
569 int res = comp(*lhsIt, *rhsIt);
570 if (res != 0)
return res;
609 template <
typename T>
613 : _ordering(ordering)
617 int operator()(
const Object* lhs,
const Object* rhs)
const 635 template <
typename T = utl::Object>
639 : _ordering(ordering)
643 bool operator()(
const T* lhs,
const T* rhs)
const 661 template <
typename T = utl::Object>
665 : _ordering(ordering)
669 bool operator()(
const T* lhs,
const T* rhs)
const 688 template <
typename T>
690 copy(T* dest,
const T* src,
size_t len)
693 const T* srcPtr = src;
694 const T* srcLim = src + len;
695 for (; srcPtr != srcLim; ++srcPtr, ++destPtr)
713 return (((x / n) * n) == x);
726 template <
typename T>
731 if (vModm == 0)
return v;
732 return (v - vModm + m);
745 template <
typename T>
764 template <
typename T>
770 for (i = 0; i < len; i++)
787 template <
typename T>
805 template <
typename T>
809 object.serialize(stream, io, mode);
823 template <
typename T>
831 Object* _object = Object::serializeInNullable(stream, mode);
832 object = utl::cast<T>(_object);
836 Object::serializeOutNullable(
object, stream, mode);
851 template <
typename T>
859 Object* _object = Object::serializeInBoxed(stream, mode);
860 object = utl::cast<T>(_object);
864 object->serializeOutBoxed(stream, mode);
880 return ((n & 0xff00) >> 8) | ((n & 0x00ff) << 8);
895 n = ((n & 0xffff0000) >> 16) | ((n & 0x0000ffff) << 16);
896 n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8);
912 n = ((n & 0xffffffff00000000) >> 32) | ((n & 0x00000000ffffffff) << 32);
913 n = ((n & 0xffff0000ffff0000) >> 16) | ((n & 0x0000ffff0000ffff) << 16);
914 n = ((n & 0xff00ff00ff00ff00) >> 8) | ((n & 0x00ff00ff00ff00ff) << 8);
928 template <
typename T>
930 swap(T* array,
size_t lhsIdx,
size_t rhsIdx)
932 T tmp = std::move(array[lhsIdx]);
933 array[lhsIdx] = std::move(array[rhsIdx]);
934 array[rhsIdx] = std::move(tmp);
948 template <
typename T>
950 swap(T* array,
size_t lhsIdx,
size_t rhsIdx,
size_t size)
952 T* pi = array + lhsIdx;
953 T* pj = array + rhsIdx;
954 T* piLim = array + lhsIdx + size;
956 for (; pi != piLim; ++pi, ++pj)
958 tmp = std::move(*pi);
959 *pi = std::move(*pj);
960 *pj = std::move(tmp);
977 asm volatile(
"" : : :
"memory");
995 asm volatile(
"" : :
"g"(ptr) :
"memory");
1010 return n * (size_t)1024;
1025 return n * (size_t)1024 * (
size_t)1024;
1040 return n * (size_t)1024 * (
size_t)1024 * (size_t)1024;
void serializeBoxed(T *&object, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boxed object.
size_t allocatedSize(const Object *object)
Compute the allocated size of an object.
T * clone(const T *object)
Create a clone of the given object.
T roundDown(const T &v, const T &m)
Round the given value v down to the nearest multiple of m.
constexpr size_t MB(size_t n)
Convert size in megabytes to size in bytes.
Object comparison abstraction.
bool comparisonResultMatchesOp(int cmpRes, uint_t cmpOp)
Determine whether the given comparison result is consistent with the given comparison operator...
const T & max(const T &a, const T &b, const R &... args)
Return the largest value among two or more provided values of the same type.
default representation (via getSerializeMode())
T * cast(utl::Object *object)
Safely cast an object pointer to the desired type.
const T & max(const T &v)
Base case for utl::max() variadic template.
uint64_t nextPow2(uint64_t n)
Return the smallest power of 2 which is >= n.
Object comparison evaluator.
Toggle a boolean value RAII-style.
const size_t size_t_max
Maximum size_t value.
unsigned int uint32_t
Unsigned 32-bit integer.
int compare(typename InputCont1::const_iterator lhsBegin, typename InputCont1::const_iterator lhsEnd, typename InputCont2::const_iterator rhsBegin, typename InputCont2::const_iterator rhsEnd, std::function< int(typename InputCont1::value_type, typename InputCont2::value_type)> comp)
Compare two sequences.
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
void serialize(T &object, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize an object (non-boxed).
bool isDivisibleBy(const T &x, const T &n)
Determine whether x is evenly divisible by n.
unsigned long uint64_t
Unsigned 64-bit integer.
constexpr size_t GB(size_t n)
Convert size in gigabytes to size in bytes.
void swap(T *array, size_t lhsIdx, size_t rhsIdx, size_t size)
Swap two sections of the given array.
void arrayGrow(T *&array, T *&arrayPtr, T *&arrayLim, size_t minSize=size_t_max, size_t growthIncrement=size_t_max)
Grow an array.
int compareNullable(const Object *lhs, const Object *rhs, const Ordering *ordering=nullptr)
Compare two objects.
unsigned int uint_t
Unsigned integer.
#define ABORT()
Immediately terminates the program.
Object comparison predicate evaluator that returns the value of (lhs < rhs).
T * cast_const(const utl::Object *object)
Safely cast an object pointer to the desired type (remove const-qualifier).
unsigned short uint16_t
Unsigned 16-bit integer.
void escape(void *ptr)
Fake read/write access to all memory, and require the object at address ptr to exist.
#define isA(className)
Determine whether the class is derived from the given class.
const T & min(const T &a, const T &b, const R &... args)
Return the smallest value among two or more provided values of the same type.
void serializeNullable(T *&object, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a nullable object.
virtual int compare(const Object &rhs) const
Compare with another object.
constexpr size_t KB(size_t n)
Convert size in kilobytes to size in bytes.
uint64_t reverseBytes(uint64_t n)
Reverse the byte ordering of a 64-bit unsigned integer.
uint64_t nextMultipleOfPow2(uint64_t x, uint64_t target)
Return the smallest number n s.t.
uint_t getSerializeMode()
Get the default serialization mode (see utl::serialize_t).
void clobber()
Fake read/write access to all memory.
Root of UTL++ class hierarchy.
int compare(bool lhs, bool rhs)
Compare two boolean values.
const T & min(const T &v)
Base case for utl::min() variadic template.
T roundUp(const T &v, const T &m)
Round the given value v up to the nearest multiple of m.
#define ASSERTD
Do an assertion in DEBUG mode only.
T nextMultiple(const T &x, const T &target)
Return the smallest number n s.t.
Object comparison predicate evaluator that returns the value of (lhs == rhs).
BoolToggle(bool &b)
Constructor.