libUTL++
Utility Functions

General-purpose functions. More...

Classes

class  utl::compareFn< T >
 Comparison functor. More...

Enumerations

enum  utl::cmp_t {
  utl::cmp_lessThan, utl::cmp_lessThanOrEqualTo, utl::cmp_equalTo, utl::cmp_greaterThan,
  utl::cmp_greaterThanOrEqualTo
}
 Comparison operators. More...
enum  utl::serialize_t { utl::ser_readable = 1, utl::ser_compact = 2, utl::ser_nonportable = 4, utl::ser_default = 8 }
 Specifies a serialization mode. More...
enum  utl::find_t { utl::find_any = 0, utl::find_first = 1, utl::find_last = 2, utl::find_ip = 3 }
 Find strategy. More...

Functions

void utl::init ()
 Initialize UTL++.
void utl::deInit ()
 De-initialize UTL++.
void utl::abort (const char *filename, int lineNo, const char *text=NULL)
 Abort the program - output a message to stderr explaining the reason for the failure, and the filename and line number where the abortion occurred.
void utl::abort (const char *text)
 Abort the program - output a message to stderr explaining the reason for the failure.
int utl::compare (bool lhs, bool rhs)
 Compare two boolean values.
void utl::die (const char *filename, int lineNo, const char *text=NULL, int exitCode=1)
 Abort the program - output a message to stderr explaining the reason for the failure, and the filename and line number where the failure occurred.
void utl::die (const char *text, int exitCode=1)
 Abort the program - output a message to stderr explaining the reason for the failure, and the filename and line number where the failure occurred.
uint_t utl::getSerializeMode ()
 Get the default serialization mode (see utl::serialize_t).
void utl::setSerializeMode (uint_t mode)
 Set the default serialization mode (see utl::serialize_t).
void * utl::realloc (void *ptr, size_t size, size_t newSize)
 Re-allocate the given block.
char * utl::strdup (const char *str)
 Duplicate the given string.
