1 /* 2 * Copyright (c) 2020 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 /** 17 * @addtogroup Graphic 18 * @{ 19 * 20 * @brief Defines a lightweight graphics system that provides basic UI and container views, 21 * including buttons, images, labels, lists, animators, scroll views, swipe views, and layouts. 22 * This system also provides the Design for X (DFX) capability to implement features such as 23 * view rendering, animation, and input event distribution. 24 * 25 * @since 1.0 26 * @version 1.0 27 */ 28 29 #ifndef GRAPHIC_LITE_VECTOR_H 30 #define GRAPHIC_LITE_VECTOR_H 31 #include "gfx_utils/diagram/common/common_basics.h" 32 #include "gfx_utils/graphic_log.h" 33 #include "gfx_utils/heap_base.h" 34 35 namespace OHOS { 36 namespace Graphic { 37 template<typename T> class Vector : public HeapBase { 38 public: 39 Vector(uint16_t capacity = 1) : capacity_(capacity) 40 { 41 array_ = new T[capacity]; 42 } 43 Vector(const Vector<T> & value)44 Vector(const Vector<T>& value) 45 { 46 capacity_ = value.Capacity(); 47 array_ = new T[capacity_]; 48 size_ = value.size_; 49 if (value.array_ != nullptr) { 50 for (uint16_t i = 0; i < value.size_; i++) { 51 array_[i] = value.array_[i]; 52 } 53 } 54 } 55 ~Vector()56 virtual ~Vector() 57 { 58 delete[] array_; 59 } 60 Front()61 T& Front() 62 { 63 return array_[0]; // undefined operation while vector is empty 64 } 65 Back()66 T& Back() 67 { 68 return array_[size_ - 1]; // undefined operation while vector is empty 69 } 70 PushBack(const T & data)71 void PushBack(const T& data) 72 { 73 if (size_ == capacity_) { 74 capacity_ <<= 1; 75 T* array = new T[capacity_]; 76 for (uint16_t i = 0; i < size_; i++) { 77 array[i] = array_[i]; 78 } 79 delete[] array_; 80 array_ = array; 81 } 82 array_[size_++] = data; 83 } 84 PopBack()85 void PopBack() 86 { 87 if (IsEmpty()) { 88 return; 89 } 90 --size_; 91 } 92 Clear()93 void Clear() 94 { 95 size_ = 0; 96 } 97 Begin()98 T* Begin() const 99 { 100 return array_; 101 } 102 End()103 const T* End() const 104 { 105 return (array_ + size_); 106 } 107 IsEmpty()108 bool IsEmpty() const 109 { 110 return (size_ == 0); 111 } 112 Size()113 uint16_t Size() const 114 { 115 return size_; 116 } 117 Capacity()118 uint16_t Capacity() const 119 { 120 return capacity_; 121 } 122 ReSize(uint16_t size)123 uint16_t ReSize(uint16_t size) 124 { 125 if (size <= capacity_) { 126 size_ = size; 127 } 128 return size_; 129 } 130 Erase(uint16_t index)131 void Erase(uint16_t index) 132 { 133 if (index >= size_) { 134 return; 135 } 136 size_--; 137 for (; index < size_; index++) { 138 array_[index] = array_[index + 1]; 139 } 140 } 141 Swap(Vector<T> & other)142 void Swap(Vector<T>& other) 143 { 144 uint16_t size = size_; 145 size_ = other.size_; 146 other.size_ = size; 147 148 uint16_t capacity = capacity_; 149 capacity_ = other.capacity_; 150 other.capacity_ = capacity; 151 152 T* array = array_; 153 array_ = other.array_; 154 other.array_ = array; 155 } 156 157 T& operator[](uint16_t index) 158 { 159 return array_[index]; 160 } 161 162 void operator=(const Vector<T>& value) 163 { 164 if (capacity_ < value.Size()) { 165 delete[] array_; 166 capacity_ = value.capacity_; 167 array_ = new T[capacity_]; 168 } 169 if (value.array_ != nullptr) { 170 for (uint16_t i = 0; i < value.size_; i++) { 171 array_[i] = value.array_[i]; 172 } 173 size_ = value.size_; 174 } 175 } 176 177 protected: 178 uint16_t size_ = 0; 179 uint16_t capacity_ = 0; 180 T* array_ = nullptr; 181 uint16_t head = 0; 182 uint16_t tail = 0; 183 }; 184 } // namespace Graphic 185 } // namespace OHOS 186 #endif // GRAPHIC_LITE_VECTOR_H 187