ListBase.h 2.8 KB

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