SHA_Sort::MergeInPlace< IT > Class Template Reference

#include <merge.hxx>

Public Member Functions

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

Detailed Description

template<typename IT>
class SHA_Sort::MergeInPlace< IT >

MergeInplace - Inplace merging of two ordered sequences of a collection Proceed a in place 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 MergeWithBuffer to proceed the merge using a buffer: Takes higher memory consumption and lower computation consumption.
Complexity
O(N * M)
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 46 of file merge.hxx.

Member Function Documentation

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

Definition at line 49 of file merge.hxx.

50  {
51  if (std::distance(begin, middle) < 1 || std::distance(middle, end) < 1)
52  return;
53 
54  // Use first half as receiver
55  for(; begin < middle; ++begin)
56  {
57  if (*middle >= *begin)
58  continue;
59 
60  typename std::iterator_traits<IT>::value_type value;
61  std::swap(value, *begin); // keep the higher value
62  std::swap(*begin, *middle); // Place it at the beginning of the second list
63 
64  // Displace the higher value in the right place of the second list by swapping
65  auto it = middle;
66  for (; it != end - 1; ++it)
67  {
68  if (*(it + 1) >= value)
69  break;
70 
71  std::swap(*it, *(it + 1));
72  }
73 
74  // Restore the value at his right place
75  std::swap(*it, value);
76  }
77  }

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