2
0

buffer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_BUFFER_H
  27. #define TINYSTL_BUFFER_H
  28. #include "new.h"
  29. #include "traits.h"
  30. namespace tinystl {
  31. template<typename T, typename Alloc = TINYSTL_ALLOCATOR>
  32. struct buffer {
  33. T* first;
  34. T* last;
  35. T* capacity;
  36. };
  37. template<typename T>
  38. static inline void buffer_destroy_range_traits(T* first, T* last, pod_traits<T, false>) {
  39. for (; first < last; ++first)
  40. first->~T();
  41. }
  42. template<typename T>
  43. static inline void buffer_destroy_range_traits(T*, T*, pod_traits<T, true>) {
  44. }
  45. template<typename T>
  46. static inline void buffer_destroy_range(T* first, T* last) {
  47. buffer_destroy_range_traits(first, last, pod_traits<T>());
  48. }
  49. template<typename T>
  50. static inline void buffer_fill_urange_traits(T* first, T* last, const T& value, pod_traits<T, false>) {
  51. for (; first < last; ++first)
  52. new(placeholder(), first) T(value);
  53. }
  54. template<typename T>
  55. static inline void buffer_fill_urange_traits(T* first, T* last, const T& value, pod_traits<T, true>) {
  56. for (; first < last; ++first)
  57. *first = value;
  58. }
  59. template<typename T>
  60. static inline void buffer_move_urange_traits(T* dest, T* first, T* last, pod_traits<T, false>) {
  61. for (T* it = first; it != last; ++it, ++dest)
  62. move_construct(dest, *it);
  63. buffer_destroy_range(first, last);
  64. }
  65. template<typename T>
  66. static inline void buffer_move_urange_traits(T* dest, T* first, T* last, pod_traits<T, true>) {
  67. for (; first != last; ++first, ++dest)
  68. *dest = *first;
  69. }
  70. template<typename T>
  71. static inline void buffer_bmove_urange_traits(T* dest, T* first, T* last, pod_traits<T, false>) {
  72. dest += (last - first);
  73. for (T* it = last; it != first; --it, --dest) {
  74. move_construct(dest - 1, *(it - 1));
  75. buffer_destroy_range(it - 1, it);
  76. }
  77. }
  78. template<typename T>
  79. static inline void buffer_bmove_urange_traits(T* dest, T* first, T* last, pod_traits<T, true>) {
  80. dest += (last - first);
  81. for (T* it = last; it != first; --it, --dest)
  82. *(dest - 1) = *(it - 1);
  83. }
  84. template<typename T>
  85. static inline void buffer_move_urange(T* dest, T* first, T* last) {
  86. buffer_move_urange_traits(dest, first, last, pod_traits<T>());
  87. }
  88. template<typename T>
  89. static inline void buffer_bmove_urange(T* dest, T* first, T* last) {
  90. buffer_bmove_urange_traits(dest, first, last, pod_traits<T>());
  91. }
  92. template<typename T>
  93. static inline void buffer_fill_urange(T* first, T* last, const T& value) {
  94. buffer_fill_urange_traits(first, last, value, pod_traits<T>());
  95. }
  96. template<typename T, typename Alloc>
  97. static inline void buffer_init(buffer<T, Alloc>* b) {
  98. b->first = b->last = b->capacity = 0;
  99. }
  100. template<typename T, typename Alloc>
  101. static inline void buffer_destroy(buffer<T, Alloc>* b) {
  102. buffer_destroy_range(b->first, b->last);
  103. Alloc::static_deallocate(b->first, (size_t)((char*)b->first - (char*)b->last));
  104. }
  105. template<typename T, typename Alloc>
  106. static inline void buffer_reserve(buffer<T, Alloc>* b, size_t capacity) {
  107. if (b->first + capacity <= b->capacity)
  108. return;
  109. typedef T* pointer;
  110. const size_t size = (size_t)(b->last - b->first);
  111. pointer newfirst = (pointer)Alloc::static_allocate(sizeof(T) * capacity);
  112. buffer_move_urange(newfirst, b->first, b->last);
  113. Alloc::static_deallocate(b->first, sizeof(T) * capacity);
  114. b->first = newfirst;
  115. b->last = newfirst + size;
  116. b->capacity = newfirst + capacity;
  117. }
  118. template<typename T, typename Alloc>
  119. static inline void buffer_resize(buffer<T, Alloc>* b, size_t size, const T& value) {
  120. buffer_reserve(b, size);
  121. buffer_fill_urange(b->last, b->first + size, value);
  122. buffer_destroy_range(b->first + size, b->last);
  123. b->last = b->first + size;
  124. }
  125. template<typename T, typename Alloc>
  126. static inline void buffer_clear(buffer<T, Alloc>* b) {
  127. buffer_destroy_range(b->first, b->last);
  128. b->last = b->first;
  129. }
  130. template<typename T, typename Alloc>
  131. static inline void buffer_insert(buffer<T, Alloc>* b, T* where, const T* first, const T* last) {
  132. const size_t offset = (size_t)(where - b->first);
  133. const size_t newsize = (size_t)((b->last - b->first) + (last - first));
  134. if (b->first + newsize > b->capacity)
  135. buffer_reserve(b, (newsize * 3) / 2);
  136. where = b->first + offset;
  137. const size_t count = (size_t)(last - first);
  138. if (where != b->last)
  139. buffer_bmove_urange(where + count, where, b->last);
  140. for (; first != last; ++first, ++where)
  141. new(placeholder(), where) T(*first);
  142. b->last = b->first + newsize;
  143. }
  144. template<typename T, typename Alloc>
  145. static inline T* buffer_erase(buffer<T, Alloc>* b, T* first, T* last) {
  146. typedef T* pointer;
  147. const size_t range = (last - first);
  148. for (pointer it = last, end = b->last, dest = first; it != end; ++it, ++dest)
  149. move(*dest, *it);
  150. buffer_destroy_range(b->last - range, b->last);
  151. b->last -= range;
  152. return first;
  153. }
  154. template<typename T, typename Alloc>
  155. static inline T* buffer_erase_unordered(buffer<T, Alloc>* b, T* first, T* last) {
  156. typedef T* pointer;
  157. const size_t range = (last - first);
  158. const size_t tail = (b->last - last);
  159. pointer it = b->last - ((range < tail) ? range : tail);
  160. for (pointer end = b->last, dest = first; it != end; ++it, ++dest)
  161. move(*dest, *it);
  162. buffer_destroy_range(b->last - range, b->last);
  163. b->last -= range;
  164. return first;
  165. }
  166. template<typename T, typename Alloc>
  167. static inline void buffer_swap(buffer<T, Alloc>* b, buffer<T, Alloc>* other) {
  168. typedef T* pointer;
  169. const pointer tfirst = b->first, tlast = b->last, tcapacity = b->capacity;
  170. b->first = other->first, b->last = other->last, b->capacity = other->capacity;
  171. other->first = tfirst, other->last = tlast, other->capacity = tcapacity;
  172. }
  173. }
  174. #endif