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