1 /*
2 * Copyright (c) 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 "ring_array.h"
17
18 namespace OHOS {
19 namespace Rosen {
20
21 using namespace std;
22 const int DEFAULT_CAPABILITY = 101;
RingArray()23 RingArray::RingArray()
24 : arrayLen_(0),
25 arrayCapability_(DEFAULT_CAPABILITY),
26 arrayHead_(0),
27 arrayTail_(0)
28 {
29 Init();
30 }
31
Init()32 void RingArray::Init()
33 {
34 ringArray_ = new std::variant<int64_t, float, bool>[arrayCapability_];
35 }
36
RingArray(int arrayCapability)37 RingArray::RingArray(int arrayCapability)
38 : arrayLen_(0),
39 arrayCapability_(arrayCapability + 1),
40 arrayHead_(0),
41 arrayTail_(0)
42 {
43 Init();
44 }
45
~RingArray()46 RingArray::~RingArray()
47 {
48 delete[] ringArray_;
49 ringArray_ = nullptr;
50 }
51
ClearArray()52 void RingArray::ClearArray()
53 {
54 arrayHead_ = 0;
55 arrayTail_ = 0;
56 arrayLen_ = 0;
57 }
58
IsArrayEmpty() const59 bool RingArray::IsArrayEmpty() const
60 {
61 if (arrayLen_ == 0) {
62 return true;
63 }
64 return false;
65 }
66
IsArrayFull() const67 bool RingArray::IsArrayFull() const
68 {
69 if (arrayLen_ == arrayCapability_ - 1) {
70 return true;
71 }
72 return false;
73 }
74
ArraySize() const75 int RingArray::ArraySize() const
76 {
77 return arrayLen_;
78 }
79
ArrayCapability() const80 int RingArray::ArrayCapability() const
81 {
82 return arrayCapability_ - 1;
83 }
84
PushElement(std::variant<int64_t,float,bool> element)85 void RingArray::PushElement(std::variant<int64_t, float, bool> element)
86 {
87 if (IsArrayFull()) {
88 arrayHead_++;
89 arrayHead_ %= arrayCapability_;
90 arrayLen_--;
91 }
92 ringArray_[arrayTail_] = element;
93 arrayTail_++;
94 arrayTail_ %= arrayCapability_;
95 arrayLen_++;
96 }
97
PopElement(std::variant<int64_t,float,bool> element)98 bool RingArray::PopElement(std::variant<int64_t, float, bool> element)
99 {
100 if (IsArrayEmpty()) {
101 return false;
102 } else {
103 element = ringArray_[arrayHead_];
104 arrayHead_++;
105 arrayHead_ %= arrayCapability_;
106 arrayLen_--;
107 }
108 return true;
109 }
110
GetElement(const int & pos) const111 std::variant<int64_t, float, bool> RingArray::GetElement(const int& pos) const
112 {
113 return ringArray_[(arrayHead_ + pos) % arrayCapability_];
114 }
115
GetElementR(const int & pos) const116 std::variant<int64_t, float, bool> RingArray::GetElementR(const int& pos) const
117 {
118 return ringArray_[(arrayHead_ + (arrayLen_ - pos) - 1) % arrayCapability_];
119 }
120
GetHeadElement() const121 std::variant<int64_t, float, bool> RingArray::GetHeadElement() const
122 {
123 return ringArray_[arrayHead_];
124 }
125
GetTailElement() const126 std::variant<int64_t, float, bool> RingArray::GetTailElement() const
127 {
128 return ringArray_[(arrayHead_ + arrayLen_ - 1) % arrayCapability_];
129 }
130
131 } // namespace Rosen
132 } // namespace OHOS