From bec09d30945c7dc143dc4ccd5e2fba1dcaccaac1 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 4 Apr 2006 23:55:47 +0000 Subject: Turned FSList::sort into a generic function which can be applied to anything which implements comparable iterators (like Array, List, or plain C arrays) svn-id: r21617 --- common/func.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'common') diff --git a/common/func.h b/common/func.h index 10c80e66f5..e2cd20fdb3 100644 --- a/common/func.h +++ b/common/func.h @@ -62,6 +62,28 @@ GENERATE_TRIVIAL_HASH_FUNCTOR(unsigned long); #undef GENERATE_TRIVIAL_HASH_FUNCTOR +// Simple sort function, modelled after std::sort. +// Use it like this: sort(container.begin(), container.end()). +// Also work on plain old int arrays etc. +template +void sort(T first, T last) { + if (first == last) + return; + + // Simple selection sort + T i(first); + for (; i != last; ++i) { + T min(i); + T j(i); + ++j; + for (; j != last; ++j) + if (*j < *min) + min = j; + if (min != i) + SWAP(*min, *i); + } +} + } // End of namespace Common -- cgit v1.2.3