VectorBase.h 6.3 KB

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