void utl::serialize (bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a boolean.
void utl::serialize (int8_t &i, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize an 8-bit integer.
void utl::serialize (char &c, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a character.
void utl::serialize (uint8_t &i, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize an 8-bit unsigned character.
void utl::serialize (int16_t &i, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a 16-bit integer.
void utl::serialize (uint16_t &i, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize an unsigned 16-bit integer.
void utl::serialize (int32_t &i, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a 32-bit integer.
void utl::serialize (uint32_t &i, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize an unsigned 32-bit integer.
void utl::serialize (long &i, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a long integer.
void utl::serialize (ulong_t &i, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize an unsigned long integer.
void utl::serialize (double &n, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a double-precision floating point number.
void utl::serialize (Object &object, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize an object.
size_t utl::allocatedSize (const Object *object)
 Create a clone of the given object.
template<typename T >
void utl::arrayGrow (T *&array, size_t &arraySize, size_t minSize=size_t_max, size_t maxGrowth=size_t_max, const T *defVal=NULL)
 Grow an array.
template<typename T >
void utl::arrayGrow (T *&array, T *&arrayPtr, T *&arrayLim, size_t minSize=size_t_max, size_t maxGrowth=size_t_max)
 Grow an array.
template<typename T >
const T & utl::min (const T &lhs, const T &rhs)
 Given two objects of the same type, determine the minimum.
template<typename T >
const T & utl::max (const T &lhs, const T &rhs)
 Given two objects of the same type, determine the maximum.
template<typename T >
T & utl::cast (const utl::Object &object)
 Safely cast an object to the desired type.
template<typename T >
T * utl::clone (const T *object)
 Create a clone of the given object.
template<typename T >
int utl::compare (const T &lhs, const T &rhs)
 Compare two objects of the same type.
template<typename T >
int utl::compare (const T *lhs, const T *rhs, size_t len)
 Compare two same-length arrays of objects of the same type.
template<typename T >
int utl::compare (const T *lhs, size_t lhsLen, const T *rhs, size_t rhsLen)
 Compare two arrays of objects of the same type.
bool utl::cmpPredicate (int cmpRes, uint_t cmpOp)
 Determine whether the given comparison result is consistent with the given comparison operator.
template<typename T >
void utl::copy (T *dest, const T *src, size_t len)
 Copy one array of objects to another.
template<typename T >
utl::roundUp (const T &v, const T &m)
 Round the given value v up to the nearest multiple of m.
template<typename T >
utl::roundDown (const T &v, const T &m)
 Round the given value v down to the nearest multiple of m.
template<typename T >
void utl::serialize (T *array, size_t len, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize an array.
template<typename T >
void utl::serialize (T *&object, Stream &stream, uint_t io, uint_t mode)
 Serialize an object (boxed serialization).
template<typename T >
void utl::serializeNullable (T *&p_object, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a nullable object.
template<typename T >
void utl::serializeBoxed (T *&p_object, Stream &stream, uint_t io, uint_t mode=ser_default)
 Serialize a boxed object.
int utl::compare (const Object *lhs, const Object *rhs, const Ordering *ordering=NULL)
 Compare two objects.
int utl::compareNullable (const Object *lhs, const Object *rhs, const Ordering *ordering=NULL)
 Compare two objects.
template<typename T >
void utl::swap (T *array, size_t lhsIdx, size_t rhsIdx)
 Swap two elements of the given array.
template<typename T >
void utl::swap (T *array, size_t lhsIdx, size_t rhsIdx, size_t size)
 Swap two sections of the given array.

Detailed Description

General-purpose functions.


Enumeration Type Documentation

enum utl::cmp_t

Comparison operators.

Enumerator:
cmp_lessThan 

less-than

cmp_lessThanOrEqualTo 

less-than-or-equal-to

cmp_equalTo 

equal-to

cmp_greaterThan 

greater-than

cmp_greaterThanOrEqualTo 

greater-than-or-equal-to

Definition at line 27 of file Object.h.

Specifies a serialization mode.

See also:
Object::serialize
Enumerator:
ser_readable 

human-readable, portable representation

ser_compact 

space-efficient, portable representation

ser_nonportable 

nonportable, time-efficient representation

ser_default 

default representation (via getSerializeMode())

Definition at line 20 of file util.h.

Find strategy.

Enumerator:
find_any 

find any matching object

find_first 

find first matching object

find_last 

find last matching object

find_ip 

find insertion point

Definition at line 17 of file util_inl.h.


Function Documentation

void utl::deInit ( )

De-initialize UTL++.

This function is called by Application's destructor, so when your application class inherits from Application (as recommended) you won't need to worry about calling this function.

Referenced by utl::AutoPtr< T >::~AutoPtr().

void utl::abort ( const char *  filename,
int  lineNo,
const char *  text = NULL 
)

Abort the program - output a message to stderr explaining the reason for the failure, and the filename and line number where the abortion occurred.

On a UNIX platform, this will also cause a core dump to be generated.

Parameters:
filenamefilename where abortion occurred
lineNoline number where abortion occurred
textmessage explaining the reason for the failure
void utl::abort ( const char *  text)

Abort the program - output a message to stderr explaining the reason for the failure.

On a UNIX platform, this will also cause a core dump to be generated.

Parameters:
textmessage explaining the reason for the failure
void utl::die ( const char *  filename,
int  lineNo,
const char *  text = NULL,
int  exitCode = 1 
)

Abort the program - output a message to stderr explaining the reason for the failure, and the filename and line number where the failure occurred.

Unlike abort, die exits with a given error code instead of dumping core.

Parameters:
filenamefilename where failure occurred
lineNoline number where failure occurred
textmessage explaining the reason for the failure
exitCodeexit code
void utl::die ( const char *  text,
int  exitCode = 1 
)

Abort the program - output a message to stderr explaining the reason for the failure, and the filename and line number where the failure occurred.

Unlike abort, die exits with a given error code instead of dumping core.

Parameters:
textmessage explaining the reason for the failure
exitCodeexit code
uint_t utl::getSerializeMode ( )

Get the default serialization mode (see utl::serialize_t).

See also:
Object::serialize

Referenced by utl::serialize().

void utl::setSerializeMode ( uint_t  mode)

Set the default serialization mode (see utl::serialize_t).

See also:
Object::serialize
void* utl::realloc ( void *  ptr,
size_t  size,
size_t  newSize 
)

Re-allocate the given block.

Returns:
address of re-allocated block
Parameters:
ptraddress of block to re-allocate
sizecurrent size of block
newSizenew size of block
char* utl::strdup ( const char *  str)

Duplicate the given string.

Returns:
address of newly allocated block
Parameters:
strstring to duplicate
void utl::serialize ( bool &  b,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize a boolean.

Parameters:
bboolean to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t

Referenced by utl::VectorMD< T >::serialize(), utl::Number< T >::serialize(), utl::Span< T, D >::serialize(), utl::Vector< T >::serialize(), utl::serialize(), and utl::Object::serializeIn().

void utl::serialize ( int8_t &  i,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize an 8-bit integer.

Parameters:
iinteger to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t
void utl::serialize ( char &  c,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize a character.

Parameters:
ccharacter to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t
void utl::serialize ( uint8_t &  i,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize an 8-bit unsigned character.

Parameters:
iinteger to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t
void utl::serialize ( int16_t &  i,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize a 16-bit integer.

Parameters:
iinteger to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t
void utl::serialize ( uint16_t &  i,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize an unsigned 16-bit integer.

Parameters:
iinteger to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t
void utl::serialize ( int32_t &  i,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize a 32-bit integer.

Parameters:
iinteger to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t
void utl::serialize ( uint32_t &  i,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize an unsigned 32-bit integer.

Parameters:
iinteger to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t
void utl::serialize ( long &  i,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize a long integer.

Parameters:
iinteger to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t
void utl::serialize ( ulong_t &  i,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize an unsigned long integer.

Parameters:
iinteger to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t
void utl::serialize ( double &  n,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize a double-precision floating point number.

Parameters:
ndouble to serialize to/from stream
streamstream to serialize to/from
iosee io_t
modesee serialize_t
void utl::serialize ( Object &  object,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize an object.

Parameters:
objectobject to serialize to/from stream
streamstream to serialize to/from
ioio_rd : serializeIn(), io_wr : serializeOut()
modesee serialize_t
size_t utl::allocatedSize ( const Object *  object)
inline

Create a clone of the given object.

Returns:
newly created copy (NULL if object == NULL)
Parameters:
objectobject to clone

Definition at line 34 of file util_inl.h.

template<typename T >
void utl::arrayGrow ( T *&  array,
size_t &  arraySize,
size_t  minSize = size_t_max,
size_t  maxGrowth = size_t_max,
const T *  defVal = NULL 
)

Grow an array.

Parameters:
arrayarray to grow
arraySizearray size
minSizegrow to at least this size
maxGrowth(optional) limit growth
defVal(optional) default value for new objects

Definition at line 53 of file util_inl.h.

References utl::size_t_max.

Referenced by utl::arrayGrow().

template<typename T >
void utl::arrayGrow ( T *&  array,
T *&  arrayPtr,
T *&  arrayLim,
size_t  minSize = size_t_max,
size_t  maxGrowth = size_t_max 
)

Grow an array.

Parameters:
arrayarray to grow
arrayPtrindex into array
arrayLimend of array
minSizeminimum size
maxGrowthlimit growth

Definition at line 102 of file util_inl.h.

References utl::arrayGrow().

template<typename T >
const T& utl::min ( const T &  lhs,
const T &  rhs 
)

Given two objects of the same type, determine the minimum.

Returns:
rhs if rhs < lhs, otherwise lhs
Parameters:
lhsleft-hand-side object
rhsright-hand-side object

Definition at line 127 of file util_inl.h.

Referenced by utl::compare(), utl::ArithmeticEncoder::decode(), utl::Span< Time, Duration >::merge(), utl::multiKeyQuickSort(), utl::Span< T, D >::overlap(), utl::Span< T, D >::overlapSize(), utl::Vector< T >::reverse(), and utl::Time::Time().

template<typename T >
const T& utl::max ( const T &  lhs,
const T &  rhs 
)

Given two objects of the same type, determine the maximum.

Returns:
rhs if rhs > lhs, otherwise lhs
Parameters:
lhsleft-hand-side object
rhsright-hand-side object

Definition at line 141 of file util_inl.h.

Referenced by utl::BoyerMooreSearch< T >::find(), utl::Span< Time, Duration >::merge(), utl::Span< T, D >::overlap(), and utl::Span< T, D >::overlapSize().

template<typename T >
T& utl::cast ( const utl::Object object)

Safely cast an object to the desired type.

Returns:
newly created copy (NULL if object == NULL)
Parameters:
objectobject to clone

Definition at line 154 of file util_inl.h.

References ASSERTD, and isA.

template<typename T >
T* utl::clone ( const T *  object)
template<typename T >
int utl::compare ( const T &  lhs,
const T &  rhs 
)
inline

Compare two objects of the same type.

Returns:
< 0 if lhs < rhs, 0 if lhs = rhs, > 0 if lhs > rhs
Parameters:
lhsleft-hand-side object
rhsright-hand-side object

Definition at line 187 of file util_inl.h.

template<typename T >
int utl::compare ( const T *  lhs,
const T *  rhs,
size_t  len 
)

Compare two same-length arrays of objects of the same type.

Returns:
< 0 if lhs < rhs, 0 if lhs = rhs, > 0 if lhs > rhs
Parameters:
lhsleft-hand-side sequence
rhsright-hand-side sequence
lenlhs and rhs sequence length

Definition at line 227 of file util_inl.h.

References utl::compare().

template<typename T >
int utl::compare ( const T *  lhs,
size_t  lhsLen,
const T *  rhs,
size_t  rhsLen 
)

Compare two arrays of objects of the same type.

Returns:
< 0 if lhs < rhs, 0 if lhs = rhs, > 0 if lhs > rhs
Parameters:
lhsleft-hand-side sequence
lhsLenlhs sequence length
rhsright-hand-side sequence
rhsLenrhs sequence length

Definition at line 254 of file util_inl.h.

References utl::compare(), and utl::min().

bool utl::cmpPredicate ( int  cmpRes,
uint_t  cmpOp 
)
inline

Determine whether the given comparison result is consistent with the given comparison operator.

Returns:
true if cmpRes is consistent with cmpOp, false otherwise
Parameters:
cmpRescomparison result (see compare(), cmp(), etc.)
cmpOpcomparison operator (see cmp_t)

Definition at line 282 of file util_inl.h.

References utl::cmp_greaterThan, and utl::cmp_lessThan.

template<typename T >
void utl::copy ( T *  dest,
const T *  src,
size_t  len 
)

Copy one array of objects to another.

Parameters:
destdestination sequence
srcsource sequence
lensource sequence length

Definition at line 306 of file util_inl.h.

Referenced by utl::SpanAllocator< T, D >::copy(), utl::VectorMD< T >::copy(), utl::Number< T >::copy(), and utl::Vector< T >::copy().

template<typename T >
T utl::roundUp ( const T &  v,
const T &  m 
)
inline

Round the given value v up to the nearest multiple of m.

Returns:
if v is already a multiple of m, then return v, otherwise return v + (v % m)
Parameters:
vvalue to round up
mrounding factor

Definition at line 327 of file util_inl.h.

template<typename T >
T utl::roundDown ( const T &  v,
const T &  m 
)

Round the given value v down to the nearest multiple of m.

Returns:
if v is already a multiple of m, then return v, otherwise return v - (v % m)
Parameters:
vvalue to round down
mrounding factor

Definition at line 346 of file util_inl.h.

template<typename T >
void utl::serialize ( T *  array,
size_t  len,
Stream &  stream,
uint_t  io,
uint_t  mode = ser_default 
)

Serialize an array.

Parameters:
arrayarray to serialize to/from stream
lenarray length
streamstream to serialize to/from
iosee io_t
modesee serialize_t

Definition at line 362 of file util_inl.h.

References utl::getSerializeMode(), utl::ser_default, and utl::serialize().

template<typename T >
void utl::serialize ( T *&  object,
Stream &  stream,
uint_t  io,
uint_t  mode 
)

Serialize an object (boxed serialization).

Parameters:
objectobject to serialize
streamstream to serialize to/from
iosee io_t
modesee serialize_t

Definition at line 389 of file util_inl.h.

References utl::serializeBoxed().

int utl::compare ( const Object *  lhs,
const Object *  rhs,
const Ordering *  ordering = NULL 
)
inline

Compare two objects.

If an Ordering is given it will be used, otherwise Object::compare() will perform the comparison.

Returns:
< 0 if lhs < rhs, 0 if lhs = rhs, > 0 if lhs > rhs
Parameters:
lhsleft-hand-side object
rhsright-hand-side object
ordering(optional) ordering

Definition at line 462 of file util_inl.h.

References ASSERTD, and utl::Object::compare().

int utl::compareNullable ( const Object *  lhs,
const Object *  rhs,
const Ordering *  ordering = NULL 
)
inline

Compare two objects.

This function is just like compare(), but it can deal with one or both of the objects being NULL. If one object is NULL, the other is considered greater. If they are both NULL, they are considered equal. Otherwise, its behavior is identical to that of compare().

Returns:
< 0 if lhs < rhs, 0 if lhs = rhs, > 0 if lhs > rhs
Parameters:
lhsleft-hand-side object
rhsright-hand-side object
ordering(optional) ordering to use

Definition at line 489 of file util_inl.h.

References utl::Object::compare().

Referenced by utl::ComparisonPredicate::cmp(), utl::TRandIt< T >::compare(), utl::TFwdIt< T >::compare(), and utl::TBidIt< T >::compare().

template<typename T >
void utl::swap ( T *  array,
size_t  lhsIdx,
size_t  rhsIdx 
)

Swap two elements of the given array.

Parameters:
arraygiven array
lhsIdxindex of lhs element
rhsIdxindex of rhs element

Definition at line 516 of file util_inl.h.

Referenced by utl::multiKeyQuickSort().

template<typename T >
void utl::swap ( T *  array,
size_t  lhsIdx,
size_t  rhsIdx,
size_t  size 
)

Swap two sections of the given array.

Parameters:
arraygiven array
lhsIdxindex of lhs segment
rhsIdxindex of rhs segment
sizesize of segments to be swapped

Definition at line 535 of file util_inl.h.