qsv: Update SDK version to v2022.2.4
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / qsv / libmfx / dispatcher / windows / mfx_vector.h
1 /*############################################################################
2   # Copyright (C) Intel Corporation
3   #
4   # SPDX-License-Identifier: MIT
5   ############################################################################*/
6
7 #pragma once
8 #include <exception>
9 #include "vpl/mfxstructures.h"
10
11 namespace MFX {
12 template <class T>
13 class iterator_tmpl {
14     template <class U>
15     friend class MFXVector;
16     mfxU32 mIndex;
17     T *mRecords;
18     iterator_tmpl(mfxU32 index, T *records) : mIndex(index), mRecords(records) {}
19
20 public:
21     iterator_tmpl() : mIndex(), mRecords() {}
22     bool operator==(const iterator_tmpl<T> &that) const {
23         return mIndex == that.mIndex;
24     }
25     bool operator!=(const iterator_tmpl<T> &that) const {
26         return mIndex != that.mIndex;
27     }
28     mfxU32 operator-(const iterator_tmpl<T> &that) const {
29         return mIndex - that.mIndex;
30     }
31     iterator_tmpl<T> &operator++() {
32         mIndex++;
33         return *this;
34     }
35     iterator_tmpl<T> &operator++(int) {
36         mIndex++;
37         return *this;
38     }
39     T &operator*() {
40         return mRecords[mIndex];
41     }
42     T *operator->() {
43         return mRecords + mIndex;
44     }
45 };
46
47 class MFXVectorRangeError : public std::exception {};
48
49 template <class T>
50 class MFXVector {
51     T *mRecords;
52     mfxU32 mNrecords;
53
54 public:
55     MFXVector() : mRecords(), mNrecords() {}
56     MFXVector(const MFXVector &rhs) : mRecords(), mNrecords() {
57         insert(end(), rhs.begin(), rhs.end());
58     }
59     MFXVector &operator=(const MFXVector &rhs) {
60         if (this != &rhs) {
61             clear();
62             insert(end(), rhs.begin(), rhs.end());
63         }
64         return *this;
65     }
66     virtual ~MFXVector() {
67         clear();
68     }
69     typedef iterator_tmpl<T> iterator;
70
71     iterator begin() const {
72         return iterator(0u, mRecords);
73     }
74     iterator end() const {
75         return iterator(mNrecords, mRecords);
76     }
77     void insert(iterator where, iterator beg_iter, iterator end_iter) {
78         mfxU32 elementsToInsert = (end_iter - beg_iter);
79         if (!elementsToInsert) {
80             return;
81         }
82         if (where.mIndex > mNrecords) {
83             throw MFXVectorRangeError();
84         }
85
86         T *newRecords = new T[mNrecords + elementsToInsert]();
87         mfxU32 i      = 0;
88
89         // save left
90         for (; i < where.mIndex; i++) {
91             newRecords[i] = mRecords[i];
92         }
93         // insert
94         for (; beg_iter != end_iter; beg_iter++, i++) {
95             newRecords[i] = *beg_iter;
96         }
97
98         //save right
99         for (; i < mNrecords + elementsToInsert; i++) {
100             newRecords[i] = mRecords[i - elementsToInsert];
101         }
102
103         delete[] mRecords;
104
105         mRecords  = newRecords;
106         mNrecords = i;
107     }
108     T &operator[](mfxU32 idx) {
109         return mRecords[idx];
110     }
111     void push_back(const T &obj) {
112         T *newRecords = new T[mNrecords + 1]();
113         mfxU32 i      = 0;
114         for (; i < mNrecords; i++) {
115             newRecords[i] = mRecords[i];
116         }
117         newRecords[i] = obj;
118         delete[] mRecords;
119
120         mRecords  = newRecords;
121         mNrecords = i + 1;
122     }
123     void erase(iterator at) {
124         if (at.mIndex >= mNrecords) {
125             throw MFXVectorRangeError();
126         }
127         mNrecords--;
128         mfxU32 i = at.mIndex;
129         for (; i != mNrecords; i++) {
130             mRecords[i] = mRecords[i + 1];
131         }
132         //destroy last element
133         mRecords[i] = T();
134     }
135     void resize(mfxU32 nSize) {
136         T *newRecords = new T[nSize]();
137         for (mfxU32 i = 0; i < mNrecords; i++) {
138             newRecords[i] = mRecords[i];
139         }
140         delete[] mRecords;
141         mRecords  = newRecords;
142         mNrecords = nSize;
143     }
144     mfxU32 size() const {
145         return mNrecords;
146     }
147     void clear() {
148         delete[] mRecords;
149         mRecords  = 0;
150         mNrecords = 0;
151     }
152     bool empty() {
153         return !mRecords;
154     }
155     T *data() const {
156         return mRecords;
157     }
158 };
159 } // namespace MFX