mfx_vector.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (c) 2013-2019 Intel Corporation
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. // SOFTWARE.
  20. #pragma once
  21. #include "mfxstructures.h"
  22. #include <exception>
  23. namespace MFX
  24. {
  25. template <class T>
  26. class iterator_tmpl
  27. {
  28. template <class U> friend class MFXVector;
  29. mfxU32 mIndex;
  30. T* mRecords;
  31. iterator_tmpl(mfxU32 index , T * records)
  32. : mIndex (index)
  33. , mRecords(records)
  34. {}
  35. public:
  36. iterator_tmpl()
  37. : mIndex ()
  38. , mRecords()
  39. {}
  40. bool operator ==(const iterator_tmpl<T> & that )const
  41. {
  42. return mIndex == that.mIndex;
  43. }
  44. bool operator !=(const iterator_tmpl<T> & that )const
  45. {
  46. return mIndex != that.mIndex;
  47. }
  48. mfxU32 operator - (const iterator_tmpl<T> &that) const
  49. {
  50. return mIndex - that.mIndex;
  51. }
  52. iterator_tmpl<T> & operator ++()
  53. {
  54. mIndex++;
  55. return * this;
  56. }
  57. iterator_tmpl<T> & operator ++(int)
  58. {
  59. mIndex++;
  60. return * this;
  61. }
  62. T & operator *()
  63. {
  64. return mRecords[mIndex];
  65. }
  66. T * operator ->()
  67. {
  68. return mRecords + mIndex;
  69. }
  70. };
  71. class MFXVectorRangeError : public std::exception
  72. {
  73. };
  74. template <class T>
  75. class MFXVector
  76. {
  77. T* mRecords;
  78. mfxU32 mNrecords;
  79. public:
  80. MFXVector()
  81. : mRecords()
  82. , mNrecords()
  83. {}
  84. MFXVector(const MFXVector & rhs)
  85. : mRecords()
  86. , mNrecords()
  87. {
  88. insert(end(), rhs.begin(), rhs.end());
  89. }
  90. MFXVector & operator = (const MFXVector & rhs)
  91. {
  92. if (this != &rhs)
  93. {
  94. clear();
  95. insert(end(), rhs.begin(), rhs.end());
  96. }
  97. return *this;
  98. }
  99. virtual ~MFXVector ()
  100. {
  101. clear();
  102. }
  103. typedef iterator_tmpl<T> iterator;
  104. iterator begin() const
  105. {
  106. return iterator(0u, mRecords);
  107. }
  108. iterator end() const
  109. {
  110. return iterator(mNrecords, mRecords);
  111. }
  112. void insert(iterator where, iterator beg_iter, iterator end_iter)
  113. {
  114. mfxU32 elementsToInsert = (end_iter - beg_iter);
  115. if (!elementsToInsert)
  116. {
  117. return;
  118. }
  119. if (where.mIndex > mNrecords)
  120. {
  121. throw MFXVectorRangeError();
  122. }
  123. T *newRecords = new T[mNrecords + elementsToInsert]();
  124. mfxU32 i = 0;
  125. // save left
  126. for (; i < where.mIndex; i++)
  127. {
  128. newRecords[i] = mRecords[i];
  129. }
  130. // insert
  131. for (; beg_iter != end_iter; beg_iter++, i++)
  132. {
  133. newRecords[i] = *beg_iter;
  134. }
  135. //save right
  136. for (; i < mNrecords + elementsToInsert; i++)
  137. {
  138. newRecords[i] = mRecords[i - elementsToInsert];
  139. }
  140. delete [] mRecords;
  141. mRecords = newRecords;
  142. mNrecords = i;
  143. }
  144. T& operator [] (mfxU32 idx)
  145. {
  146. return mRecords[idx];
  147. }
  148. void push_back(const T& obj)
  149. {
  150. T *newRecords = new T[mNrecords + 1]();
  151. mfxU32 i = 0;
  152. for (; i <mNrecords; i++)
  153. {
  154. newRecords[i] = mRecords[i];
  155. }
  156. newRecords[i] = obj;
  157. delete [] mRecords;
  158. mRecords = newRecords;
  159. mNrecords = i + 1;
  160. }
  161. void erase (iterator at)
  162. {
  163. if (at.mIndex >= mNrecords)
  164. {
  165. throw MFXVectorRangeError();
  166. }
  167. mNrecords--;
  168. mfxU32 i = at.mIndex;
  169. for (; i != mNrecords; i++)
  170. {
  171. mRecords[i] = mRecords[i+1];
  172. }
  173. //destroy last element
  174. mRecords[i] = T();
  175. }
  176. void resize(mfxU32 nSize)
  177. {
  178. T * newRecords = new T[nSize]();
  179. for (mfxU32 i = 0; i <mNrecords; i++)
  180. {
  181. newRecords[i] = mRecords[i];
  182. }
  183. delete [] mRecords;
  184. mRecords = newRecords;
  185. mNrecords = nSize;
  186. }
  187. mfxU32 size() const
  188. {
  189. return mNrecords;
  190. }
  191. void clear()
  192. {
  193. delete [] mRecords;
  194. mRecords = 0;
  195. mNrecords = 0;
  196. }
  197. bool empty()
  198. {
  199. return !mRecords;
  200. }
  201. T * data() const
  202. {
  203. return mRecords;
  204. }
  205. };
  206. }