mfx_vector.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* ****************************************************************************** *\
  2. Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. - Neither the name of Intel Corporation nor the names of its contributors
  11. may be used to endorse or promote products derived from this software
  12. without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS" AND ANY EXPRESS OR
  14. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. File Name: mfx_vector.h
  24. \* ****************************************************************************** */
  25. #pragma once
  26. #include "mfxstructures.h"
  27. #include <exception>
  28. namespace MFX
  29. {
  30. template <class T>
  31. class iterator_tmpl
  32. {
  33. template <class U> friend class MFXVector;
  34. mfxU32 mIndex;
  35. T* mRecords;
  36. iterator_tmpl(mfxU32 index , T * records)
  37. : mIndex (index)
  38. , mRecords(records)
  39. {}
  40. public:
  41. iterator_tmpl()
  42. : mIndex ()
  43. , mRecords()
  44. {}
  45. bool operator ==(const iterator_tmpl<T> & that )const
  46. {
  47. return mIndex == that.mIndex;
  48. }
  49. bool operator !=(const iterator_tmpl<T> & that )const
  50. {
  51. return mIndex != that.mIndex;
  52. }
  53. mfxU32 operator - (const iterator_tmpl<T> &that) const
  54. {
  55. return mIndex - that.mIndex;
  56. }
  57. iterator_tmpl<T> & operator ++()
  58. {
  59. mIndex++;
  60. return * this;
  61. }
  62. iterator_tmpl<T> & operator ++(int)
  63. {
  64. mIndex++;
  65. return * this;
  66. }
  67. T & operator *()
  68. {
  69. return mRecords[mIndex];
  70. }
  71. T * operator ->()
  72. {
  73. return mRecords + mIndex;
  74. }
  75. };
  76. class MFXVectorRangeError : public std::exception
  77. {
  78. };
  79. template <class T>
  80. class MFXVector
  81. {
  82. T* mRecords;
  83. mfxU32 mNrecords;
  84. public:
  85. MFXVector()
  86. : mRecords()
  87. , mNrecords()
  88. {}
  89. MFXVector(const MFXVector & rhs)
  90. : mRecords()
  91. , mNrecords()
  92. {
  93. insert(end(), rhs.begin(), rhs.end());
  94. }
  95. MFXVector & operator = (const MFXVector & rhs)
  96. {
  97. if (this != &rhs)
  98. {
  99. clear();
  100. insert(end(), rhs.begin(), rhs.end());
  101. }
  102. return *this;
  103. }
  104. virtual ~MFXVector ()
  105. {
  106. clear();
  107. }
  108. typedef iterator_tmpl<T> iterator;
  109. iterator begin() const
  110. {
  111. return iterator(0u, mRecords);
  112. }
  113. iterator end() const
  114. {
  115. return iterator(mNrecords, mRecords);
  116. }
  117. void insert(iterator where, iterator beg_iter, iterator end_iter)
  118. {
  119. mfxU32 elementsToInsert = (end_iter - beg_iter);
  120. if (!elementsToInsert)
  121. {
  122. return;
  123. }
  124. if (where.mIndex > mNrecords)
  125. {
  126. throw MFXVectorRangeError();
  127. }
  128. T *newRecords = new T[mNrecords + elementsToInsert]();
  129. mfxU32 i = 0;
  130. // save left
  131. for (; i < where.mIndex; i++)
  132. {
  133. newRecords[i] = mRecords[i];
  134. }
  135. // insert
  136. for (; beg_iter != end_iter; beg_iter++, i++)
  137. {
  138. newRecords[i] = *beg_iter;
  139. }
  140. //save right
  141. for (; i < mNrecords + elementsToInsert; i++)
  142. {
  143. newRecords[i] = mRecords[i - elementsToInsert];
  144. }
  145. delete [] mRecords;
  146. mRecords = newRecords;
  147. mNrecords = i;
  148. }
  149. T& operator [] (mfxU32 idx)
  150. {
  151. return mRecords[idx];
  152. }
  153. void push_back(const T& obj)
  154. {
  155. T *newRecords = new T[mNrecords + 1]();
  156. mfxU32 i = 0;
  157. for (; i <mNrecords; i++)
  158. {
  159. newRecords[i] = mRecords[i];
  160. }
  161. newRecords[i] = obj;
  162. delete [] mRecords;
  163. mRecords = newRecords;
  164. mNrecords = i + 1;
  165. }
  166. void erase (iterator at)
  167. {
  168. if (at.mIndex >= mNrecords)
  169. {
  170. throw MFXVectorRangeError();
  171. }
  172. mNrecords--;
  173. mfxU32 i = at.mIndex;
  174. for (; i != mNrecords; i++)
  175. {
  176. mRecords[i] = mRecords[i+1];
  177. }
  178. //destroy last element
  179. mRecords[i] = T();
  180. }
  181. void resize(mfxU32 nSize)
  182. {
  183. T * newRecords = new T[nSize]();
  184. for (mfxU32 i = 0; i <mNrecords; i++)
  185. {
  186. newRecords[i] = mRecords[i];
  187. }
  188. delete [] mRecords;
  189. mRecords = newRecords;
  190. mNrecords = nSize;
  191. }
  192. mfxU32 size() const
  193. {
  194. return mNrecords;
  195. }
  196. void clear()
  197. {
  198. delete [] mRecords;
  199. mRecords = 0;
  200. mNrecords = 0;
  201. }
  202. };
  203. }