1 /*
2 * Copyright (c) 2023-2023 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
68 uint32_t index = it->second;
69 DeferredVideoJobPtr replace = heap_.back();
70 indexMap_.erase(obj);
71 heap_.pop_back();
72 --size_;
73 if (obj == replace) {
74 return;
75 }
76 heap_[index] = replace;
77 indexMap_[replace] = index;
78 Update(replace);
79 }
80
Update(DeferredVideoJobPtr obj)81 void VideoJobQueue::Update(DeferredVideoJobPtr obj)
82 {
83 uint32_t index = indexMap_[obj];
84 HeapInsert(index);
85 Heapify(index);
86 }
87
GetAllElements() const88 std::vector<DeferredVideoJobPtr> VideoJobQueue::GetAllElements() const
89 {
90 return heap_;
91 }
92
HeapInsert(uint32_t index)93 void VideoJobQueue::HeapInsert(uint32_t index)
94 {
95 while (index > 0 && comp_(heap_[index], heap_[(index - 1) >> 1])) {
96 Swap(index, (index - 1) >> 1);
97 index = (index - 1) >> 1;
98 }
99 }
100
Heapify(uint32_t index)101 void VideoJobQueue::Heapify(uint32_t index)
102 {
103 uint32_t left = (index << 1) + 1;
104 while (left < size_) {
105 uint32_t best = (left + 1 < size_ && comp_(heap_[left + 1], heap_[left])) ? left + 1 : left;
106 best = comp_(heap_[best], heap_[index]) ? best : index;
107 if (best == index) {
108 break;
109 }
110 Swap(best, index);
111 index = best;
112 left = (index << 1) + 1;
113 }
114 }
115
Swap(uint32_t x,uint32_t y)116 void VideoJobQueue::Swap(uint32_t x, uint32_t y)
117 {
118 DP_CHECK_ERROR_RETURN_LOG(x < DEFAULT || x >= size_ || y < DEFAULT || y >= size_, "swap failed.");
119 std::swap(heap_[x], heap_[y]);
120 auto item = indexMap_.find(heap_[x]);
121 if (item != indexMap_.end()) {
122 indexMap_[heap_[x]] = x;
123 }
124 item = indexMap_.find(heap_[y]);
125 if (item != indexMap_.end()) {
126 indexMap_[heap_[y]] = y;
127 }
128 }
129
Clear()130 void VideoJobQueue::Clear()
131 {
132 heap_.clear();
133 indexMap_.clear();
134 }
135 } // namespace DeferredProcessing
136 } // namespace CameraStandard
137 } // namespace OHOS