1 /* 2 * Copyright (c) 2022 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 #include "bundle_active_binary_search.h" 17 18 namespace OHOS { 19 namespace DeviceUsageStats { BundleActiveBinarySearch()20BundleActiveBinarySearch::BundleActiveBinarySearch() 21 { 22 } 23 ~BundleActiveBinarySearch()24BundleActiveBinarySearch::~BundleActiveBinarySearch() 25 { 26 } 27 BinarySearch(const std::vector<int64_t> & tableNameArray,int64_t targetValue)28int32_t BundleActiveBinarySearch::BinarySearch(const std::vector<int64_t> &tableNameArray, int64_t targetValue) 29 { 30 int32_t low = 0; 31 int32_t high = static_cast<int32_t>(tableNameArray.size() - 1); 32 int32_t divider = 2; 33 while (low <= high) { 34 int32_t mid = (low + high) / divider; 35 int64_t midValue = tableNameArray.at(mid); 36 if (midValue < targetValue) { 37 low = mid + 1; 38 } else if (midValue > targetValue) { 39 high = mid - 1; 40 } else { 41 return mid; 42 } 43 } 44 return -1; 45 } 46 } // namespace DeviceUsageStats 47 } // namespace OHOS 48 49