max_distance.hxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * SHA - Simple Hybesis Algorithms
4  *
5  * Copyright (c) Michael Jeulin-Lagarrigue
6  *
7  * Licensed under the MIT License, you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * https://github.com/michael-jeulinl/Simple-Hybesis-Algorithms/blob/master/LICENSE
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License is
13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and limitations under the License.
15  *
16  * The above copyright notice and this permission notice shall be included in all copies or
17  * substantial portions of the Software.
18  *
19  *=========================================================================================================*/
20 #ifndef MODULE_SEARCH_MAX_DISTANCE_HXX
21 #define MODULE_SEARCH_MAX_DISTANCE_HXX
22 
23 // STD includes
24 #include <iterator>
25 #include <utility>
26 
27 namespace SHA_Search
28 {
47  template <typename IT, typename Distance = std::minus<typename std::iterator_traits<IT>::value_type>>
48  std::pair<int, int> MaxDistance(const IT& begin, const IT& end)
49  {
50  if (std::distance(begin, end) < 2)
51  return std::pair<int, int>(-1, -1);
52 
53  int minValIdx = 0;
54  std::pair<int, int> indexes(minValIdx, 1);
55  auto maxDist = Distance()(*begin, *(begin + 1));
56 
57  for (auto it = begin + 1; it != end; ++it)
58  {
59  const auto currentIdx = static_cast<const int>(std::distance(begin, it));
60 
61  // Keeps track of the minimum value index
62  if (*it < *(begin + minValIdx))
63  minValIdx = currentIdx;
64 
65  // Keeps track of the largest distance and the indexes
66  const auto distance = Distance()(*it, *(begin + minValIdx));
67  if (distance > maxDist)
68  {
69  maxDist = distance;
70  indexes.first = minValIdx;
71  indexes.second = currentIdx;
72  }
73  }
74 
75  return indexes;
76  }
77 };
78 
79 #endif // MODULE_COLLECTIONS_SEARCH_HXX
std::pair< int, int > MaxDistance(const IT &begin, const IT &end)