max_distance.hxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * HUC - Hurna Core
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/Hurna/Hurna-Core/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 huc
28 {
29  namespace search
30  {
46  template <typename IT, typename Distance = std::minus<typename std::iterator_traits<IT>::value_type>>
47  std::pair<int, int> MaxDistance(const IT& begin, const IT& end)
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  }
76  }
77 }
78 
79 #endif // MODULE_COLLECTIONS_SEARCH_HXX
std::pair< int, int > MaxDistance(const IT &begin, const IT &end)