23 #ifndef TLX_SORT_STRINGS_MULTIKEY_QUICKSORT_HEADER 24 #define TLX_SORT_STRINGS_MULTIKEY_QUICKSORT_HEADER 37 namespace sort_strings_detail {
39 template <
typename StringSet>
41 typename StringSet::Iterator a,
typename StringSet::Iterator b,
size_t n) {
46 template <
typename StringSet>
47 static inline typename StringSet::Iterator
med3func(
49 typename StringSet::Iterator a,
typename StringSet::Iterator b,
50 typename StringSet::Iterator c,
size_t depth) {
51 typename StringSet::Char va = ss.get_char(*a, depth);
52 typename StringSet::Char vb = ss.get_char(*b, depth);
55 typename StringSet::Char vc = ss.get_char(*c, depth);
56 if (vc == va || vc == vb)
59 ? (vb < vc ? b : (va < vc ? c : a))
60 : (vb > vc ? b : (va < vc ? a : c));
73 template <
typename StringPtr>
75 const StringPtr& strptr,
size_t depth,
size_t memory) {
78 typedef typename StringSet::Iterator Iterator;
80 const StringSet& ss = strptr.
active();
81 const Iterator a = ss.begin();
85 static const size_t memory_use =
86 2 *
sizeof(size_t) +
sizeof(StringSet) + 5 *
sizeof(Iterator);
88 if (n < 32 || (memory != 0 && memory < memory_use + 1)) {
93 Iterator pa, pb, pc, pd, pn;
97 Iterator pm = a + (n / 2);
102 pl =
med3func(ss, pl, pl + d, pl + 2 * d, depth);
103 pm =
med3func(ss, pm - d, pm, pm + d, depth);
104 pn =
med3func(ss, pn - 2 * d, pn - d, pn, depth);
106 pm =
med3func(ss, pl, pm, pn, depth);
108 int pivot = ss.get_char(*a, depth);
112 while (pb <= pc && (r = static_cast<int>(ss.get_char(*pb, depth)) - pivot) <= 0) {
116 while (pb <= pc && (r = static_cast<int>(ss.get_char(*pc, depth)) - pivot) >= 0) {
125 size_t pe_start_index, pe_end_index;
127 vec_swap<StringSet>(a, pb - r, r);
130 pe_end_index = (pn - a) - r;
131 vec_swap<StringSet>(pb, pn - r, r);
133 for (
size_t it = pe_start_index + 1; it < pe_end_index; ++it)
140 strptr.
set_lcp(a - ss.begin() + r, depth);
144 depth, memory - memory_use);
146 if (ss.get_char(*(a + r), depth) != 0) {
148 strptr.
sub(a - ss.begin() + r, (pa - a) + (pn - pd - 1)),
149 depth + 1, memory - memory_use);
153 strptr.
set_lcp(a - ss.begin() + n - r, depth);
155 if ((r = pd - pc) > 1) {
157 depth, memory - memory_use);
167 #endif // !TLX_SORT_STRINGS_MULTIKEY_QUICKSORT_HEADER StringPtr sub(size_t offset, size_t sub_size) const
Advance (both) pointers by given offset, return sub-array.
static void multikey_quicksort(const StringPtr &strptr, size_t depth, size_t memory)
Generic multikey quicksort for strings.
const StringSet & active() const
return currently active array
static void vec_swap(typename StringSet::Iterator a, typename StringSet::Iterator b, size_t n)
void set_lcp(size_t, const LcpType &) const
set the i-th lcp to v and check its value
static uint32_t min(uint32_t x, uint32_t y)
static enable_if<!StringPtr::with_lcp, void >::type insertion_sort(const StringPtr &strptr, size_t depth, size_t)
Generic insertion sort for abstract string sets.
void swap(CountingPtr< A, D > &a1, CountingPtr< A, D > &a2) noexcept
swap enclosed object with another counting pointer (no reference counts need change) ...
Objectified string array pointer array.
static StringSet::Iterator med3func(const StringSet &ss, typename StringSet::Iterator a, typename StringSet::Iterator b, typename StringSet::Iterator c, size_t depth)