14 #ifndef TLX_SORT_STRINGS_INSERTION_SORT_HEADER 15 #define TLX_SORT_STRINGS_INSERTION_SORT_HEADER 26 namespace sort_strings_detail {
32 template <
typename StringPtr>
37 typedef typename StringSet::Iterator Iterator;
38 typedef typename StringSet::String String;
39 typedef typename StringSet::CharIterator CharIterator;
46 const Iterator begin = ss.begin();
49 for (Iterator i = begin + 1;
TLX_UNLIKELY(--n != 0); ++i)
51 String tmp = std::move(ss[i]);
56 CharIterator s = ss.get_chars(ss[j - 1], depth);
57 CharIterator t = ss.get_chars(tmp, depth);
59 while (
TLX_LIKELY(ss.is_equal(ss[j - 1], s, tmp, t)))
66 ss[j] = std::move(ss[j - 1]);
70 ss[j] = std::move(tmp);
79 template <
typename StringPtr>
84 typedef typename StringPtr::LcpType LcpType;
85 typedef typename StringSet::Iterator Iterator;
86 typedef typename StringSet::String String;
87 typedef typename StringSet::CharIterator CharIterator;
90 const StringSet& ss = strptr.
active();
94 const Iterator begin = ss.begin();
96 for (
size_t j = 0; j < n - 1; ++j)
100 String new_str = std::move(ss[begin + j]);
101 LcpType new_lcp = depth;
106 LcpType prev_lcp = new_lcp;
108 String cur_str = std::move(ss[begin + i - 1]);
109 LcpType cur_lcp = strptr.get_lcp(i);
111 if (cur_lcp < new_lcp)
116 ss[begin + i - 1] = std::move(cur_str);
119 else if (cur_lcp == new_lcp)
123 CharIterator c1 = ss.get_chars(new_str, new_lcp);
124 CharIterator c2 = ss.get_chars(cur_str, new_lcp);
126 while (ss.is_equal(new_str, c1, cur_str, c2))
127 ++c1, ++c2, ++new_lcp;
130 if (!ss.is_less(new_str, c1, cur_str, c2))
138 ss[begin + i - 1] = std::move(cur_str);
144 ss[begin + i] = std::move(cur_str);
145 strptr.
set_lcp(i + 1, cur_lcp);
150 ss[begin + i] = std::move(new_str);
151 strptr.
set_lcp(i + 1, new_lcp);
160 String new_str = std::move(ss[begin + j]);
161 LcpType new_lcp = depth;
166 LcpType prev_lcp = new_lcp;
168 String cur_str = std::move(ss[begin + i - 1]);
169 LcpType cur_lcp = strptr.get_lcp(i);
171 if (cur_lcp < new_lcp)
176 ss[begin + i - 1] = std::move(cur_str);
179 else if (cur_lcp == new_lcp)
183 CharIterator c1 = ss.get_chars(new_str, new_lcp);
184 CharIterator c2 = ss.get_chars(cur_str, new_lcp);
186 while (ss.is_equal(new_str, c1, cur_str, c2))
187 ++c1, ++c2, ++new_lcp;
190 if (!ss.is_less(new_str, c1, cur_str, c2))
198 ss[begin + i - 1] = std::move(cur_str);
204 ss[begin + i] = std::move(cur_str);
206 strptr.
set_lcp(i + 1, cur_lcp);
211 ss[begin + i] = std::move(new_str);
213 strptr.
set_lcp(i + 1, new_lcp);
226 #endif // !TLX_SORT_STRINGS_INSERTION_SORT_HEADER const StringSet & active() const
return currently active array
void set_lcp(size_t, const LcpType &) const
set the i-th lcp to v and check its value
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.
SFINAE enable_if – copy of std::enable_if<> with less extra cruft.
Objectified string array pointer array.