This class implements a merge and sort algorithm based on the divide-and-conquer concept.
More...
|
| JMergeSort () |
| Default constructor.
|
|
template<class JSuperContainer_t > |
void | operator() (const JSuperContainer_t &input, std::vector< JElement_t, JAllocator_t > &output) const |
| Merge set of sorted data sets into single sorted data set.
|
|
template<class JSuperContainer_t , class JComparator_t > |
void | operator() (const JSuperContainer_t &input, std::vector< JElement_t, JAllocator_t > &output, const JComparator_t &comparator) const |
| Merge set of sorted data sets into single sorted data set.
|
|
template<class T > |
void | operator() (T __begin, T __end, std::vector< JElement_t, JAllocator_t > &output) const |
| Merge set of sorted data sets into single sorted data set.
|
|
template<class T , class JComparator_t > |
void | operator() (T __begin, T __end, std::vector< JElement_t, JAllocator_t > &output, const JComparator_t &comparator) const |
| Merge set of sorted data sets into single sorted data set.
|
|
template<class JElement_t, class JAllocator_t = std::allocator<JElement_t>>
class JTOOLS::JMergeSort< JElement_t, JAllocator_t >
This class implements a merge and sort algorithm based on the divide-and-conquer concept.
Note that this class heavily uses internal buffers. It should therefore be used as an I/O operator, rather than a constructor.
The template argument refers to the element of the input and output containers.
The input data containers should be compatible with that of STL containers, i.e provide for the following member methods:
typedef <constant iterator> const_iterator;
size_t size() const; // number of elements;
const_iterator begin() const; // begin of data
const_iterator end() const; // end of data
The input should consist of a set of sorted data sets, each terminated by an end marker.
In this, an end marker corresponds to a value that is always larger than any physical value.
The end marker is the first element beyond the size but within the capacity of the container.
The output then consists of a single sorted data set which is also terminated by an end marker.
Definition at line 49 of file JMergeSort.hh.
template<class JElement_t , class JAllocator_t = std::allocator<JElement_t>>
template<class JContainer_t , class JComparator_t >
static void JTOOLS::JMergeSort< JElement_t, JAllocator_t >::merge |
( |
const JContainer_t & | first, |
|
|
const JContainer_t & | second, |
|
|
std::vector< JElement_t > & | output, |
|
|
const JComparator_t & | comparator ) |
|
inlinestaticprivate |
Fast merge of two data sets into one data set.
It is assumed that the input data sets are sorted and terminated by an end marker. In this, an end marker corresponds to a value that is larger than any physical value. The output data set will be terminated by the same end marker.
- Parameters
-
first | first data set |
second | second data set |
output | output data set |
comparator | element comparator |
Definition at line 182 of file JMergeSort.hh.
186 {
187 size_t n = first.size() + second.size();
188
189 output.reserve(
n + 1);
190 output.resize (
n + 0);
191
192 typename JContainer_t::const_iterator i = first .begin();
193 typename JContainer_t::const_iterator
j = second.begin();
195
196 for ( ;
n >= 4;
n -= 4) {
197
198 if (comparator(*i,*
j))
199 *out++ = *i++;
200 else
202
203 if (comparator(*i,*
j))
204 *out++ = *i++;
205 else
207
208 if (comparator(*i,*
j))
209 *out++ = *i++;
210 else
212
213 if (comparator(*i,*
j))
214 *out++ = *i++;
215 else
217 }
218
219 for ( ;
n != 0; --
n) {
220
221 if (comparator(*i,*
j))
222 *out++ = *i++;
223 else
225 }
226
227 *out = *i;
228 }