vector.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*-
  2. * Copyright 2012 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& back() const;
  51. T& back();
  52. void resize(size_t size);
  53. void resize(size_t size, const T& value);
  54. void clear();
  55. void reserve(size_t _capacity);
  56. void push_back(const T& t);
  57. void pop_back();
  58. void shrink_to_fit();
  59. void swap(vector& other);
  60. typedef T value_type;
  61. typedef T* iterator;
  62. iterator begin();
  63. iterator end();
  64. typedef const T* const_iterator;
  65. const_iterator begin() const;
  66. const_iterator end() const;
  67. void insert(iterator where, const T& value);
  68. void insert(iterator where, const T* first, const T* last);
  69. iterator erase(iterator where);
  70. iterator erase(iterator first, iterator last);
  71. iterator erase_unordered(iterator where);
  72. iterator erase_unordered(iterator first, iterator last);
  73. private:
  74. buffer<T, Alloc> m_buffer;
  75. };
  76. template<typename T, typename Alloc>
  77. inline vector<T, Alloc>::vector() {
  78. buffer_init(&m_buffer);
  79. }
  80. template<typename T, typename Alloc>
  81. inline vector<T, Alloc>::vector(const vector& other) {
  82. buffer_init(&m_buffer);
  83. buffer_reserve(&m_buffer, other.size());
  84. buffer_insert(&m_buffer, m_buffer.last, other.m_buffer.first, other.m_buffer.last);
  85. }
  86. template<typename T, typename Alloc>
  87. inline vector<T, Alloc>::vector(size_t _size) {
  88. buffer_init(&m_buffer);
  89. buffer_resize(&m_buffer, _size, T());
  90. }
  91. template<typename T, typename Alloc>
  92. inline vector<T, Alloc>::vector(size_t _size, const T& value) {
  93. buffer_init(&m_buffer);
  94. buffer_resize(&m_buffer, _size, value);
  95. }
  96. template<typename T, typename Alloc>
  97. inline vector<T, Alloc>::vector(const T* first, const T* last) {
  98. buffer_init(&m_buffer);
  99. buffer_insert(&m_buffer, m_buffer.last, first, last);
  100. }
  101. template<typename T, typename Alloc>
  102. inline vector<T, Alloc>::~vector() {
  103. buffer_destroy(&m_buffer);
  104. }
  105. template<typename T, typename Alloc>
  106. inline vector<T, Alloc>& vector<T, Alloc>::operator=(const vector& other) {
  107. vector(other).swap(*this);
  108. return *this;
  109. }
  110. template<typename T, typename Alloc>
  111. inline void vector<T, Alloc>::assign(const T* first, const T* last) {
  112. buffer_clear(&m_buffer);
  113. buffer_insert(&m_buffer, m_buffer.last, first, last);
  114. }
  115. template<typename T, typename Alloc>
  116. inline const T* vector<T, Alloc>::data() const {
  117. return m_buffer.first;
  118. }
  119. template<typename T, typename Alloc>
  120. inline T* vector<T, Alloc>::data() {
  121. return m_buffer.first;
  122. }
  123. template<typename T, typename Alloc>
  124. inline size_t vector<T, Alloc>::size() const {
  125. return (size_t)(m_buffer.last - m_buffer.first);
  126. }
  127. template<typename T, typename Alloc>
  128. inline size_t vector<T, Alloc>::capacity() const {
  129. return (size_t)(m_buffer.capacity - m_buffer.first);
  130. }
  131. template<typename T, typename Alloc>
  132. inline bool vector<T, Alloc>::empty() const {
  133. return m_buffer.last == m_buffer.first;
  134. }
  135. template<typename T, typename Alloc>
  136. inline T& vector<T, Alloc>::operator[](size_t idx) {
  137. return m_buffer.first[idx];
  138. }
  139. template<typename T, typename Alloc>
  140. inline const T& vector<T, Alloc>::operator[](size_t idx) const {
  141. return m_buffer.first[idx];
  142. }
  143. template<typename T, typename Alloc>
  144. inline const T& vector<T, Alloc>::back() const {
  145. return m_buffer.last[-1];
  146. }
  147. template<typename T, typename Alloc>
  148. inline T& vector<T, Alloc>::back() {
  149. return m_buffer.last[-1];
  150. }
  151. template<typename T, typename Alloc>
  152. inline void vector<T, Alloc>::resize(size_t _size) {
  153. buffer_resize(&m_buffer, _size, T());
  154. }
  155. template<typename T, typename Alloc>
  156. inline void vector<T, Alloc>::resize(size_t _size, const T& value) {
  157. buffer_resize(&m_buffer, _size, value);
  158. }
  159. template<typename T, typename Alloc>
  160. inline void vector<T, Alloc>::clear() {
  161. buffer_clear(&m_buffer);
  162. }
  163. template<typename T, typename Alloc>
  164. inline void vector<T, Alloc>::reserve(size_t _capacity) {
  165. buffer_reserve(&m_buffer, _capacity);
  166. }
  167. template<typename T, typename Alloc>
  168. inline void vector<T, Alloc>::push_back(const T& t) {
  169. buffer_insert(&m_buffer, m_buffer.last, &t, &t + 1);
  170. }
  171. template<typename T, typename Alloc>
  172. inline void vector<T, Alloc>::pop_back() {
  173. buffer_erase(&m_buffer, m_buffer.last - 1, m_buffer.last);
  174. }
  175. template<typename T, typename Alloc>
  176. inline void vector<T, Alloc>::shrink_to_fit() {
  177. buffer_shrink_to_fit(&m_buffer);
  178. }
  179. template<typename T, typename Alloc>
  180. inline void vector<T, Alloc>::swap(vector& other) {
  181. buffer_swap(&m_buffer, &other.m_buffer);
  182. }
  183. template<typename T, typename Alloc>
  184. inline typename vector<T, Alloc>::iterator vector<T,Alloc>::begin() {
  185. return m_buffer.first;
  186. }
  187. template<typename T, typename Alloc>
  188. inline typename vector<T, Alloc>::iterator vector<T,Alloc>::end() {
  189. return m_buffer.last;
  190. }
  191. template<typename T, typename Alloc>
  192. inline typename vector<T, Alloc>::const_iterator vector<T,Alloc>::begin() const {
  193. return m_buffer.first;
  194. }
  195. template<typename T, typename Alloc>
  196. inline typename vector<T, Alloc>::const_iterator vector<T,Alloc>::end() const {
  197. return m_buffer.last;
  198. }
  199. template<typename T, typename Alloc>
  200. inline void vector<T, Alloc>::insert(iterator where, const T& value) {
  201. buffer_insert(&m_buffer, where, &value, &value + 1);
  202. }
  203. template<typename T, typename Alloc>
  204. inline void vector<T, Alloc>::insert(iterator where, const T* first, const T* last) {
  205. buffer_insert(&m_buffer, where, first, last);
  206. }
  207. template<typename T, typename Alloc>
  208. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(iterator where) {
  209. return buffer_erase(&m_buffer, where, where + 1);
  210. }
  211. template<typename T, typename Alloc>
  212. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(iterator first, iterator last) {
  213. return buffer_erase(&m_buffer, first, last);
  214. }
  215. template<typename T, typename Alloc>
  216. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase_unordered(iterator where) {
  217. return buffer_erase_unordered(&m_buffer, where, where + 1);
  218. }
  219. template<typename T, typename Alloc>
  220. inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase_unordered(iterator first, iterator last) {
  221. return buffer_erase_unordered(&m_buffer, first, last);
  222. }
  223. }
  224. #endif