diff --git a/etk-core/Map.hpp b/etk-core/Map.hpp index 91e97d9..2454fad 100644 --- a/etk-core/Map.hpp +++ b/etk-core/Map.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace etk { /** @@ -274,12 +275,16 @@ namespace etk { return; } if (m_comparator != null) { - m_data.sort(0, m_data.size(), m_comparator); + etk::algorithm::quickSort(m_data, 0, m_data.size(), m_comparator); } else { - m_data.sort(0, m_data.size(), [](etk::Pair* const & _key1, - etk::Pair* const & _key2) { - return _key1->first < _key2->first; - }); + sortFunction tmp = [](etk::Pair* const & _key1, + etk::Pair* const & _key2) { + return _key1->first < _key2->first; + }; + etk::algorithm::quickSort(m_data, + 0, + m_data.size(), + tmp); } } public: diff --git a/etk-core/Vector.hpp b/etk-core/Vector.hpp index 667432d..104d847 100644 --- a/etk-core/Vector.hpp +++ b/etk-core/Vector.hpp @@ -9,6 +9,7 @@ //#include #include #include +//#include //#define ETK_VECTOR_DEBUG(...) printf(__VA_ARGS__) #define ETK_VECTOR_DEBUG(...) do {} while (false) @@ -978,31 +979,11 @@ namespace etk { // TODO : Later return false; } + /* void sort(size_t _start, size_t _stop, bool (*_comparator)(const ETK_VECTOR_TYPE&, const ETK_VECTOR_TYPE&)) { - if (_stop > m_size) { - _stop = m_size; - } - if (_start > m_size) { - _start = m_size; - } - if (_start > _stop) { - size_t start = _start; - _start = _stop; - _stop = start; - } - for (size_t iii=_start; iii<_stop; ++iii) { - bool swapped = false; - for (size_t jjj=_start; jjj<_stop - (iii+1); ++jjj) { - if (_comparator(m_data[jjj], m_data[jjj+1]) == false) { - etk::swap(m_data[jjj], m_data[jjj+1]); - swapped = true; - } - } - if (swapped == false) { - //break; - } - } + etk::algorithm::quickSort(*this, _start, _stop, _comparator); } + */ }; //! @not_in_doc template diff --git a/etk-core/algorithm.hpp b/etk-core/algorithm.hpp new file mode 100644 index 0000000..caa0921 --- /dev/null +++ b/etk-core/algorithm.hpp @@ -0,0 +1,79 @@ +/** + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license MPL-2 (see license file) + */ +#pragma once + +#include +#include +#include + +namespace etk { + namespace algorithm { + namespace detail { + template + int64_t quickSortPartition (etk::Vector& _data, + int64_t _low, + int64_t _high, + bool (*_comparator)(const ETK_VECTOR_TYPE&, const ETK_VECTOR_TYPE&)) { + int64_t iii = (_low - 1); + for (int64_t jjj = _low; jjj < _high; ++jjj) { + if (_comparator(_data[jjj], _data[_high]) == true) { + iii++; + etk::swap(_data[iii], _data[jjj]); + } + } + etk::swap(_data[iii + 1], _data[_high]); + return (iii + 1); + } + template + void quickSort(etk::Vector& _data, + int64_t _low, + int64_t _high, + bool (*_comparator)(const ETK_VECTOR_TYPE&, const ETK_VECTOR_TYPE&)) { + if (_low >= _high) { + return; + } + // pi is partitioning index, arr[p] is now at right place + int64_t pi = detail::quickSortPartition(_data, _low, _high, _comparator); + // Separately sort elements before partition and after partition + //if (pi != 0) { + detail::quickSort(_data, _low, pi - 1, _comparator); + //} + detail::quickSort(_data, pi + 1, _high, _comparator); + } + } + /** + * @brief: QuickSort implementation of sorting vector. + * @param[in,out] _data Vector to sort. + * @param[in] _low Lowest element to sort. + * @param[in] _high Highest element to sort. + * @param[in] _high Comparator function of this element. + */ + template + void quickSort(etk::Vector& _data, + size_t _low, + size_t _high, + bool (*_comparator)(ETK_VECTOR_TYPE const &, ETK_VECTOR_TYPE const &)) { + if (_high >= _data.size()) { + _high = _data.size()-1; + } + /*if (_low >= _data.size()) { + _low = _data.size()-1; + }*/ + detail::quickSort(_data, _low, _high, _comparator); + } + /** + * @brief: QuickSort implementation of sorting vector (all elements. + * @param[in,out] _data Vector to sort. + * @param[in] _high Comparator function of this element. + */ + template + void quickSort(etk::Vector& _data, + bool (*_comparator)(ETK_VECTOR_TYPE const &, ETK_VECTOR_TYPE const &)) { + detail::quickSort(_data, 0, _data.size()-1, _comparator); + } + } +} +