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/heap_base.h" 32 #include <cstdint> 33 34 namespace OHOS { 35 namespace Graphic { 36 template<typename T> class Vector : public HeapBase { 37 public: capacity_(capacity)38 Vector(uint16_t capacity = 1) : capacity_(capacity) 39 { 40 array_ = new T[capacity]; 41 } 42 Vector(const Vector<T> & value)43 Vector(const Vector<T>& value) 44 { 45 capacity_ = value.Capacity(); 46 array_ = new T[capacity_]; 47 size_ = value.size_; 48 if (value.array_ != nullptr) { 49 for (uint16_t i = 0; i < value.size_; i++) { 50 array_[i] = value.array_[i]; 51 } 52 } 53 } 54 ~Vector()55 virtual ~Vector() 56 { 57 delete[] array_; 58 } 59 Front()60 T& Front() 61 { 62 return array_[0]; // undefined operation while vector is empty 63 } 64 Back()65 T& Back() 66 { 67 return array_[size_ - 1]; // undefined operation while vector is empty 68 } 69 PushBack(const T & data)70 void PushBack(const T& data) 71 { 72 if (size_ == capacity_) { 73 capacity_ <<= 1; 74 T* array = new T[capacity_]; 75 for (uint16_t i = 0; i < size_; i++) { 76 array[i] = array_[i]; 77 } 78 delete[] array_; 79 array_ = array; 80 } 81 array_[size_++] = data; 82 } 83 PopBack()84 void PopBack() 85 { 86 if (IsEmpty()) { 87 return; 88 } 89 --size_; 90 } 91 Clear()92 void Clear() 93 { 94 size_ = 0; 95 } 96 Begin()97 T* Begin() const 98 { 99 return array_; 100 } 101 End()102 const T* End() const 103 { 104 return (array_ + size_); 105 } 106 IsEmpty()107 bool IsEmpty() const 108 { 109 return (size_ == 0); 110 } 111 Size()112 uint16_t Size() const 113 { 114 return size_; 115 } 116 Capacity()117 uint16_t Capacity() const 118 { 119 return capacity_; 120 } 121 ReSize(uint16_t size)122 uint16_t ReSize(uint16_t size) 123 { 124 if (size <= capacity_) { 125 size_ = size; 126 } 127 return size_; 128 } 129 Erase(uint16_t index)130 void Erase(uint16_t index) 131 { 132 if (index >= size_) { 133 return; 134 } 135 size_--; 136 for (; index < size_; index++) { 137 array_[index] = array_[index + 1]; 138 } 139 } 140 Swap(Vector<T> & other)141 void Swap(Vector<T>& other) 142 { 143 uint16_t size = size_; 144 size_ = other.size_; 145 other.size_ = size; 146 147 uint16_t capacity = capacity_; 148 capacity_ = other.capacity_; 149 other.capacity_ = capacity; 150 151 T* array = array_; 152 array_ = other.array_; 153 other.array_ = array; 154 } 155 156 T& operator[](uint16_t index) 157 { 158 return array_[index]; 159 } 160 161 void operator=(const Vector<T>& value) 162 { 163 if (capacity_ < value.Size()) { 164 delete[] array_; 165 capacity_ = value.capacity_; 166 array_ = new T[capacity_]; 167 } 168 if (value.array_ != nullptr) { 169 for (uint16_t i = 0; i < value.size_; i++) { 170 array_[i] = value.array_[i]; 171 } 172 size_ = value.size_; 173 } 174 } 175 176 protected: 177 uint16_t size_ = 0; 178 uint16_t capacity_ = 0; 179 T* array_ = nullptr; 180 uint16_t head = 0; 181 uint16_t tail = 0; 182 }; 183 } // namespace Graphic 184 } // namespace OHOS 185 #endif // GRAPHIC_LITE_VECTOR_H