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