Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
minmax.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file utility/minmax.hh
11 /// @author Christopher Miles (cmiles@uw.edu)
12 
13 #ifndef INCLUDED_UTILITY_MINMAX_HH
14 #define INCLUDED_UTILITY_MINMAX_HH
15 
16 // C/C++ headers
17 #include <vector>
18 
19 // Package headers
20 #include <utility/vector1.hh>
21 
22 namespace utility {
23 
24 // -- argmin -- //
25 
26 /// @brief Returns the argument whose value is minimal according to operator<.
27 /// @details Adheres to STL numbering (0-indexed).
28 template <typename T>
29 int argmin(const std::vector<T>& iterable) {
30  if ( iterable.size() == 0 ) {
31  return -1;
32  }
33 
34  int min_idx = 0;
35  for ( std::size_t curr_idx = 1; curr_idx < iterable.size(); ++curr_idx ) {
36  if ( iterable[curr_idx] < iterable[min_idx] ) {
37  min_idx = curr_idx;
38  }
39  }
40 
41  return min_idx;
42 }
43 
44 /// @brief Returns the argument whose value is minimal according to operator<.
45 /// @details Adheres to Rosetta numbering (1-indexed).
46 template <typename T>
47 int argmin(const utility::vector1<T>& iterable) {
48  if ( iterable.size() == 0 ) {
49  return 0;
50  }
51 
52  int min_idx = 1;
53  for ( std::size_t curr_idx = 2; curr_idx <= iterable.size(); ++curr_idx ) {
54  if ( iterable[curr_idx] < iterable[min_idx] ) {
55  min_idx = curr_idx;
56  }
57  }
58 
59  return min_idx;
60 }
61 
62 // -- argmax -- //
63 
64 /// @brief Returns the argument whose value is maximal according to operator>.
65 /// @details Adheres to STL numbering (0-indexed).
66 template <typename T>
67 int argmax(const std::vector<T>& iterable) {
68  if ( iterable.size() == 0 ) {
69  return -1;
70  }
71 
72  int max_idx = 0;
73  for ( std::size_t curr_idx = 1; curr_idx < iterable.size(); ++curr_idx ) {
74  if ( iterable[curr_idx] > iterable[max_idx] ) {
75  max_idx = curr_idx;
76  }
77  }
78 
79  return max_idx;
80 }
81 
82 /// @brief Returns the argument whose value is maximal according to operator>.
83 /// @details Adheres to Rosetta numbering (1-indexed).
84 template <typename T>
85 int argmax(const utility::vector1<T>& iterable) {
86  if ( iterable.size() == 0 ) {
87  return 0;
88  }
89 
90  int max_idx = 1;
91  for ( std::size_t curr_idx = 2; curr_idx <= iterable.size(); ++curr_idx ) {
92  if ( iterable[curr_idx] > iterable[max_idx] ) {
93  max_idx = curr_idx;
94  }
95  }
96 
97  return max_idx;
98 }
99 
100 } // namespace utility
101 
102 #endif // INCLUDED_UTILITY_MINMAX_HH
int argmax(const std::vector< T > &iterable)
Returns the argument whose value is maximal according to operator>.
Definition: minmax.hh:67
int argmin(const std::vector< T > &iterable)
Returns the argument whose value is minimal according to operator<.
Definition: minmax.hh:29
vector1: std::vector with 1-based indexing