VectorBase.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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> class RandomAccessIterator
  27. {
  28. public:
  29. /// Construct.
  30. RandomAccessIterator() :
  31. ptr_(0)
  32. {
  33. }
  34. /// Construct with an object pointer.
  35. explicit RandomAccessIterator(T* ptr) :
  36. ptr_(ptr)
  37. {
  38. }
  39. /// Point to the object.
  40. T* operator -> () const { return ptr_; }
  41. /// Dereference the object.
  42. T& operator * () const { return *ptr_; }
  43. /// Preincrement the pointer.
  44. RandomAccessIterator<T>& operator ++ () { ++ptr_; return *this; }
  45. /// Postincrement the pointer.
  46. RandomAccessIterator<T> operator ++ (int) { RandomAccessIterator<T> it = *this; ++ptr_; return it; }
  47. /// Predecrement the pointer.
  48. RandomAccessIterator<T>& operator -- () { --ptr_; return *this; }
  49. /// Postdecrement the pointer.
  50. RandomAccessIterator<T> operator -- (int) { RandomAccessIterator<T> it = *this; --ptr_; return it; }
  51. /// Add an offset to the pointer.
  52. RandomAccessIterator<T>& operator += (int value) { ptr_ += value; return *this; }
  53. /// Subtract an offset from the pointer.
  54. RandomAccessIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
  55. /// Add an offset to the pointer.
  56. RandomAccessIterator<T> operator + (int value) const { return RandomAccessIterator<T>(ptr_ + value); }
  57. /// Subtract an offset from the pointer.
  58. RandomAccessIterator<T> operator - (int value) const { return RandomAccessIterator<T>(ptr_ - value); }
  59. /// Calculate offset to another iterator.
  60. int operator - (const RandomAccessIterator& rhs) const { return ptr_ - rhs.ptr_; }
  61. /// Test for equality with another iterator.
  62. bool operator == (const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
  63. /// Test for inequality with another iterator.
  64. bool operator != (const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
  65. /// Test for less than with another iterator.
  66. bool operator < (const RandomAccessIterator& rhs) const { return ptr_ < rhs.ptr_; }
  67. /// Test for greater than with another iterator.
  68. bool operator > (const RandomAccessIterator& rhs) const { return ptr_ > rhs.ptr_; }
  69. /// Test for less than or equal with another iterator.
  70. bool operator <= (const RandomAccessIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  71. /// Test for greater than or equal with another iterator.
  72. bool operator >= (const RandomAccessIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  73. /// Pointer.
  74. T* ptr_;
  75. };
  76. /// Random access const iterator.
  77. template <class T> class RandomAccessConstIterator
  78. {
  79. public:
  80. /// Construct.
  81. RandomAccessConstIterator() :
  82. ptr_(0)
  83. {
  84. }
  85. /// Construct with an object pointer.
  86. explicit RandomAccessConstIterator(T* ptr) :
  87. ptr_(ptr)
  88. {
  89. }
  90. /// Construct from a non-const iterator.
  91. RandomAccessConstIterator(const RandomAccessIterator<T>& rhs) :
  92. ptr_(rhs.ptr_)
  93. {
  94. }
  95. /// Assign from a non-const iterator.
  96. RandomAccessConstIterator<T>& operator = (const RandomAccessIterator<T>& rhs) { ptr_ = rhs.ptr_; return *this; }
  97. /// Point to the object.
  98. const T* operator -> () const { return ptr_; }
  99. /// Dereference the object.
  100. const T& operator * () const { return *ptr_; }
  101. /// Preincrement the pointer.
  102. RandomAccessConstIterator<T>& operator ++ () { ++ptr_; return *this; }
  103. /// Postincrement the pointer.
  104. RandomAccessConstIterator<T> operator ++ (int) { RandomAccessConstIterator<T> it = *this; ++ptr_; return it; }
  105. /// Predecrement the pointer.
  106. RandomAccessConstIterator<T>& operator -- () { --ptr_; return *this; }
  107. /// Postdecrement the pointer.
  108. RandomAccessConstIterator<T> operator -- (int) { RandomAccessConstIterator<T> it = *this; --ptr_; return it; }
  109. /// Add an offset to the pointer.
  110. RandomAccessConstIterator<T>& operator += (int value) { ptr_ += value; return *this; }
  111. /// Subtract an offset from the pointer.
  112. RandomAccessConstIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
  113. /// Add an offset to the pointer.
  114. RandomAccessConstIterator<T> operator + (int value) const { return RandomAccessConstIterator<T>(ptr_ + value); }
  115. /// Subtract an offset from the pointer.
  116. RandomAccessConstIterator<T> operator - (int value) const { return RandomAccessConstIterator<T>(ptr_ - value); }
  117. /// Calculate offset to another iterator.
  118. int operator - (const RandomAccessConstIterator& rhs) const { return ptr_ - rhs.ptr_; }
  119. /// Test for equality with another iterator.
  120. bool operator == (const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
  121. /// Test for inequality with another iterator.
  122. bool operator != (const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
  123. /// Test for less than with another iterator.
  124. bool operator < (const RandomAccessConstIterator& rhs) const { return ptr_ < rhs.ptr_; }
  125. /// Test for greater than with another iterator.
  126. bool operator > (const RandomAccessConstIterator& rhs) const { return ptr_ > rhs.ptr_; }
  127. /// Test for less than or equal with another iterator.
  128. bool operator <= (const RandomAccessConstIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  129. /// Test for greater than or equal with another iterator.
  130. bool operator >= (const RandomAccessConstIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  131. /// Pointer.
  132. T* ptr_;
  133. };
  134. /// %Vector base class.
  135. class VectorBase
  136. {
  137. public:
  138. /// Construct.
  139. VectorBase() :
  140. size_(0),
  141. capacity_(0),
  142. buffer_(0)
  143. {
  144. }
  145. /// Swap with another vector.
  146. void Swap(VectorBase& rhs)
  147. {
  148. ::Swap(size_, rhs.size_);
  149. ::Swap(capacity_, rhs.capacity_);
  150. ::Swap(buffer_, rhs.buffer_);
  151. }
  152. protected:
  153. /// Size of vector.
  154. unsigned size_;
  155. /// Buffer capacity.
  156. unsigned capacity_;
  157. /// Buffer.
  158. unsigned char* buffer_;
  159. };