1 /*
2 * Copyright (c) 2024-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 #include "video_job_queue.h"
17
18 namespace OHOS {
19 namespace CameraStandard {
20 namespace DeferredProcessing {
VideoJobQueue(Comparator comp)21 VideoJobQueue::VideoJobQueue(Comparator comp)
22 : comp_(comp)
23 {
24 DP_DEBUG_LOG("entered.");
25 }
26
~VideoJobQueue()27 VideoJobQueue::~VideoJobQueue()
28 {
29 DP_DEBUG_LOG("entered.");
30 comp_ = nullptr;
31 }
32
Contains(DeferredVideoJobPtr obj) const33 bool VideoJobQueue::Contains(DeferredVideoJobPtr obj) const
34 {
35 return indexMap_.find(obj) != indexMap_.end();
36 }
37
Peek() const38 DeferredVideoJobPtr VideoJobQueue::Peek() const
39 {
40 DP_CHECK_RETURN_RET(size_ == DEFAULT, nullptr);
41 return heap_.front();
42 }
43
Push(DeferredVideoJobPtr obj)44 void VideoJobQueue::Push(DeferredVideoJobPtr obj)
45 {
46 heap_.push_back(obj);
47 indexMap_[obj] = size_;
48 HeapInsert(size_++);
49 }
50
Pop()51 DeferredVideoJobPtr VideoJobQueue::Pop()
52 {
53 DP_CHECK_RETURN_RET(size_ == DEFAULT, nullptr);
54 DeferredVideoJobPtr ans = heap_.front();
55 Swap(DEFAULT, size_ - 1);
56 indexMap_.erase(ans);
57 heap_.pop_back();
58 --size_;
59 Heapify(DEFAULT);
60 return ans;
61 }
62
Remove(DeferredVideoJobPtr obj)63 void VideoJobQueue::Remove(DeferredVideoJobPtr obj)
64 {
65 auto it = indexMap_.find(obj);
66 DP_CHECK_RETURN(it == indexMap_.end());
67 // LCOV_EXCL_START
68
69 uint32_t index = it->second;
70 DeferredVideoJobPtr replace = heap_.back();
71 indexMap_.erase(obj);
72 heap_.pop_back();
73 --size_;
74 if (obj == replace) {
75 return;
76 }
77 heap_[index] = replace;
78 indexMap_[replace] = index;
79 Update(replace);
80 // LCOV_EXCL_STOP
81 }
82
83 // LCOV_EXCL_START
Update(DeferredVideoJobPtr obj)84 void VideoJobQueue::Update(DeferredVideoJobPtr obj)
85 {
86 uint32_t index = indexMap_[obj];
87 HeapInsert(index);
88 Heapify(index);
89 }
90 // LCOV_EXCL_STOP
91
GetAllElements() const92 std::vector<DeferredVideoJobPtr> VideoJobQueue::GetAllElements() const
93 {
94 return heap_;
95 }
96
HeapInsert(uint32_t index)97 void VideoJobQueue::HeapInsert(uint32_t index)
98 {
99 while (index > 0 && comp_(heap_[index], heap_[(index - 1) >> 1])) {
100 Swap(index, (index - 1) >> 1);
101 index = (index - 1) >> 1;
102 }
103 }
104
Heapify(uint32_t index)105 void VideoJobQueue::Heapify(uint32_t index)
106 {
107 uint32_t left = (index << 1) + 1;
108 while (left < size_) {
109 // LCOV_EXCL_START
110 uint32_t best = (left + 1 < size_ && comp_(heap_[left + 1], heap_[left])) ? left + 1 : left;
111 best = comp_(heap_[best], heap_[index]) ? best : index;
112 if (best == index) {
113 break;
114 }
115 Swap(best, index);
116 index = best;
117 left = (index << 1) + 1;
118 // LCOV_EXCL_STOP
119 }
120 }
121
Swap(uint32_t x,uint32_t y)122 void VideoJobQueue::Swap(uint32_t x, uint32_t y)
123 {
124 DP_CHECK_ERROR_RETURN_LOG(x < DEFAULT || x >= size_ || y < DEFAULT || y >= size_, "swap failed.");
125 std::swap(heap_[x], heap_[y]);
126 auto item = indexMap_.find(heap_[x]);
127 if (item != indexMap_.end()) {
128 indexMap_[heap_[x]] = x;
129 }
130 item = indexMap_.find(heap_[y]);
131 if (item != indexMap_.end()) {
132 indexMap_[heap_[y]] = y;
133 }
134 }
135
Clear()136 void VideoJobQueue::Clear()
137 {
138 heap_.clear();
139 indexMap_.clear();
140 }
141 } // namespace DeferredProcessing
142 } // namespace CameraStandard
143 } // namespace OHOS