vector.h 7.6 KB

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