1 /*############################################################################
2 # Copyright (C) 2013-2020 Intel Corporation
4 # SPDX-License-Identifier: MIT
5 ############################################################################*/
9 #include "vpl/mfxstructures.h"
15 friend class MFXVector;
18 iterator_tmpl(mfxU32 index, T *records) : mIndex(index), mRecords(records) {}
21 iterator_tmpl() : mIndex(), mRecords() {}
22 bool operator==(const iterator_tmpl<T> &that) const {
23 return mIndex == that.mIndex;
25 bool operator!=(const iterator_tmpl<T> &that) const {
26 return mIndex != that.mIndex;
28 mfxU32 operator-(const iterator_tmpl<T> &that) const {
29 return mIndex - that.mIndex;
31 iterator_tmpl<T> &operator++() {
35 iterator_tmpl<T> &operator++(int) {
40 return mRecords[mIndex];
43 return mRecords + mIndex;
47 class MFXVectorRangeError : public std::exception {};
55 MFXVector() : mRecords(), mNrecords() {}
56 MFXVector(const MFXVector &rhs) : mRecords(), mNrecords() {
57 insert(end(), rhs.begin(), rhs.end());
59 MFXVector &operator=(const MFXVector &rhs) {
62 insert(end(), rhs.begin(), rhs.end());
66 virtual ~MFXVector() {
69 typedef iterator_tmpl<T> iterator;
71 iterator begin() const {
72 return iterator(0u, mRecords);
74 iterator end() const {
75 return iterator(mNrecords, mRecords);
77 void insert(iterator where, iterator beg_iter, iterator end_iter) {
78 mfxU32 elementsToInsert = (end_iter - beg_iter);
79 if (!elementsToInsert) {
82 if (where.mIndex > mNrecords) {
83 throw MFXVectorRangeError();
86 T *newRecords = new T[mNrecords + elementsToInsert]();
90 for (; i < where.mIndex; i++) {
91 newRecords[i] = mRecords[i];
94 for (; beg_iter != end_iter; beg_iter++, i++) {
95 newRecords[i] = *beg_iter;
99 for (; i < mNrecords + elementsToInsert; i++) {
100 newRecords[i] = mRecords[i - elementsToInsert];
105 mRecords = newRecords;
108 T &operator[](mfxU32 idx) {
109 return mRecords[idx];
111 void push_back(const T &obj) {
112 T *newRecords = new T[mNrecords + 1]();
114 for (; i < mNrecords; i++) {
115 newRecords[i] = mRecords[i];
120 mRecords = newRecords;
123 void erase(iterator at) {
124 if (at.mIndex >= mNrecords) {
125 throw MFXVectorRangeError();
128 mfxU32 i = at.mIndex;
129 for (; i != mNrecords; i++) {
130 mRecords[i] = mRecords[i + 1];
132 //destroy last element
135 void resize(mfxU32 nSize) {
136 T *newRecords = new T[nSize]();
137 for (mfxU32 i = 0; i < mNrecords; i++) {
138 newRecords[i] = mRecords[i];
141 mRecords = newRecords;
144 mfxU32 size() const {