vector.h 7.5 KB

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