ListBase.h 2.9 KB

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