ListBase.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "Allocator.h"
  25. #include "Swap.h"
  26. /// %List node base.
  27. struct ListNodeBase
  28. {
  29. /// Construct.
  30. ListNodeBase() :
  31. prev_(0),
  32. next_(0)
  33. {
  34. }
  35. /// Previous node.
  36. ListNodeBase* prev_;
  37. /// Next node.
  38. ListNodeBase* next_;
  39. };
  40. /// %List iterator base class.
  41. class ListIteratorBase
  42. {
  43. public:
  44. /// Construct.
  45. ListIteratorBase() :
  46. ptr_(0)
  47. {
  48. }
  49. /// Construct with a node pointer.
  50. explicit ListIteratorBase(ListNodeBase* ptr) :
  51. ptr_(ptr)
  52. {
  53. }
  54. /// Test for equality with another iterator.
  55. bool operator == (const ListIteratorBase& rhs) const { return ptr_ == rhs.ptr_; }
  56. /// Test for inequality with another iterator.
  57. bool operator != (const ListIteratorBase& rhs) const { return ptr_ != rhs.ptr_; }
  58. /// Go to the next node.
  59. void GotoNext()
  60. {
  61. if (ptr_)
  62. ptr_ = ptr_->next_;
  63. }
  64. /// Go to the previous node.
  65. void GotoPrev()
  66. {
  67. if (ptr_)
  68. ptr_ = ptr_->prev_;
  69. }
  70. /// Node pointer.
  71. ListNodeBase* ptr_;
  72. };
  73. /// Linked list base class.
  74. class ListBase
  75. {
  76. public:
  77. /// Construct.
  78. ListBase() :
  79. allocator_(0),
  80. size_(0)
  81. {
  82. }
  83. /// Swap with another linked list.
  84. void Swap(ListBase& rhs)
  85. {
  86. ::Swap(head_, rhs.head_);
  87. ::Swap(tail_, rhs.tail_);
  88. ::Swap(allocator_, rhs.allocator_);
  89. ::Swap(size_, rhs.size_);
  90. }
  91. protected:
  92. /// Head node pointer.
  93. ListNodeBase* head_;
  94. /// Tail node pointer.
  95. ListNodeBase* tail_;
  96. /// Node allocator.
  97. AllocatorBlock* allocator_;
  98. /// Number of nodes.
  99. unsigned size_;
  100. };