[DEV] add basic interface of quicksort for vector

This commit is contained in:
Edouard DUPIN 2018-09-13 22:48:19 +02:00
parent 68a391e7ad
commit 6cd2c7414e
3 changed files with 93 additions and 28 deletions

View File

@ -10,6 +10,7 @@
#include <etk/Vector.hpp>
#include <etk/Allocator.hpp>
#include <etk/Exception.hpp>
#include <etk/algorithm.hpp>
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<ETK_MAP_TYPE_KEY, ETK_MAP_TYPE_DATA>* const & _key1,
etk::Pair<ETK_MAP_TYPE_KEY, ETK_MAP_TYPE_DATA>* const & _key2) {
return _key1->first < _key2->first;
});
sortFunction tmp = [](etk::Pair<ETK_MAP_TYPE_KEY, ETK_MAP_TYPE_DATA>* const & _key1,
etk::Pair<ETK_MAP_TYPE_KEY, ETK_MAP_TYPE_DATA>* const & _key2) {
return _key1->first < _key2->first;
};
etk::algorithm::quickSort(m_data,
0,
m_data.size(),
tmp);
}
}
public:

View File

@ -9,6 +9,7 @@
//#include <etk/debug.hpp>
#include <etk/Stream.hpp>
#include <etk/Allocator.hpp>
//#include <etk/algorithm.hpp>
//#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<typename ETK_VECTOR_TYPE_1, typename ETK_VECTOR_TYPE_2>

79
etk-core/algorithm.hpp Normal file
View File

@ -0,0 +1,79 @@
/**
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#pragma once
#include <etk/types.hpp>
#include <etk/Stream.hpp>
#include <etk/Allocator.hpp>
namespace etk {
namespace algorithm {
namespace detail {
template<class ETK_VECTOR_TYPE>
int64_t quickSortPartition (etk::Vector<ETK_VECTOR_TYPE>& _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<class ETK_VECTOR_TYPE>
void quickSort(etk::Vector<ETK_VECTOR_TYPE>& _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<class ETK_VECTOR_TYPE>
void quickSort(etk::Vector<ETK_VECTOR_TYPE>& _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<class ETK_VECTOR_TYPE>
void quickSort(etk::Vector<ETK_VECTOR_TYPE>& _data,
bool (*_comparator)(ETK_VECTOR_TYPE const &, ETK_VECTOR_TYPE const &)) {
detail::quickSort(_data, 0, _data.size()-1, _comparator);
}
}
}