• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Generic utils similar to those from the C++ header <algorithm>.
18 
19 #ifndef LIBTEXTCLASSIFIER_COMMON_ALGORITHM_H_
20 #define LIBTEXTCLASSIFIER_COMMON_ALGORITHM_H_
21 
22 #include <algorithm>
23 #include <vector>
24 
25 namespace libtextclassifier {
26 namespace nlp_core {
27 
28 // Returns index of max element from the vector |elements|.  Returns 0 if
29 // |elements| is empty.  T should be a type that can be compared by operator<.
30 template<typename T>
GetArgMax(const std::vector<T> & elements)31 inline int GetArgMax(const std::vector<T> &elements) {
32   return std::distance(
33       elements.begin(),
34       std::max_element(elements.begin(), elements.end()));
35 }
36 
37 // Returns index of min element from the vector |elements|.  Returns 0 if
38 // |elements| is empty.  T should be a type that can be compared by operator<.
39 template<typename T>
GetArgMin(const std::vector<T> & elements)40 inline int GetArgMin(const std::vector<T> &elements) {
41   return std::distance(
42       elements.begin(),
43       std::min_element(elements.begin(), elements.end()));
44 }
45 
46 }  // namespace nlp_core
47 }  // namespace libtextclassifier
48 
49 #endif  // LIBTEXTCLASSIFIER_COMMON_ALGORITHM_H_
50