13 template <
class Arg1,
class Arg2,
class Result>
18 typedef Result result_t;
139 #include <libutl/algorithms.h> 140 #include <libutl/Vector.h> 159 template <
class T,
class P,
class C>
171 size_t n = end - begin;
174 for (pi = array + 1; --n > 0; pi++)
176 for (pj = pi; pj > array; pj--)
178 for (s = *(pj - 1) + depth, t = *pj + depth; (cmp(*s, *t) == 0) && (cmp(*s, defT) != 0);
181 if (cmp(*s, *t) <= 0)
break;
201 template <
class T,
class P,
class C>
206 if (cmp(vl = array[l][depth], vm = array[m][depth]) == 0)
210 if ((cmp(vh = array[h][depth], vl) == 0) || (cmp(vh, vm) == 0))
216 return (cmp(vm, vh) < 0) ? m : ((cmp(vl, vh) < 0) ? h : l);
220 return (cmp(vm, vh) > 0) ? m : ((cmp(vl, vh) < 0) ? l : h);
237 template <
class T,
class P,
class C>
242 size_t i, l, m, h, a, b, c, d, n;
255 multiKeyInsertionSort<T>(array, cmp, begin, end, depth);
271 m = (begin + end) / 2;
276 l = multiKeyMedianOfThree<T>(array, cmp, l, l + n8, l + 2 * n8, depth);
277 m = multiKeyMedianOfThree<T>(array, cmp, m - n8, m, m + n8, depth);
278 h = multiKeyMedianOfThree<T>(array, cmp, h - 2 * n8, h - n8, h, depth);
280 m = multiKeyMedianOfThree<T>(array, cmp, l, m, h, depth);
282 T v = array[begin][depth];
291 r = cmp(array[b][depth], v);
310 r = cmp(array[c][depth], v);
338 r =
min(a - begin, b - a);
340 r =
min(d - c, end - d - 1);
346 bed[0][1] = begin + r;
349 bed[1][0] = begin + r;
350 bed[1][1] = end - (d - c);
351 bed[1][2] = depth + 1;
352 if (cmp(array[begin + r][depth], T()) == 0)
354 bed[1][1] = bed[1][0];
359 bed[1][3] = bed[1][1] - bed[1][0];
368 if (bed[2][3] < bed[1][3])
370 memmove(bed[3], bed[1], 4 *
sizeof(
size_t));
371 memmove(bed[1], bed[2], 4 *
sizeof(
size_t));
372 memmove(bed[2], bed[3], 4 *
sizeof(
size_t));
374 if (bed[1][3] < bed[0][3])
376 memmove(bed[3], bed[0], 4 *
sizeof(
size_t));
377 memmove(bed[0], bed[1], 4 *
sizeof(
size_t));
378 memmove(bed[1], bed[3], 4 *
sizeof(
size_t));
380 if (bed[2][3] < bed[1][3])
382 memmove(bed[3], bed[1], 4 *
sizeof(
size_t));
383 memmove(bed[1], bed[2], 4 *
sizeof(
size_t));
384 memmove(bed[2], bed[3], 4 *
sizeof(
size_t));
388 for (i = 2; i >= 1; i--)
419 template <
class T,
class C>
422 const T* array,
size_t begin,
size_t end,
const T& key, C cmpFn,
uint_t findType =
find_first)
424 size_t low, high, mid, res;
431 if ((high-- - low) == 0)
return low;
437 res = mid = (low + high) / 2;
440 cmpRes = cmpFn(key, array[mid]);
459 if (findType ==
find_ip)
return (cmpRes != 0) ? low : res;
467 if ((res > low) && (cmpFn(key, array[res - 1]) == 0))
469 res =
binarySearch(array, low, res, key, cmpFn, findType);
474 if ((res < high) && (cmpFn(key, array[res + 1]) == 0))
476 res =
binarySearch(array, res + 1, high + 1, key, cmpFn, findType);
find last matching object
Reverse subtraction function.
Abstraction for binary functions.
Allows multi-key sort algorithms to be used with objects.
const size_t size_t_max
Maximum size_t value.
void swap(T *array, size_t lhsIdx, size_t rhsIdx)
Swap two elements of the given array.
const char & operator[](size_t idx) const
Array element access operator.
void multiKeyQuickSort(P *array, C cmp, size_t begin, size_t end, size_t depth=0)
Multi-key quick-sort (part of) the given sequence.
T operator()(const T &lhs, const T &rhs) const
Return the result of (rhs - lhs).
unsigned int uint_t
Unsigned integer.
#define ABORT()
Immediately terminates the program.
const char * operator+(size_t num) const
Addition operator.
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.
find first matching object
void multiKeyInsertionSort(P *array, C cmp, size_t begin, size_t end, size_t depth=0)
Multi-key insertion-sort (part of) the given sequence.
#define UTL_TYPE_NO_SERIALIZE(typeName)
Declare that a type cannot be serialized.
size_t binarySearch(const T *array, size_t begin, size_t end, const T &key, C cmpFn, uint_t findType=find_first)
Binary search an ordered sequence for a key.
Root of UTL++ class hierarchy.
const Object * getObject() const
Return the object.
size_t multiKeyMedianOfThree(P *array, C cmp, size_t l, size_t m, size_t h, size_t depth=0)
Perform median-of-three partitioning.
#define ASSERTD
Do an assertion in DEBUG mode only.
T operator()(const T &lhs, const T &rhs) const
Return the result of (lhs - rhs).