huc::search Namespace Reference

Functions

template<typename IT , typename IsEqual = std::equal_to<typename std::iterator_traits<IT>::value_type>>
int BinarySearch (const IT &begin, const IT &end, const typename std::iterator_traits< IT >::value_type &key)
 
template<typename IT , typename Compare = std::less_equal<typename std::iterator_traits<IT>::value_type>>
IT KthOrderStatistic (const IT &begin, const IT &end, unsigned int k)
 
template<typename IT , typename Distance = std::minus<typename std::iterator_traits<IT>::value_type>>
std::pair< int, int > MaxDistance (const IT &begin, const IT &end)
 
template<typename Container , typename IT , typename Compare = std::greater_equal<typename std::iterator_traits<IT>::value_type>>
Container MaxMElements (const IT &begin, const IT &end, const int m)
 
template<typename IT , typename Distance = std::minus<typename std::iterator_traits<IT>::value_type>, typename Compare = std::greater<typename std::iterator_traits<IT>::value_type>>
std::pair< int, int > MaxSubSequence (const IT &begin, const IT &end)
 

Function Documentation

template<typename IT , typename IsEqual = std::equal_to<typename std::iterator_traits<IT>::value_type>>
int huc::search::BinarySearch ( const IT &  begin,
const IT &  end,
const typename std::iterator_traits< IT >::value_type &  key 
)

Binary Search - Given a sorted sequence, find the exact position of a specific value.

Template Parameters
ITtype using to go through the collection.
Comparefunctor type (std::less_equal to find kth smallest element, std::greater_equal to find the kth biggest one).
Parameters
begin,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.
keythe key value to be searched.
Returns
The index of the first key occurence found, -1 if not found.

Definition at line 44 of file binary.hxx.

45  {
46  int index = -1;
47  auto lowIt = begin;
48  auto highIt = end;
49  auto middleIt = lowIt + std::distance(lowIt, highIt) / 2;
50 
51  // While there is still objects between the two ITs and no object has been foud yet
52  while(lowIt < highIt && index < 0)
53  {
54  // Found object - Set index computed from initial begin IT
55  if (IsEqual()(key, *middleIt))
56  {
57  index = static_cast<int>(std::distance(begin, middleIt));
58  break;
59  }
60  // Search key within upper collection
61  else if (key > *middleIt)
62  lowIt = middleIt + 1;
63  // Search key within lower collection
64  else
65  highIt = middleIt;
66 
67  middleIt = lowIt + std::distance(lowIt, highIt) / 2;
68  }
69 
70  return index;
71  }
template<typename IT , typename Compare = std::less_equal<typename std::iterator_traits<IT>::value_type>>
IT huc::search::KthOrderStatistic ( const IT &  begin,
const IT &  end,
unsigned int  k 
)

Kt'h Order Statitstic - Find the kth smallest/biggest element contained within [begin, end[.

Warning
this method is not stable (does not keep order with element of the same value).
this method changes the elements order between your iterators.
Template Parameters
ITRandom-access iterator type.
Comparefunctor type (std::less_equal to find kth smallest element, std::greater_equal to find the kth biggest one).
Parameters
begin,end- ITs 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.
kthe zero-based kth element - 0 for the biggest/smallest.
Returns
the kth smallest IT element of the array, the end IT in case of failure.

Definition at line 48 of file kth_order_statistic.hxx.

49  {
50  // Sequence does not contain enough elements: Could not find the k'th one.
51  const auto kSize = static_cast<const int>(std::distance(begin, end));
52  if (k >= static_cast<unsigned int>(kSize))
53  return end;
54 
55  auto pivot = begin + (rand() % kSize); // Take random pivot
56  auto newPivot = sort::Partition<IT, Compare>(begin, pivot, end); // Partition
57 
58  // Get the index of the pivot (i'th value)
59  const auto kPivotIndex = std::distance(begin, newPivot);
60 
61  // If at the k'th position: found!
62  if (kPivotIndex == k)
63  return newPivot;
64 
65  // Recurse search on left part if there is more than k elements within the left sequence
66  // Recurse search on right otherwise
67  return (kPivotIndex > k) ? KthOrderStatistic<IT, Compare>(begin, newPivot, k)
68  : KthOrderStatistic<IT, Compare>(newPivot, end, k - kPivotIndex);
69 
70  }
template<typename IT , typename Distance = std::minus<typename std::iterator_traits<IT>::value_type>>
std::pair<int, int> huc::search::MaxDistance ( const IT &  begin,
const IT &  end 
)

MaxDistance Identifies the two indexes of the array with the maximal distance.

Known as the simple stock market problem with the default functor (std::minus): It finds i and j that maximizes Aj - Ai, where i < j. In other words, maximizes the benefice of a resell given an array of prices varying over time.

