27 #ifndef GENERICSORTING 28 #define GENERICSORTING 37 #include <boost/throw_exception.hpp> 53 template <
typename T,
typename Pred = std::less<T>>
class Bubble 68 template <
typename TIterator>
static void Sort(TIterator begin, TIterator end)
77 for (
auto item = begin; item != end - 1; ++item)
79 if (compare(*(item + 1), *item))
81 std::swap(*item, *(item + 1));
85 }
while (changesMade);
96 template <
typename T,
typename Pred = std::less<T>>
class Selection 111 template <
typename TIterator>
static void Sort(TIterator begin, TIterator end)
118 auto minValue = std::min_element(item, end, compare);
120 if (minValue != item)
122 std::swap(*item, *minValue);
137 template <
typename T,
typename Pred = std::less<T>>
class Insertion 152 template <
typename TIterator>
static void Sort(TIterator begin, TIterator end)
159 for (
auto item = begin; item != end; ++item)
161 std::rotate(std::upper_bound(begin, item, *item, compare), item, std::next(item));
174 template <
typename T,
typename Pred = std::less<T>>
class Quick 189 template <
typename TIterator>
static void Sort(TIterator begin, TIterator end)
191 auto length = std::distance(begin, end);
198 auto const pivot = std::next(begin, length / 2);
200 std::nth_element(begin, pivot, end, compare);
203 Sort(std::next(pivot), end);
214 template <
typename T,
typename Pred = std::less<T>>
class Bucket 240 template <
typename TIterator>
249 PutIntoBuckets(begin, end, bucketDefinitions, bucketValues);
250 WriteBackBuckets(begin, bucketValues);
251 SortResult(begin, end);
267 template <
typename TIterator>
271 Sort(begin, end, bucketDefinitions, bucketValues);
288 if (bucketValues.size() != bucketDefinitions.size())
290 bucketValues.resize(bucketDefinitions.size());
307 for (
const auto& bucket : bucketDefinitions)
309 if ((value >= bucket.first) && (value <= bucket.second))
317 if (index == bucketDefinitions.size())
319 BOOST_THROW_EXCEPTION(std::runtime_error(
"bucket not found"));
337 template <
typename TIterator>
342 CheckBucketsSize(bucketDefinitions, bucketValues);
344 for (
auto item = begin; item != end; ++item)
346 size_t bucketIndex{GetBucketIndex(*item, bucketDefinitions)};
347 bucketValues[bucketIndex].emplace_back(*item);
361 template <
typename TIterator>
364 TIterator pos{begin};
366 for (
auto& bucket : bucketValues)
368 std::move(bucket.begin(), bucket.end(), pos);
369 pos += bucket.size();
382 template <
typename TIterator>
static void SortResult(TIterator begin, TIterator end)
391 #endif // GENERICSORTING static void CheckBucketsSize(const bucket_definitions &bucketDefinitions, bucket_values &bucketValues)
Static function to check bucket values collection size.
Definition: GenericSorting.h:285
static void Sort(TIterator begin, TIterator end)
In-place static sort function.
Definition: GenericSorting.h:152
static void Sort(TIterator begin, TIterator end)
In-place static sort function.
Definition: GenericSorting.h:68
static void PutIntoBuckets(TIterator begin, TIterator end, const bucket_definitions &bucketDefinitions, bucket_values &bucketValues)
Static to put the items in to the correct buckets.
Definition: GenericSorting.h:338
Templated static class to perform a quick sort.
Definition: GenericSorting.h:174
std::vector< std::list< T > > bucket_values
Public typedef for bucket values.
Definition: GenericSorting.h:222
Templated static class to perform a bucket sort.
Definition: GenericSorting.h:214
Pred item_compare
Public typedef for predicate.
Definition: GenericSorting.h:141
The core_lib namespace.
Definition: AsioDefines.h:59
Pred item_compare
Public typedef for predicate.
Definition: GenericSorting.h:100
Templated static class to perform a selection sort.
Definition: GenericSorting.h:96
static size_t GetBucketIndex(const T &value, const bucket_definitions &bucketDefinitions)
Static compute the correct bucket index for a given item.
Definition: GenericSorting.h:303
static void Sort(TIterator begin, TIterator end, const bucket_definitions &bucketDefinitions)
Static sort function.
Definition: GenericSorting.h:268
Templated static class to perform a insertion sort.
Definition: GenericSorting.h:137
static void Sort(TIterator begin, TIterator end)
In-place static sort function.
Definition: GenericSorting.h:111
Templated static class to perform a bubble sort.
Definition: GenericSorting.h:53
Pred item_compare
Public typedef for predicate.
Definition: GenericSorting.h:57
Pred item_compare
Public typedef for predicate.
Definition: GenericSorting.h:178
static void Sort(TIterator begin, TIterator end, const bucket_definitions &bucketDefinitions, bucket_values &bucketValues)
In-place static sort function.
Definition: GenericSorting.h:241
std::vector< std::pair< T, T > > bucket_definitions
Public typedef for bucket definitions.
Definition: GenericSorting.h:220
static void Sort(TIterator begin, TIterator end)
In-place static sort function.
Definition: GenericSorting.h:189
static void SortResult(TIterator begin, TIterator end)
Static method to perform a final sort on the collection.
Definition: GenericSorting.h:382
static void WriteBackBuckets(TIterator begin, const bucket_values &bucketValues)
Static to move the items from the buckets back into the original collection.
Definition: GenericSorting.h:362
Pred item_compare
Public typedef for predicate.
Definition: GenericSorting.h:218