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