Template Parameters
Iteratortype using to go through the collection.
Distancefunctor type computing the distance between two elements.
Parameters
begin,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
indexes of the array with the maximal distance, <-1,-1> in case of error.

Definition at line 47 of file max_distance.hxx.

48  {
49  if (std::distance(begin, end) < 2)
50  return std::pair<int, int>(-1, -1);
51 
52  int minValIdx = 0;
53  std::pair<int, int> indexes(minValIdx, 1);
54  auto maxDist = Distance()(*begin, *(begin + 1));
55 
56  for (auto it = begin + 1; it != end; ++it)
57  {
58  const auto currentIdx = static_cast<const int>(std::distance(begin, it));
59 
60  // Keeps track of the minimum value index
61  if (*it < *(begin + minValIdx))
62  minValIdx = currentIdx;
63 
64  // Keeps track of the largest distance and the indexes
65  const auto distance = Distance()(*it, *(begin + minValIdx));
66  if (distance > maxDist)
67  {
68  maxDist = distance;
69  indexes.first = minValIdx;
70  indexes.second = currentIdx;
71  }
72  }
73 
74  return indexes;
75  }
template<typename Container , typename IT , typename Compare = std::greater_equal<typename std::iterator_traits<IT>::value_type>>
Container huc::search::MaxMElements ( const IT &  begin,
const IT &  end,
const int  m 
)

Max M Elements Identify the m maximal/minimal values sorted in decreasing/increasing order.

using this algorithm with the size of the vector as the number of elements to be found will give you a bubble sort algorithm.

Template Parameters
Containertype used to return the elements.
ITtype using to go through the collection.
Comparefunctor type.
Parameters
begin,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.
mthe numbers of max elements value to be found.
Returns
a vector of sorted in decreasing/increasing order of the m maximum/minimum elements, an empty array in case of failure.

Definition at line 51 of file max_m_elements.hxx.

52  {
53  if (m < 1 || m > std::distance(begin, end))
54  return Container();
55 
56  // Initiale values depends on the comparator functor
57  const auto limitValue = Compare()(0,
58  std::numeric_limits<typename std::iterator_traits<IT>::value_type>::lowest()) ?
59  std::numeric_limits<typename std::iterator_traits<IT>::value_type>::lowest() :
60  std::numeric_limits<typename std::iterator_traits<IT>::value_type>::max();
61 
62  // Allocate the container final size
63  Container maxMElements;
64  maxMElements.resize(m, limitValue);
65  for (auto it = begin; it != end; ++it)
66  {
67  // Insert the value at the right place and bubble down replacement value
68  int index = 0;
69  auto tmpVal = *it;
70  for (auto subIt = maxMElements.begin(); index < m; ++subIt, ++index)
71  if (Compare()(tmpVal, *subIt))
72  std::swap(*subIt, tmpVal);
73  }
74 
75  return maxMElements;
76  }
template<typename IT , typename Distance = std::minus<typename std::iterator_traits<IT>::value_type>, typename Compare = std::greater<typename std::iterator_traits<IT>::value_type>>
std::pair<int, int> huc::search::MaxSubSequence ( const IT &  begin,
const IT &  end 
)

Max Sub Sequence Identify the subarray with the maximum/minimum sum.

The algorithm can be seen as an elicitation of the MaxDistance one. One of the problem resolved by this algorithm is: "Given an array of gains/losses over time, find the period that represents the best/worst cummulative gain." The algorithm uses the fact that the sum operator is a transitive function.

Template Parameters
ITtype using to go through the collection.
Distancefunctor type computing the distance between two elements.
Comparefunctor type.
Parameters
begin,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
indexes of the array with the maximum/minimum sum, <-1,-1> in case of error.

Definition at line 52 of file max_sub_sequence.hxx.

53  {
54  if (std::distance(begin, end) < 2)
55  return std::pair<int, int>(-1, -1);
56 
57  int minValIdx = 0;
58  std::pair<int, int> indexes(minValIdx, minValIdx);
59  auto minSum = static_cast<typename std::iterator_traits<IT>::value_type>(0);
60  auto currSum = *begin;
61  auto maxSum = *begin;
62 
63  int currentIdx = 1;
64  for (auto it = begin + 1; it != end; ++it, ++currentIdx)
65  {
66  currSum += *it;
67 
68  // keep track of the minimum sum and its first value index
69  if (Compare()(minSum, currSum))
70  {
71  minValIdx = currentIdx;
72  minSum = currSum;
73  }
74 
75  // Keeps track of the maximal sub array and its end value index
76  auto curMax = Distance()(currSum, minSum);
77  if (Compare()(curMax, maxSum))
78  {
79  indexes.first = minValIdx + ((*(begin + minValIdx) < 0) ? 1 : 0);
80  indexes.second = currentIdx;
81  maxSum = Distance()(currSum, minSum);
82  }
83  }
84 
85  return indexes;
86  }