PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/List.hpp

https://bitbucket.org/ayufan/ayuine2b
C++ Header | 88 lines | 55 code | 11 blank | 22 comment | 3 complexity | 732e9316d1159c939976a97f37f0e982 MD5 | raw file
  1. //------------------------------------//
  2. //
  3. // List.hpp
  4. //
  5. // Author: ayufan (ayufan[at]o2.pl)
  6. // Project: ayuine2
  7. // Date: 2006-9-2
  8. //
  9. //------------------------------------//
  10. #pragma once
  11. namespace ayuine
  12. {
  13. template<typename Type>
  14. class List
  15. {
  16. ValueType(List<Type>, 0);
  17. // Fields
  18. public:
  19. Type* Object;
  20. List<Type>* Next;
  21. List<Type>* Prev;
  22. // Constructor
  23. public:
  24. List(Type *object) {
  25. // Ustaw domyślną listę
  26. Object = object;
  27. Next = this;
  28. Prev = this;
  29. }
  30. // Destructor
  31. public:
  32. ~List() {
  33. // Odepnij listę
  34. unlink();
  35. }
  36. // Properties
  37. public:
  38. bool empty() const {
  39. return Next == Prev;
  40. }
  41. u32 count() const {
  42. u32 index = 0;
  43. for(List<Type>* curr = Next; curr != this; curr = curr->Next) {
  44. index++;
  45. }
  46. return index;
  47. }
  48. // Methods
  49. public:
  50. void linkFront(List<Type> &list) {
  51. // Podepnij listę z przodu
  52. Next = list.Next;
  53. Prev = &list;
  54. // Uaktualnij wskaźniki
  55. Next->Prev = this;
  56. Prev->Next = this;
  57. }
  58. void linkBack(List<Type> &list) {
  59. // Podepnij listę z tyłu
  60. Prev = list.Prev;
  61. Next = &list;
  62. // Uaktualnij wskaźniki
  63. Next->Prev = this;
  64. Prev->Next = this;
  65. }
  66. void unlink() {
  67. Assert(Next);
  68. Assert(Prev);
  69. // Odepnij listę
  70. Next->Prev = Prev;
  71. Prev->Next = Next;
  72. // Ustaw wartości domyślne
  73. Next = this;
  74. Prev = this;
  75. }
  76. };
  77. };