SHA_Sort::MergeWithBuffer< Container, IT > Class Template Reference

#include <merge.hxx>

Public Member Functions

void operator() (IT begin, IT middle, IT end)
 

Detailed Description

template<typename Container, typename IT>
class SHA_Sort::MergeWithBuffer< Container, IT >

MergeWithBuffer - Merging of two ordered sequences of a collection using intermediate buffer Proceed a merge of two sequences of elements contained in [begin, middle[ and [middle, end[.

Warning
Both sequence [bengin, middle[ and [middle, end[ need to be ordered.
Remarks
use MergeInPlace to proceed the merge in place: Takes lower memory consumption and higher computation consumption.
Complexity
O(N).
Template Parameters
ITtype.
Parameters
begin,middle,enditerators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
Returns
void.

Definition at line 99 of file merge.hxx.

Member Function Documentation

template<typename Container, typename IT>
void SHA_Sort::MergeWithBuffer< Container, IT >::operator() ( IT  begin,
IT  middle,
IT  end 
)
inline

Definition at line 102 of file merge.hxx.

103  {
104  if (std::distance(begin, middle) < 1 || std::distance(middle, end) < 1)
105  return;
106 
107  Container buffer;
108  buffer.resize(std::distance(begin, end)); // Reserve right space (e.g. string container)
109  auto buffIt = buffer.begin();
110  auto tmpBegin = begin;
111 
112  // Merge into the buffer array taking one by one the lowest sequence element
113  const auto curMiddle(middle);
114  while (begin != curMiddle && middle != end)
115  {
116  if (*begin <= *middle)
117  *buffIt++ = *begin++;
118  else
119  *buffIt++ = *middle++;
120  }
121 
122  // Finish remaining list into the buffer
123  for (; begin != curMiddle; ++buffIt, ++begin)
124  *buffIt = *begin;
125  for (; middle != end; ++buffIt, ++middle)
126  *buffIt = *middle;
127 
128  // Refill array given the right position
129  for (buffIt = buffer.begin(); buffIt != buffer.end(); ++buffIt, ++tmpBegin)
130  *tmpBegin = *buffIt;
131  }

The documentation for this class was generated from the following file: