vector.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*-
  2. * Copyright 2012-1015 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 "buffer.h"
  29. #include "new.h"
  30. #include "stddef.h"
  31. namespace tinystl {
  32. template<typename T, typename Alloc = TINYSTL_ALLOCATOR>
  33. class vector {
  34. public:
  35. vector();
  36. vector(const vector& other);
  37. vector(size_t _size);
  38. vector(size_t _size, const T& value);
  39. vector(const T* first, const T* last);
  40. ~vector();
  41. vector& operator=(const vector& other);
  42. void assign(const T* first, const T* last);
  43. const T* data() const;
  44. T* data();
  45. size_t size() const;
  46. size_t capacity() const;
  47. bool empty() const;
  48. T& operator[](size_t idx);
  49. const T& operator[](size_t idx) const;
  50. const T& front() const;
  51. T& front();
  52. const T& back() const;
  53. T& back();
  54. void resize(size_t size);
  55. void resize(size_t size, const T& value);
  56. void clear();
  57. void reserve(size_t _capacity);
  58. void push_back(const T& t);
  59. void pop_back();
  60. void emplace_back();
  61. template<typename Param>
  62. void emplace_back(const Param& param);
  63. void shrink_to_fit();
  64. void swap(vector& other);
  65. typedef T value_type;
  66. typedef T* iterator;
  67. iterator begin();
  68. iterator end();
  69. typedef const T* const_iterator;
  70. const_iterator begin() const;
  71. const_iterator end() const;
  72. void insert(iterator where);
  73. void insert(iterator where, const T& value);
  74. void insert(iterator where, const T* first, const T* last);
  75. template<typename Param>
  76. void emplace(iterator where, const Param& param);
  77. iterator erase(iterator where);
  78. iterator erase(iterator first, iterator last);
  79. iterator erase_unordered(iterator where);
  80. iterator erase_unordered(iterator first, iterator last);
  81. private:
  82. buffer<T, Alloc> m_buffer;
  83. };
  84. template<typename T, typename Alloc>
  85. inline vector<T, Alloc>::vector() {
  86. buffer_init(&m_buffer);
  87. }
  88. template<typename T, typename Alloc>
  89. inline vector<T, Alloc>::vector(const vector& other) {
  90. buffer_init(&m_buffer);
  91. buffer_reserve(&m_buffer, other.size());
  92. buffer_insert(&m_buffer, m_buffer.last, other.m_buffer.first, other.m_buffer.last);
  93. }
  94. template<typename T, typename Alloc>
  95. inline vector<T, Alloc>::vector(size_t _size) {
  96. buffer_init(&m_buffer);
  97. buffer_resize(&m_buffer, _size);
  98. }
  99. template<typename T, typename Alloc>
  100. inline vector<T, Alloc>::vector(size_t _size, const T& value) {
  101. buffer_init(&m_buffer);
  102. buffer_resize(&m_buffer, _size, value);
  103. }
  104. template<typename T, typename Alloc>
  105. inline vector<T, Alloc>::vector(const T* first, const T* last) {
  106. buffer_init(&m_buffer);
  107. buffer_insert(&m_buffer, m_buffer.last, first, last);
  108. }
  109. template<typename T, typename Alloc>
  110. inline vector<T, Alloc>::~vector() {
  111. buffer_destroy(&m_buffer);
  112. }
  113. template<typename T, typename Alloc>
  114. inline vector<T, Alloc>& vector<T, Alloc>::operator=(const vector& other) {
  115. vector(other).swap(*this);
  116. return *this;
  117. }
  118. template<typename T, typename Alloc>
  119. inline void vector<T, Alloc>::assign(const T* first, const T* last) {
  120. buffer_clear(&m_buffer);
  121. buffer_insert(&m_buffer, m_buffer.last, first, last);
  122. }
  123. template<typename T, typename Alloc>
  124. inline const T* vector<T, Alloc>::data() const {
  125. return m_buffer.first;
  126. }
  127. template<typename T, typename Alloc>
  128. inline T* vector<T, Alloc>::data() {
  129. return m_buffer.first;
  130. }
  131. template<typename T, typename Alloc>
  132. inline size_t vector<T, Alloc>::size() const {
  133. return (size_t)(m_buffer.last - m_buffer.first);
  134. }
  135. template<typename T, typename Alloc>
  136. inline size_t vector<T, Alloc>::capacity() const {
  137. return (size_t)(m_buffer.capacity - m_buffer.first);
  138. }
  139. template<typename T, typename Alloc>
  140. inline bool vector<T, Alloc>::empty() const {
  141. return m_buffer.last == m_buffer.first;
  142. }
  143. template<typename T, typename Alloc>
  144. inline T& vector<T, Alloc>::operator[](size_t idx) {
  145. return m_buffer.first[idx];
  146. }
  147. template<typename T, typename Alloc>
  148. inline const T& vector<T, Alloc>::operator[](size_t idx) const {
  149. return m_buffer.first[idx];
  150. }
  151. template<typename T, typename Alloc>
  152. inline const T& vector<T, Alloc>::front() const {
  153. return m_buffer.first[0];
  154. }
  155. template<typename T, typename Alloc>
  156. inline T& vector<T, Alloc>::front() {
  157. return m_buffer.first[0];
  158. }
  159. template<typename T, typename Alloc>
  160. inline const T& vector<T, Alloc>::back() const {
  161. return m_buffer.last[-1];
  162. }
  163. template<typename T, typename Alloc>
  164. inline T& vector<T, Alloc>::back() {
  165. return m_buffer.last[-1];
  166. }
  167. template<typename T, typename Alloc>
  168. inline void vector<T, Alloc>::resize(size_t _size) {
  169. buffer_resize(&m_buffer, _size);
  170. }
  171. template<typename T, typename Alloc>
  172. inline void vector<T, Alloc>::resize(size_t _size, const T& value) {
  173. buffer_resize(&m_buffer, _size, value);
  174. }
  175. template<typename T, typename Alloc>
  176. inline void vector<T, Alloc>::clear() {
  177. buffer_clear(&m_buffer);
  178. }
  179. template<typename T, typename Alloc>
  180. inline void vector<T, Alloc>::reserve(size_t _capacity) {
  181. buffer_reserve(&m_buffer, _capacity);
  182. }
  183. template<typename T, typename Alloc>
  184. inline void vector<T, Alloc>::push_back(const T& t) {
  185. buffer_append(&m_buffer, &t);
  186. }
  187. template<typename T, typename Alloc>
  188. inline void vector<T, Alloc>::emplace_back() {
  189. buffer_append(&m_buffer);
  190. }
  191. template<typename T, typename Alloc>
  192. template<typename Param>
  193. inline void vector<T, Alloc>::emplace_back(const Param& param) {
  194. buffer_append(&m_buffer, &param);
  195. }
  196. template<typename T, typename Alloc>
  197. inline void vector<T, Alloc>::pop_back() {
  198. buffer_erase(&m_buffer, m_buffer.last - 1, m_buffer.last);
  199. }
  200. template<typename T, typename Alloc>
  201. inline void vector<T, Alloc>::shrink_to_fit() {
  202. buffer_shrink_to_fit(&m_buffer);
  203. }
  204. template<typename T, typename Alloc>
  205. inline void vector<T, Alloc>::swap(vector& other) {
  206. buffer_swap(&m_buffer, &other.m_buffer);
  207. }
  208. template<typename T, typename Alloc>
  209. inline typename vector<T, Alloc>::iterator vector<T,Alloc>::begin() {
  210. return m_buffer.first;
  211. }
  212. template<typename T, typename Alloc>
  213. inline typename vector<T, Alloc>::iterator vector<T,Alloc>::end() {
  214. return m_buffer.last;
  215. }
  216. template<typename T, typename Alloc>
  217. inline typename vector<T, Alloc>::const_iterator vector<T,Alloc>::begin() const {
  218. return m_buffer.first;
  219. }
  220. template<typename T, typename Alloc>
  221. inline typename vector<T, Alloc>::const_iterator vector<T,Alloc>::end() const {
  222. return m_buffer.last;
  223. }
  224. template<typename T, typename Alloc>
  225. inline void vector<T, Alloc>::insert(iterator where) {
  226. buffer_insert(&m_buffer, where, 1);
  227. }
  228. template<typename T, typename Alloc>
  229. inline void vector<T, Alloc>::insert(iterator where, const T& value) {
  230. buffer_insert(&m_buffer, where, &value, &value + 1);
  231. }
  232. template<typename T, typename Alloc>
  233. inline void vector<T, Alloc>::insert(iterator where, const T* first, const T* last) {
  234. buffer_insert(&m_buffer, where, first, last);
  235. }
  236. template<typename T, typename Alloc>
  237. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(iterator where) {
  238. return buffer_erase(&m_buffer, where, where + 1);
  239. }
  240. template<typename T, typename Alloc>
  241. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(iterator first, iterator last) {
  242. return buffer_erase(&m_buffer, first, last);
  243. }
  244. template<typename T, typename Alloc>
  245. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase_unordered(iterator where) {
  246. return buffer_erase_unordered(&m_buffer, where, where + 1);
  247. }
  248. template<typename T, typename Alloc>
  249. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase_unordered(iterator first, iterator last) {
  250. return buffer_erase_unordered(&m_buffer, first, last);
  251. }
  252. template<typename T, typename Alloc>
  253. template<typename Param>
  254. void vector<T, Alloc>::emplace(iterator where, const Param& param) {
  255. buffer_insert(&m_buffer, where, &param, &param + 1);
  256. }
  257. }
  258. #endif