Debian Codegraph @

thirdparty/thorvg/src/common/tvgArray.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #ifndef _TVG_ARRAY_H_
24 #define _TVG_ARRAY_H_
25
26 #include <memory.h>
27 #include <cstdint>
28 #include <cstdlib>
29
30 namespace tvg
31 {
32
33 template<class T>
34 struct Array
35 {
36 T* data = nullptr;
37 uint32_t count = 0;
38 uint32_t reserved = 0;
39
40 Array(){}
41
42 Array(int32_t size)
43 {
44 reserve(size);
45 }
46
47 Array(const Array& rhs)
48 {
49 reset();
50 *this = rhs;
51 }
52
53 void push(T element)
54 {
55 if (count + 1 > reserved) {
56 reserved = count + (count + 2) / 2;
57 data = static_cast<T*>(realloc(data, sizeof(T) * reserved));
58 }
59 data[count++] = element;
60 }
61
62 void push(const Array<T>& rhs)
63 {
64 if (rhs.count == 0) return;
65 grow(rhs.count);
66 memcpy(data + count, rhs.data, rhs.count * sizeof(T));
67 count += rhs.count;
68 }
69
70 bool reserve(uint32_t size)
71 {
72 if (size > reserved) {
73 reserved = size;
74 data = static_cast<T*>(realloc(data, sizeof(T) * reserved));
75 }
76 return true;
77 }
78
79 bool grow(uint32_t size)
80 {
81 return reserve(count + size);
82 }
83
84 const T& operator[](size_t idx) const
85 {
86 return data[idx];
87 }
88
89 T& operator[](size_t idx)
90 {
91 return data[idx];
92 }
93
94 const T* begin() const
95 {
96 return data;
97 }
98
99 T* begin()
100 {
101 return data;
102 }
103
104 T* end()
105 {
106 return data + count;
107 }
108
109 const T* end() const
110 {
111 return data + count;
112 }
113
114 const T& last() const
115 {
116 return data[count - 1];
117 }
118
119 const T& first() const
120 {
121 return data[0];
122 }
123
124 T& last()
125 {
126 return data[count - 1];
127 }
128
129 T& first()
130 {
131 return data[0];
132 }
133
134 void pop()
135 {
136 if (count > 0) --count;
137 }
138
139 void reset()
140 {
141 free(data);
142 data = nullptr;
143 count = reserved = 0;
144 }
145
146 void clear()
147 {
148 count = 0;
149 }
150
151 bool empty() const
152 {
153 return count == 0;
154 }
155
156 template<class COMPARE>
157 void sort()
158 {
159 qsort<COMPARE>(data, 0, static_cast<int32_t>(count) - 1);
160 }
161
162 void operator=(const Array& rhs)
163 {
164 reserve(rhs.count);
165 if (rhs.count > 0) memcpy(data, rhs.data, sizeof(T) * rhs.count);
166 count = rhs.count;
167 }
168
169 ~Array()
170 {
171 free(data);
172 }
173
174 private:
175 template<class COMPARE>
176 void qsort(T* arr, int32_t low, int32_t high)
177 {
178 if (low < high) {
179 int32_t i = low;
180 int32_t j = high;
181 T tmp = arr[low];
182 while (i < j) {
183 while (i < j && !COMPARE{}(arr[j], tmp)) --j;
184 if (i < j) {
185 arr[i] = arr[j];
186 ++i;
187 }
188 while (i < j && COMPARE{}(arr[i], tmp)) ++i;
189 if (i < j) {
190 arr[j] = arr[i];
191 --j;
192 }
193 }
194 arr[i] = tmp;
195 qsort<COMPARE>(arr, low, i - 1);
196 qsort<COMPARE>(arr, i + 1, high);
197 }
198 }
199 };
200
201 }
202
203 #endif //_TVG_ARRAY_H_