vector.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*-
  2. * Copyright 2012-2018 Matthew Endsley
  3. * All rights reserved
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted providing that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef TINYSTL_VECTOR_H
  27. #define TINYSTL_VECTOR_H
  28. #include <tinystl/allocator.h>
  29. #include <tinystl/buffer.h>
  30. #include <tinystl/new.h>
  31. #include <tinystl/stddef.h>
  32. namespace tinystl {
  33. template<typename T, typename Alloc = TINYSTL_ALLOCATOR>
  34. class vector {
  35. public:
  36. vector();
  37. vector(const vector& other);
  38. vector(vector&& other);
  39. vector(size_t size);
  40. vector(size_t size, const T& value);
  41. vector(const T* first, const T* last);
  42. ~vector();
  43. vector& operator=(const vector& other);
  44. vector& operator=(vector&& other);
  45. void assign(const T* first, const T* last);
  46. const T* data() const;
  47. T* data();
  48. size_t size() const;
  49. size_t capacity() const;
  50. bool empty() const;
  51. T& operator[](size_t idx);
  52. const T& operator[](size_t idx) const;
  53. const T& front() const;
  54. T& front();
  55. const T& back() const;
  56. T& back();
  57. void resize(size_t size);
  58. void resize(size_t size, const T& value);
  59. void clear();
  60. void reserve(size_t capacity);
  61. void push_back(const T& t);
  62. void pop_back();
  63. void emplace_back();
  64. template<typename Param>
  65. void emplace_back(const Param& param);
  66. void shrink_to_fit();
  67. void swap(vector& other);
  68. typedef T value_type;
  69. typedef T* iterator;
  70. iterator begin();
  71. iterator end();
  72. typedef const T* const_iterator;
  73. const_iterator begin() const;
  74. const_iterator end() const;
  75. void insert(iterator where);
  76. void insert(iterator where, const T& value);
  77. void insert(iterator where, const T* first, const T* last);
  78. template<typename Param>
  79. void emplace(iterator where, const Param& param);
  80. iterator erase(iterator where);
  81. iterator erase(iterator first, iterator last);
  82. iterator erase_unordered(iterator where);
  83. iterator erase_unordered(iterator first, iterator last);
  84. private:
  85. buffer<T, Alloc> m_buffer;
  86. };
  87. template<typename T, typename Alloc>
  88. inline vector<T, Alloc>::vector() {
  89. buffer_init(&m_buffer);
  90. }
  91. template<typename T, typename Alloc>
  92. inline vector<T, Alloc>::vector(const vector& other) {
  93. buffer_init(&m_buffer);
  94. buffer_reserve(&m_buffer, other.size());
  95. buffer_insert(&m_buffer, m_buffer.last, other.m_buffer.first, other.m_buffer.last);
  96. }
  97. template<typename T, typename Alloc>
  98. inline vector<T, Alloc>::vector(vector&& other) {
  99. buffer_move(&m_buffer, &other.m_buffer);
  100. }
  101. template<typename T, typename Alloc>
  102. inline vector<T, Alloc>::vector(size_t size) {
  103. buffer_init(&m_buffer);
  104. buffer_resize(&m_buffer, size);
  105. }
  106. template<typename T, typename Alloc>
  107. inline vector<T, Alloc>::vector(size_t size, const T& value) {
  108. buffer_init(&m_buffer);
  109. buffer_resize(&m_buffer, size, value);
  110. }
  111. template<typename T, typename Alloc>
  112. inline vector<T, Alloc>::vector(const T* first, const T* last) {
  113. buffer_init(&m_buffer);
  114. buffer_insert(&m_buffer, m_buffer.last, first, last);
  115. }
  116. template<typename T, typename Alloc>
  117. inline vector<T, Alloc>::~vector() {
  118. buffer_destroy(&m_buffer);
  119. }
  120. template<typename T, typename Alloc>
  121. inline vector<T, Alloc>& vector<T, Alloc>::operator=(const vector& other) {
  122. vector(other).swap(*this);
  123. return *this;
  124. }
  125. template<typename T, typename Alloc>
  126. vector<T, Alloc>& vector<T, Alloc>::operator=(vector&& other) {
  127. buffer_destroy(&m_buffer);
  128. buffer_move(&m_buffer, &other.m_buffer);
  129. return *this;
  130. }
  131. template<typename T, typename Alloc>
  132. inline void vector<T, Alloc>::assign(const T* first, const T* last) {
  133. buffer_clear(&m_buffer);
  134. buffer_insert(&m_buffer, m_buffer.last, first, last);
  135. }
  136. template<typename T, typename Alloc>
  137. inline const T* vector<T, Alloc>::data() const {
  138. return m_buffer.first;
  139. }
  140. template<typename T, typename Alloc>
  141. inline T* vector<T, Alloc>::data() {
  142. return m_buffer.first;
  143. }
  144. template<typename T, typename Alloc>
  145. inline size_t vector<T, Alloc>::size() const {
  146. return (size_t)(m_buffer.last - m_buffer.first);
  147. }
  148. template<typename T, typename Alloc>
  149. inline size_t vector<T, Alloc>::capacity() const {
  150. return (size_t)(m_buffer.capacity - m_buffer.first);
  151. }
  152. template<typename T, typename Alloc>
  153. inline bool vector<T, Alloc>::empty() const {
  154. return m_buffer.last == m_buffer.first;
  155. }
  156. template<typename T, typename Alloc>
  157. inline T& vector<T, Alloc>::operator[](size_t idx) {
  158. return m_buffer.first[idx];
  159. }
  160. template<typename T, typename Alloc>
  161. inline const T& vector<T, Alloc>::operator[](size_t idx) const {
  162. return m_buffer.first[idx];
  163. }
  164. template<typename T, typename Alloc>
  165. inline const T& vector<T, Alloc>::front() const {
  166. return m_buffer.first[0];
  167. }
  168. template<typename T, typename Alloc>
  169. inline T& vector<T, Alloc>::front() {
  170. return m_buffer.first[0];
  171. }
  172. template<typename T, typename Alloc>
  173. inline const T& vector<T, Alloc>::back() const {
  174. return m_buffer.last[-1];
  175. }
  176. template<typename T, typename Alloc>
  177. inline T& vector<T, Alloc>::back() {
  178. return m_buffer.last[-1];
  179. }
  180. template<typename T, typename Alloc>
  181. inline void vector<T, Alloc>::resize(size_t size) {
  182. buffer_resize(&m_buffer, size);
  183. }
  184. template<typename T, typename Alloc>
  185. inline void vector<T, Alloc>::resize(size_t size, const T& value) {
  186. buffer_resize(&m_buffer, size, value);
  187. }
  188. template<typename T, typename Alloc>
  189. inline void vector<T, Alloc>::clear() {
  190. buffer_clear(&m_buffer);
  191. }
  192. template<typename T, typename Alloc>
  193. inline void vector<T, Alloc>::reserve(size_t capacity) {
  194. buffer_reserve(&m_buffer, capacity);
  195. }
  196. template<typename T, typename Alloc>
  197. inline void vector<T, Alloc>::push_back(const T& t) {
  198. buffer_append(&m_buffer, &t);
  199. }
  200. template<typename T, typename Alloc>
  201. inline void vector<T, Alloc>::emplace_back() {
  202. buffer_append(&m_buffer);
  203. }
  204. template<typename T, typename Alloc>
  205. template<typename Param>
  206. inline void vector<T, Alloc>::emplace_back(const Param& param) {
  207. buffer_append(&m_buffer, &param);
  208. }
  209. template<typename T, typename Alloc>
  210. inline void vector<T, Alloc>::pop_back() {
  211. buffer_erase(&m_buffer, m_buffer.last - 1, m_buffer.last);
  212. }
  213. template<typename T, typename Alloc>
  214. inline void vector<T, Alloc>::shrink_to_fit() {
  215. buffer_shrink_to_fit(&m_buffer);
  216. }
  217. template<typename T, typename Alloc>
  218. inline void vector<T, Alloc>::swap(vector& other) {
  219. buffer_swap(&m_buffer, &other.m_buffer);
  220. }
  221. template<typename T, typename Alloc>
  222. inline typename vector<T, Alloc>::iterator vector<T,Alloc>::begin() {
  223. return m_buffer.first;
  224. }
  225. template<typename T, typename Alloc>
  226. inline typename vector<T, Alloc>::iterator vector<T,Alloc>::end() {
  227. return m_buffer.last;
  228. }
  229. template<typename T, typename Alloc>
  230. inline typename vector<T, Alloc>::const_iterator vector<T,Alloc>::begin() const {
  231. return m_buffer.first;
  232. }
  233. template<typename T, typename Alloc>
  234. inline typename vector<T, Alloc>::const_iterator vector<T,Alloc>::end() const {
  235. return m_buffer.last;
  236. }
  237. template<typename T, typename Alloc>
  238. inline void vector<T, Alloc>::insert(typename vector::iterator where) {
  239. buffer_insert(&m_buffer, where, 1);
  240. }
  241. template<typename T, typename Alloc>
  242. inline void vector<T, Alloc>::insert(iterator where, const T& value) {
  243. buffer_insert(&m_buffer, where, &value, &value + 1);
  244. }
  245. template<typename T, typename Alloc>
  246. inline void vector<T, Alloc>::insert(iterator where, const T* first, const T* last) {
  247. buffer_insert(&m_buffer, where, first, last);
  248. }
  249. template<typename T, typename Alloc>
  250. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(iterator where) {
  251. return buffer_erase(&m_buffer, where, where + 1);
  252. }
  253. template<typename T, typename Alloc>
  254. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(iterator first, iterator last) {
  255. return buffer_erase(&m_buffer, first, last);
  256. }
  257. template<typename T, typename Alloc>
  258. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase_unordered(iterator where) {
  259. return buffer_erase_unordered(&m_buffer, where, where + 1);
  260. }
  261. template<typename T, typename Alloc>
  262. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase_unordered(iterator first, iterator last) {
  263. return buffer_erase_unordered(&m_buffer, first, last);
  264. }
  265. template<typename T, typename Alloc>
  266. template<typename Param>
  267. void vector<T, Alloc>::emplace(typename vector::iterator where, const Param& param) {
  268. buffer_insert(&m_buffer, where, &param, &param + 1);
  269. }
  270. }
  271. #endif // TINYSTL_VECTOR_H