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_event_list.h" 17 18 namespace OHOS { 19 namespace DeviceUsageStats { BundleActiveEventList()20BundleActiveEventList::BundleActiveEventList() 21 { 22 } 23 Size()24int32_t BundleActiveEventList::Size() 25 { 26 return events_.size(); 27 } 28 Clear()29void BundleActiveEventList::Clear() 30 { 31 events_.clear(); 32 std::vector<BundleActiveEvent>().swap(events_); 33 } 34 Insert(BundleActiveEvent event)35void BundleActiveEventList::Insert(BundleActiveEvent event) 36 { 37 uint32_t size = events_.size(); 38 if (size == 0 || event.timeStamp_ >= events_.back().timeStamp_) { 39 events_.push_back(event); 40 return; 41 } 42 int32_t insertIdx = FindBestIndex(event.timeStamp_); 43 events_.insert(events_.begin() + insertIdx, event); 44 } 45 FindBestIndex(const int64_t timeStamp)46int32_t BundleActiveEventList::FindBestIndex(const int64_t timeStamp) 47 { 48 int32_t size = static_cast<int32_t>(events_.size()); 49 int32_t result = size; 50 int32_t lo = 0; 51 int32_t hi = size - 1; 52 while (lo <= hi) { 53 int32_t mid = (hi - lo) / 2 + lo; 54 int64_t midTimeStamp = events_[mid].timeStamp_; 55 if (midTimeStamp >= timeStamp) { 56 hi = mid - 1; 57 result = mid; 58 } else { 59 lo = mid + 1; 60 } 61 } 62 return result; 63 } 64 Merge(const BundleActiveEventList & right)65void BundleActiveEventList::Merge(const BundleActiveEventList& right) 66 { 67 int32_t size = static_cast<int32_t>(right.events_.size()); 68 for (int32_t i = 0; i < size; i++) { 69 Insert(right.events_[i]); 70 } 71 } 72 } // namespace DeviceUsageStats 73 } // namespace OHOS 74 75