VectorBase.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Swap.h"
  25. /// Random access iterator.
  26. template <class T> struct RandomAccessIterator
  27. {
  28. /// Construct.
  29. RandomAccessIterator() :
  30. ptr_(0)
  31. {
  32. }
  33. /// Construct with an object pointer.
  34. explicit RandomAccessIterator(T* ptr) :
  35. ptr_(ptr)
  36. {
  37. }
  38. /// Point to the object.
  39. T* operator -> () const { return ptr_; }
  40. /// Dereference the object.
  41. T& operator * () const { return *ptr_; }
  42. /// Preincrement the pointer.
  43. RandomAccessIterator<T>& operator ++ () { ++ptr_; return *this; }
  44. /// Postincrement the pointer.
  45. RandomAccessIterator<T> operator ++ (int) { RandomAccessIterator<T> it = *this; ++ptr_; return it; }
  46. /// Predecrement the pointer.
  47. RandomAccessIterator<T>& operator -- () { --ptr_; return *this; }
  48. /// Postdecrement the pointer.
  49. RandomAccessIterator<T> operator -- (int) { RandomAccessIterator<T> it = *this; --ptr_; return it; }
  50. /// Add an offset to the pointer.
  51. RandomAccessIterator<T>& operator += (int value) { ptr_ += value; return *this; }
  52. /// Subtract an offset from the pointer.
  53. RandomAccessIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
  54. /// Add an offset to the pointer.
  55. RandomAccessIterator<T> operator + (int value) const { return RandomAccessIterator<T>(ptr_ + value); }
  56. /// Subtract an offset from the pointer.
  57. RandomAccessIterator<T> operator - (int value) const { return RandomAccessIterator<T>(ptr_ - value); }
  58. /// Calculate offset to another iterator.
  59. int operator - (const RandomAccessIterator& rhs) const { return ptr_ - rhs.ptr_; }
  60. /// Test for equality with another iterator.
  61. bool operator == (const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
  62. /// Test for inequality with another iterator.
  63. bool operator != (const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
  64. /// Test for less than with another iterator.
  65. bool operator < (const RandomAccessIterator& rhs) const { return ptr_ < rhs.ptr_; }
  66. /// Test for greater than with another iterator.
  67. bool operator > (const RandomAccessIterator& rhs) const { return ptr_ > rhs.ptr_; }
  68. /// Test for less than or equal with another iterator.
  69. bool operator <= (const RandomAccessIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  70. /// Test for greater than or equal with another iterator.
  71. bool operator >= (const RandomAccessIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  72. /// Pointer.
  73. T* ptr_;
  74. };
  75. /// Random access const iterator.
  76. template <class T> struct RandomAccessConstIterator
  77. {
  78. /// Construct.
  79. RandomAccessConstIterator() :
  80. ptr_(0)
  81. {
  82. }
  83. /// Construct with an object pointer.
  84. explicit RandomAccessConstIterator(T* ptr) :
  85. ptr_(ptr)
  86. {
  87. }
  88. /// Construct from a non-const iterator.
  89. RandomAccessConstIterator(const RandomAccessIterator<T>& rhs) :
  90. ptr_(rhs.ptr_)
  91. {
  92. }
  93. /// Assign from a non-const iterator.
  94. RandomAccessConstIterator<T>& operator = (const RandomAccessIterator<T>& rhs) { ptr_ = rhs.ptr_; return *this; }
  95. /// Point to the object.
  96. const T* operator -> () const { return ptr_; }
  97. /// Dereference the object.
  98. const T& operator * () const { return *ptr_; }
  99. /// Preincrement the pointer.
  100. RandomAccessConstIterator<T>& operator ++ () { ++ptr_; return *this; }
  101. /// Postincrement the pointer.
  102. RandomAccessConstIterator<T> operator ++ (int) { RandomAccessConstIterator<T> it = *this; ++ptr_; return it; }
  103. /// Predecrement the pointer.
  104. RandomAccessConstIterator<T>& operator -- () { --ptr_; return *this; }
  105. /// Postdecrement the pointer.
  106. RandomAccessConstIterator<T> operator -- (int) { RandomAccessConstIterator<T> it = *this; --ptr_; return it; }
  107. /// Add an offset to the pointer.
  108. RandomAccessConstIterator<T>& operator += (int value) { ptr_ += value; return *this; }
  109. /// Subtract an offset from the pointer.
  110. RandomAccessConstIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
  111. /// Add an offset to the pointer.
  112. RandomAccessConstIterator<T> operator + (int value) const { return RandomAccessConstIterator<T>(ptr_ + value); }
  113. /// Subtract an offset from the pointer.
  114. RandomAccessConstIterator<T> operator - (int value) const { return RandomAccessConstIterator<T>(ptr_ - value); }
  115. /// Calculate offset to another iterator.
  116. int operator - (const RandomAccessConstIterator& rhs) const { return ptr_ - rhs.ptr_; }
  117. /// Test for equality with another iterator.
  118. bool operator == (const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
  119. /// Test for inequality with another iterator.
  120. bool operator != (const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
  121. /// Test for less than with another iterator.
  122. bool operator < (const RandomAccessConstIterator& rhs) const { return ptr_ < rhs.ptr_; }
  123. /// Test for greater than with another iterator.
  124. bool operator > (const RandomAccessConstIterator& rhs) const { return ptr_ > rhs.ptr_; }
  125. /// Test for less than or equal with another iterator.
  126. bool operator <= (const RandomAccessConstIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  127. /// Test for greater than or equal with another iterator.
  128. bool operator >= (const RandomAccessConstIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  129. /// Pointer.
  130. T* ptr_;
  131. };
  132. /// %Vector base class.
  133. class VectorBase
  134. {
  135. public:
  136. /// Construct.
  137. VectorBase() :
  138. size_(0),
  139. capacity_(0),
  140. buffer_(0)
  141. {
  142. }
  143. /// Swap with another vector.
  144. void Swap(VectorBase& rhs)
  145. {
  146. ::Swap(size_, rhs.size_);
  147. ::Swap(capacity_, rhs.capacity_);
  148. ::Swap(buffer_, rhs.buffer_);
  149. }
  150. protected:
  151. /// Size of vector.
  152. unsigned size_;
  153. /// Buffer capacity.
  154. unsigned capacity_;
  155. /// Buffer.
  156. unsigned char* buffer_;
  157. };