Vector.C 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: Vector.C /main/3 1996/08/17 08:15:28 mgreess $ */
  24. // Copyright (c) 1994, 1996 James Clark
  25. // See the file COPYING for copying permission.
  26. #ifndef Vector_DEF_INCLUDED
  27. #define Vector_DEF_INCLUDED 1
  28. #include <stddef.h>
  29. #include <string.h>
  30. #ifdef SP_QUAL_TEMPLATE_DTOR_BROKEN
  31. #define DTOR(T) ~T
  32. #else
  33. #define DTOR(T) T::~T
  34. #endif
  35. #ifdef SP_NAMESPACE
  36. namespace SP_NAMESPACE {
  37. #endif
  38. template<class T>
  39. Vector<T>::~Vector()
  40. {
  41. if (ptr_) {
  42. erase(ptr_, ptr_ + size_);
  43. ::operator delete((void *)ptr_);
  44. }
  45. }
  46. template<class T>
  47. Vector<T>::Vector(const Vector<T> &v)
  48. : ptr_(0), size_(0), alloc_(0)
  49. {
  50. insert(ptr_ + size_, v.ptr_, v.ptr_ + v.size_);
  51. }
  52. template<class T>
  53. Vector<T>::Vector(size_t n, const T &t)
  54. : ptr_(0), size_(0), alloc_(0)
  55. {
  56. insert(ptr_ + size_, n, t);
  57. }
  58. template<class T>
  59. Vector<T> &Vector<T>::operator=(const Vector<T> &v)
  60. {
  61. if (&v != this) {
  62. size_t n = v.size_;
  63. if (n > size_) {
  64. n = size_;
  65. insert(ptr_ + size_, v.ptr_ + size_, v.ptr_ + v.size_);
  66. }
  67. else if (n < size_)
  68. erase(ptr_ + n, ptr_ + size_);
  69. while (n-- > 0)
  70. ptr_[n] = v.ptr_[n];
  71. }
  72. return *this;
  73. }
  74. template<class T>
  75. void Vector<T>::assign(size_t n, const T &t)
  76. {
  77. size_t sz = n;
  78. if (n > size_) {
  79. sz = size_;
  80. insert(ptr_ + size_, n - size_, t);
  81. }
  82. else if (n < size_)
  83. erase(ptr_ + n, ptr_ + size_);
  84. while (sz-- > 0)
  85. ptr_[sz] = t;
  86. }
  87. template<class T>
  88. void Vector<T>::insert(const T *p, size_t n, const T &t)
  89. {
  90. size_t i = p - ptr_;
  91. reserve(size_ + n);
  92. if (i != size_)
  93. memmove(ptr_ + i + n, ptr_ + i, (size_ - i)*sizeof(T));
  94. size_ += n;
  95. for (T *pp = ptr_ + i; n-- > 0; pp++)
  96. (void)new (pp) T(t);
  97. }
  98. template<class T>
  99. void Vector<T>::insert(const T *p, const T *q1, const T *q2)
  100. {
  101. size_t i = p - ptr_;
  102. size_t n = q2 - q1;
  103. reserve(size_ + n);
  104. if (i != size_)
  105. memmove(ptr_ + i + n, ptr_ + i, (size_ - i)*sizeof(T));
  106. size_ += n;
  107. for (T *pp = ptr_ + i; q1 != q2; q1++, pp++)
  108. (void)new (pp) T(*q1);
  109. }
  110. template<class T>
  111. void Vector<T>::swap(Vector<T> &v)
  112. {
  113. {
  114. T *tem = ptr_;
  115. ptr_ = v.ptr_;
  116. v.ptr_ = tem;
  117. }
  118. {
  119. size_t tem = size_;
  120. size_ = v.size_;
  121. v.size_ = tem;
  122. }
  123. {
  124. size_t tem = alloc_;
  125. alloc_ = v.alloc_;
  126. v.alloc_ = tem;
  127. }
  128. }
  129. template<class T>
  130. void Vector<T>::append(size_t n)
  131. {
  132. reserve(size_ + n);
  133. while (n-- > 0)
  134. (void)new (ptr_ + size_++) T;
  135. }
  136. template<class T>
  137. T *Vector<T>::erase(const T *p1, const T *p2)
  138. {
  139. #if !defined(SP_TEMPLATE_DESTRUCTOR_COMPILER_BUG)
  140. for (const T *p = p1; p != p2; p++)
  141. p->~T();
  142. #endif
  143. if (p2 != ptr_ + size_)
  144. memmove((T *)p1, p2, ((const T *)(ptr_ + size_) - p2)*sizeof(T));
  145. size_ -= p2 - p1;
  146. return (T *)p1;
  147. }
  148. template<class T>
  149. void Vector<T>::reserve1(size_t size)
  150. {
  151. alloc_ *= 2;
  152. if (size > alloc_)
  153. alloc_ += size;
  154. void *p = ::operator new(alloc_*sizeof(T));
  155. if (ptr_) {
  156. memcpy(p, ptr_, size_*sizeof(T));
  157. ::operator delete((void *)ptr_);
  158. }
  159. ptr_ = (T *)p;
  160. }
  161. #ifdef SP_NAMESPACE
  162. }
  163. #endif
  164. #endif /* not Vector_DEF_INCLUDED */