Mako 8.2.0 API
MakoCore SDK API Documentation
Loading...
Searching...
No Matches
edlvector.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008-2025 Global Graphics Software Ltd. All rights reserved.
3 */
4
10
11#ifndef edlvector_h
12#define edlvector_h
13
14#include <vector>
15#include <initializer_list>
16
17#include <edl/edltypes.h>
18#include <edl/edlnamespaces.h>
19#include <edl/edlallocator.h>
20
22
24
25template <class T, uint32_t STATICBUFFERLEN> class CEDLVectorIterator;
26template <class T, uint32_t STATICBUFFERLEN> class CEDLVectorConstIterator;
27
28template <class T, uint32_t STATICBUFFERLEN = 1>
30{
31public:
34
35
36 // Copy constructor
37 CEDLVector(const CEDLVector& incoming)
38 {
39 init();
40 assign(incoming);
41 }
42
43 CEDLVector(const std::vector<T>& incoming)
44 {
45 init();
46 assign(incoming);
47 }
48
49 CEDLVector(const std::initializer_list<T>& incoming)
50 {
51 init();
52 assign(incoming);
53 }
54
55 // Allocate an empty vector
56 CEDLVector(uint32_t size = 0)
57 {
58 init();
59 if (size > 0)
60 {
61 resize(size);
62 }
63 }
64
65 CEDLVector(uint32_t size, const T values[])
66 {
67 init();
68 resize(size);
69 for (uint32_t i = 0; i < size; i++)
70 {
71 m_vector[i] = values[i];
72 }
73 }
74
75 virtual ~CEDLVector()
76 {
77 clear();
78 }
79
80 void clear()
81 {
82 resizeVector(0);
83 m_vectorSize = 0;
84 }
85
86 void reserve(uint32_t reserveSize)
87 {
88 if (reserveSize > m_reservedSize)
89 {
90 resizeVector(reserveSize);
91 }
92 }
93
94 // Return a std::vector equivalent
95 std::vector<T> toVector()
96 {
97 std::vector<T> vect;
98
99 vect.reserve(m_vectorSize);
100 for (uint32_t i = 0; i < m_vectorSize; i++)
101 vect.push_back(m_vector[i]);
102 return vect;
103 }
104
105 // Get size
106 uint32_t size() const
107 {
108 return m_vectorSize;
109 }
110
111 bool empty() const
112 {
113 return m_vectorSize == 0;
114 }
115
116
117 // Dereference via index
118 T& operator[](uint32_t index)
119 {
120 if (index >= m_vectorSize)
122
123 return m_vector[index];
124 }
125
126 bool operator== (const CEDLVector& other) const
127 {
128 if (m_vectorSize != other.m_vectorSize)
129 return false;
130
131 for (uint32_t i = 0; i < m_vectorSize; i++)
132 {
133 if (m_vector[i] != other.m_vector[i])
134 return false;
135 }
136 return true;
137 }
138
139 bool operator!= (const CEDLVector& other) const
140 {
141 return !(*this == other);
142 }
143
144 T& operator[](uint32_t index) const
145 {
146 if (index >= m_vectorSize)
148
149 return m_vector[index];
150 }
151
152 T& last()
153 {
154 if (empty())
156
157 return m_vector[m_vectorSize - 1];
158 }
159
160 T& last() const
161 {
162 if (empty())
164
165 return m_vector[m_vectorSize - 1];
166 }
167
169 {
170 assign(incoming);
171 return *this;
172 }
173
174 void reverse()
175 {
176 if (!m_vectorSize)
177 return;
178
179 for (uint32_t i = 0, x = m_vectorSize - 1; i < x; ++i, --x)
180 {
181 T cache = m_vector[i];
182 m_vector[i] = m_vector[x];
183 m_vector[x] = cache;
184 }
185 }
186
187 void resize(uint32_t newSize)
188 {
189 if (newSize == m_vectorSize)
190 return;
191
192 if (newSize == 0 && m_vector)
193 {
194 clear();
195 }
196 else if (newSize > m_vectorSize && newSize <= m_reservedSize && m_vector)
197 {
198 for (uint32_t i = m_vectorSize; i < newSize; i++)
199 new (m_vector + i) T();
200 }
201 else if (newSize < m_vectorSize && m_vector)
202 {
203 for (uint32_t i = newSize; i < m_vectorSize; i++)
204 m_vector[i].~T();
205 }
206 else
207 {
208 uint32_t reserveSize = newSize;
209 if (newSize > m_vectorSize)
210 {
211 reserveSize = m_vectorSize * 3 / 2;
212 if (reserveSize < newSize)
213 reserveSize = newSize;
214 }
215
216 if (reserveSize > 0 && reserveSize < STATICBUFFERLEN)
217 {
218 reserveSize = STATICBUFFERLEN;
219 }
220
221 resizeVector(reserveSize);
222 m_reservedSize = reserveSize;
223 if (newSize > m_vectorSize)
224 {
225 for (uint32_t i = m_vectorSize; i < newSize; i++)
226 new (m_vector + i) T();
227 }
228 }
229 m_vectorSize = newSize;
230 }
231
232 void append(const T& obj)
233 {
234 resize(m_vectorSize + 1);
235 m_vector[m_vectorSize - 1] = obj;
236 }
237
238 void append(const CEDLVector& items)
239 {
240 uint32_t startPos = m_vectorSize;
241 resize(m_vectorSize + items.size());
242 for (uint32_t i = 0; i < items.size(); ++i)
243 {
244 m_vector[i + startPos] = items[i];
245 }
246 }
247
248 void insert(uint32_t index, const T& obj)
249 {
250 if (index > m_vectorSize)
252
253 if (index == m_vectorSize)
254 {
255 append(obj);
256 }
257 else
258 {
259 resize(m_vectorSize + 1);
260 for (uint32_t i = m_vectorSize - 1; i > index; i--)
261 {
262 m_vector[i] = m_vector[i - 1];
263 }
264 m_vector[index] = obj;
265 }
266 }
267
268 void insert(uint32_t index, const CEDLVector& items)
269 {
270 for (uint32_t i = 0; i < items.size(); ++i)
271 {
272 insert(index++, items[i]);
273 }
274 }
275
276 void fill(const T& obj)
277 {
278 for (uint32_t i = 0; i < m_vectorSize; i++)
279 {
280 m_vector[i] = obj;
281 }
282 }
283
284 // Erase the entry at the given index, shortening the vector by one.
285 void erase(uint32_t index)
286 {
287 if (index >= m_vectorSize)
289 for (uint32_t i = index + 1; i < m_vectorSize; i++)
290 m_vector[i - 1] = m_vector[i];
291 resize(m_vectorSize - 1);
292 }
293
294 // Find the specified entry in the collection, throwing if none found.
295 // For a non-throwing equivalent, see find() below.
296 uint32_t indexOf(const T& obj) const
297 {
298 for (uint32_t i = 0; i < m_vectorSize; i++)
299 {
300 if (m_vector[i] == obj)
301 {
302 return i;
303 }
304 }
305 throwVectorError(); // Object not found.
306 return (uint32_t)-1; // Should never be reached.
307 }
308
309 bool contains(const T& obj) const
310 {
311 for (uint32_t i = 0; i < m_vectorSize; i++)
312 {
313 if (m_vector[i] == obj)
314 {
315 return true;
316 }
317 }
318 return false;
319 }
320
321 // An iterator that allows, amongst other things, C++11 ranged for syntax
323 {
324 return Iterator(this, 0);
325 }
326
328 {
329 return Iterator(this, m_vectorSize);
330 }
331
333 {
334 return ConstIterator(this, 0);
335 }
336
338 {
339 return ConstIterator(this, m_vectorSize);
340 }
341
342 ConstIterator find(const T& obj) const
343 {
344 for (uint32_t i = 0; i < m_vectorSize; i++)
345 {
346 if (m_vector[i] == obj)
347 {
348 return ConstIterator(this, i);
349 }
350 }
351 return end();
352 }
353 Iterator find(const T& obj)
354 {
355 for (uint32_t i = 0; i < m_vectorSize; i++)
356 {
357 if (m_vector[i] == obj)
358 {
359 return Iterator(this, i);
360 }
361 }
362 return end();
363 }
364
365private:
366 void init()
367 {
368 m_vector = NULL;
369 m_vectorSize = 0;
370 m_reservedSize = 0;
371 }
372
373 void assign(const CEDLVector& incoming)
374 {
375 uint32_t newSize = incoming.size();
376
377 if (newSize != m_vectorSize)
378 {
379 resize(0);
380 if (newSize > 0)
381 resize(newSize);
382 }
383
384 for (uint32_t i = 0; i < newSize; i++)
385 {
386 m_vector[i] = incoming[i];
387 }
388 }
389
390 void assign(const std::vector<T>& incoming)
391 {
392 uint32_t newSize = (uint32_t)incoming.size();
393
394 if (newSize != m_vectorSize)
395 {
396 resize(0);
397 if (newSize > 0)
398 resize(newSize);
399 }
400
401 for (uint32_t i = 0; i < newSize; i++)
402 {
403 m_vector[i] = incoming[i];
404 }
405 }
406
407 void assign(const std::initializer_list<T>& incoming)
408 {
409 uint32_t newSize = (uint32_t)incoming.size();
410
411 if (newSize != m_vectorSize)
412 {
413 resize(0);
414 if (newSize > 0)
415 {
416 resize(newSize);
417 }
418 }
419
420 uint32_t i = 0;
421 for (T element : incoming)
422 {
423 m_vector[i] = element;
424 i++;
425 }
426 }
427
428 // Change the size of the actual storage array
429 void resizeVector(uint32_t newSize)
430 {
431 // We will impose a minimum reserved size of SBLEN
432 if (newSize > 0 && STATICBUFFERLEN > 0 && newSize < STATICBUFFERLEN)
433 {
434 newSize = STATICBUFFERLEN;
435 }
436
437 if (newSize == m_reservedSize)
438 {
439 return;
440 }
441
442 T* newVect = nullptr;
443 if (newSize > 0)
444 {
445 if (newSize == STATICBUFFERLEN)
446 {
447 newVect = (T*)m_staticVector;
448 }
449 else
450 {
451 newVect = (T*)m_allocator.allocate(sizeof(T) * newSize);
452 if (!newVect) throwVectorError();
453 }
454 uint32_t cpSz = newSize > m_vectorSize ? m_vectorSize : newSize;
455 for (uint32_t i = 0; i < cpSz; i++)
456 new (newVect + i) T(m_vector[i]);
457 }
458 if (m_vector)
459 {
460 for (uint32_t i = 0; i < m_vectorSize; i++)
461 m_vector[i].~T();
462 if (m_vector != (T*)m_staticVector)
463 {
464 m_allocator.deallocate(m_vector);
465 }
466 }
467 m_vector = newVect;
468 m_reservedSize = newSize;
469 }
470
471 T* m_vector;
472 // Ensure at least eight byte alignment for static storage
473 uint64_t m_staticVector[(sizeof(T) * STATICBUFFERLEN + 7) / 8];
474 uint32_t m_vectorSize;
475 uint32_t m_reservedSize;
476
477 EDLAllocator m_allocator;
478};
479
480
481template <class T, uint32_t STATICBUFFERLEN = 1>
483{
484 friend class CEDLVector<T, STATICBUFFERLEN>;
485 friend class CEDLVectorConstIterator<T, STATICBUFFERLEN>;
486
487public:
488 CEDLVectorIterator(CEDLVector<T, STATICBUFFERLEN>* vector = nullptr, uint32_t index = 0) : m_vector(vector), m_index(index)
489 {
490 }
491 CEDLVectorIterator(const CEDLVectorIterator& other) : m_vector(other.m_vector), m_index(other.m_index)
492 {
493 }
494
496 {
497 m_vector = incoming.m_vector;
498 m_index = incoming.m_index;
499 return *this;
500 }
501
502 bool operator!= (const CEDLVectorIterator& other) const
503 {
504 return (m_vector != other.m_vector || m_index != other.m_index);
505 }
506 bool operator== (const CEDLVectorIterator& other) const
507 {
508 return m_vector == other.m_vector && m_index == other.m_index;
509 }
511 {
512 if (!m_vector)
513 {
515 }
516 return (*m_vector)[m_index];
517 }
519 {
520 if (!m_vector)
521 {
523 }
524 return &(*m_vector)[m_index];
525 }
527 {
528 m_index++;
529 return *this;
530 }
532 {
533 CEDLVectorIterator result(*this);
534 m_index++;
535 return result;
536 }
537 // Obtain the index of the iterator
538 uint32_t getIndex() const
539 {
540 return m_index;
541 }
542
543
544 // These functions moveNext(), hasCurrentValue(), and currentValue()
545 // are for an alternate way to loop through the enumeration when
546 // you don't have the original vector object check if you've reached the end.
547 //
548 // In C++ the loop goes something like this :
549 //
550 // for (var iter = vect.begin (); iter != vect.end (); iter++)
551 // val = *iter;
552 //
553 // However if you don't have the original object you can't compare the end
554 // so an alternative loop would look like this :
555 //
556 // for (var iter = vect.begin (); iter.hasCurrentValue (); iter.moveNext ())
557 // val = iter.currentValue ();
558
559 void moveNext()
560 {
561 m_index++;
562 }
563
564 bool hasCurrentValue() const
565 {
566 return m_index < m_vector->size();
567 }
568
569 T currentValue() const
570 {
571 if (!m_vector
572 || (m_index >= m_vector->size())
573 )
574 {
576 }
577 return (*m_vector)[m_index];
578 }
579
580private:
582 uint32_t m_index;
583};
584
585template <class T, uint32_t STATICBUFFERLEN = 1>
587{
588 friend class CEDLVector<T, STATICBUFFERLEN>;
589 friend class CEDLVectorIterator<T, STATICBUFFERLEN>;
590
591public:
592 CEDLVectorConstIterator(const CEDLVector<T, STATICBUFFERLEN>* vector = nullptr, uint32_t index = 0) : m_vector(vector), m_index(index)
593 {
594 }
595
596 CEDLVectorConstIterator(const CEDLVectorConstIterator& other) : m_vector(other.m_vector), m_index(other.m_index)
597 {
598 }
599
600 CEDLVectorConstIterator(const CEDLVectorIterator<T, STATICBUFFERLEN>& other) : m_vector(other.m_vector), m_index(other.m_index)
601 {
602 }
603
605 {
606 m_vector = incoming.m_vector;
607 m_index = incoming.m_index;
608 return *this;
609 }
610
611 bool operator!= (const CEDLVectorConstIterator& other) const
612 {
613 return (m_vector != other.m_vector) || (m_index != other.m_index);
614 }
615
616 bool operator== (const CEDLVectorConstIterator& other) const
617 {
618 return (m_vector == other.m_vector) && (m_index == other.m_index);
619 }
620
621 const T& operator* () const
622 {
623 if (!m_vector)
624 {
626 }
627 return (*m_vector)[m_index];
628 }
629
630 const T* operator->() const
631 {
632 if (!m_vector)
633 {
635 }
636 return &(*m_vector)[m_index];
637 }
638
640 {
641 m_index++;
642 return *this;
643 }
644
646 {
647 CEDLVectorConstIterator result(*this);
648 m_index++;
649 return result;
650 }
651 // Obtain the index of the iterator
652 uint32_t getIndex() const
653 {
654 return m_index;
655 }
656
657 void moveNext()
658 {
659 m_index++;
660 }
661
662 bool hasCurrentValue() const
663 {
664 return m_index < m_vector->size();
665 }
666
667 T currentValue() const
668 {
669 if (!m_vector
670 || (m_index >= m_vector->size())
671 )
672 {
674 }
675 return (*m_vector)[m_index];
676 }
677
678private:
679 const CEDLVector<T, STATICBUFFERLEN>* m_vector;
680 uint32_t m_index;
681};
682
684
685#endif
686
Definition edlvector.h:587
const T & operator*() const
Definition edlvector.h:621
void moveNext()
Definition edlvector.h:657
bool operator!=(const CEDLVectorConstIterator &other) const
Definition edlvector.h:611
bool operator==(const CEDLVectorConstIterator &other) const
Definition edlvector.h:616
CEDLVectorConstIterator(const CEDLVectorConstIterator &other)
Definition edlvector.h:596
uint32_t getIndex() const
Definition edlvector.h:652
bool hasCurrentValue() const
Definition edlvector.h:662
const T * operator->() const
Definition edlvector.h:630
CEDLVectorConstIterator operator++(int)
Definition edlvector.h:645
T currentValue() const
Definition edlvector.h:667
const CEDLVectorConstIterator & operator++()
Definition edlvector.h:639
CEDLVectorConstIterator(const CEDLVectorIterator< T, STATICBUFFERLEN > &other)
Definition edlvector.h:600
CEDLVectorConstIterator(const CEDLVector< T, STATICBUFFERLEN > *vector=nullptr, uint32_t index=0)
Definition edlvector.h:592
CEDLVectorConstIterator & operator=(const CEDLVectorConstIterator &incoming)
Definition edlvector.h:604
Definition edlvector.h:30
T & operator[](uint32_t index) const
Definition edlvector.h:144
CEDLVectorConstIterator< IDOMImagePtr, 1 > ConstIterator
Definition edlvector.h:33
CEDLVector(const std::initializer_list< T > &incoming)
Definition edlvector.h:49
ConstIterator find(const T &obj) const
Definition edlvector.h:342
T & last() const
Definition edlvector.h:160
T & operator[](uint32_t index)
Definition edlvector.h:118
void append(const T &obj)
Definition edlvector.h:232
void insert(uint32_t index, const T &obj)
Definition edlvector.h:248
CEDLVector(uint32_t size, const T values[])
Definition edlvector.h:65
CEDLVector & operator=(const CEDLVector &incoming)
Definition edlvector.h:168
void reserve(uint32_t reserveSize)
Definition edlvector.h:86
ConstIterator end() const
Definition edlvector.h:337
T & last()
Definition edlvector.h:152
bool contains(const T &obj) const
Definition edlvector.h:309
bool empty() const
Definition edlvector.h:111
void insert(uint32_t index, const CEDLVector &items)
Definition edlvector.h:268
ConstIterator begin() const
Definition edlvector.h:332
void fill(const T &obj)
Definition edlvector.h:276
uint32_t size() const
Definition edlvector.h:106
Iterator find(const T &obj)
Definition edlvector.h:353
virtual ~CEDLVector()
Definition edlvector.h:75
void erase(uint32_t index)
Definition edlvector.h:285
uint32_t indexOf(const T &obj) const
Definition edlvector.h:296
Iterator begin()
Definition edlvector.h:322
Iterator end()
Definition edlvector.h:327
void append(const CEDLVector &items)
Definition edlvector.h:238
CEDLVector(const std::vector< T > &incoming)
Definition edlvector.h:43
bool operator==(const CEDLVector &other) const
Definition edlvector.h:126
CEDLVector(const CEDLVector &incoming)
Definition edlvector.h:37
void resize(uint32_t newSize)
Definition edlvector.h:187
bool operator!=(const CEDLVector &other) const
Definition edlvector.h:139
std::vector< T > toVector()
Definition edlvector.h:95
void clear()
Definition edlvector.h:80
CEDLVectorIterator< IDOMImagePtr, 1 > Iterator
Definition edlvector.h:32
void reverse()
Definition edlvector.h:174
CEDLVector(uint32_t size=0)
Definition edlvector.h:56
Definition edlvector.h:483
CEDLVectorIterator(CEDLVector< T, STATICBUFFERLEN > *vector=nullptr, uint32_t index=0)
Definition edlvector.h:488
bool hasCurrentValue() const
Definition edlvector.h:564
const CEDLVectorIterator & operator++()
Definition edlvector.h:526
CEDLVectorIterator & operator=(const CEDLVectorIterator &incoming)
Definition edlvector.h:495
uint32_t getIndex() const
Definition edlvector.h:538
const CEDLVectorIterator operator++(int)
Definition edlvector.h:531
T & operator*()
Definition edlvector.h:510
bool operator!=(const CEDLVectorIterator &other) const
Definition edlvector.h:502
bool operator==(const CEDLVectorIterator &other) const
Definition edlvector.h:506
void moveNext()
Definition edlvector.h:559
T * operator->()
Definition edlvector.h:518
CEDLVectorIterator(const CEDLVectorIterator &other)
Definition edlvector.h:491
T currentValue() const
Definition edlvector.h:569
Definition edlallocator.h:25
EDL C++ namespace(s)
#define _BEGIN_EDL_NAMESPACE
Definition edlnamespaces.h:75
#define _END_EDL_NAMESPACE
Definition edlnamespaces.h:76
EDL "standard" types including known bit-length signed and unsigned integer type[def]s and definition...
#define EDL_API
Definition edltypes.h:86
_BEGIN_EDL_NAMESPACE EDL_API void throwVectorError()