1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef META_BASE_ALGORITHMS_H 17 #define META_BASE_ALGORITHMS_H 18 19 #include <stdint.h> 20 21 #include <base/containers/type_traits.h> 22 #include <base/containers/vector.h> 23 24 #include <meta/base/namespace.h> 25 26 META_BEGIN_NAMESPACE() 27 28 constexpr size_t NPOS = -1; 29 30 /** 31 * @brief Find index of first matching value in vector or NPOS if no such value 32 * @param vec Vector to search 33 * @param value Value to search 34 * @param pos Index to start the search 35 * @return First index where is 'value' or NPOS 36 */ 37 template<typename Type> 38 size_t FindFirstOf(const BASE_NS::vector<Type>& vec, const Type& value, size_t pos = 0) 39 { 40 for (; pos < vec.size(); ++pos) { 41 if (vec[pos] == value) { 42 return pos; 43 } 44 } 45 return NPOS; 46 } 47 48 META_END_NAMESPACE() 49 50 #endif 51