list.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * List header
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #ifndef DORAYME_LIST_H
  10. #define DORAYME_LIST_H
  11. #include <shape.h>
  12. struct ChainList
  13. {
  14. Shape *shape;
  15. ChainList *next;
  16. };
  17. class List
  18. {
  19. private:
  20. ChainList *head;
  21. ChainList *tail;
  22. uint32_t count;
  23. public:
  24. List() : head(nullptr), tail(nullptr), count(0) { };
  25. ~List()
  26. {
  27. ChainList *p = this->head;
  28. if (p == nullptr) { return; }
  29. /* clear up the list */
  30. }
  31. Shape *last()
  32. {
  33. ChainList *p = this->tail;
  34. if (p == nullptr) { return nullptr; }
  35. return p->shape;
  36. }
  37. void remove(Shape *s)
  38. {
  39. ChainList *p = this->head;
  40. if (p == nullptr) { return; }
  41. if ((p->next == nullptr) && (p->shape == s))
  42. {
  43. /* First element */
  44. this->tail = nullptr;
  45. free(this->head);
  46. this->head = nullptr;
  47. this->count = 0;
  48. return;
  49. }
  50. while(p->next != nullptr)
  51. {
  52. if (p->next->shape == s)
  53. {
  54. ChainList *found = p->next;
  55. p->next = p->next->next;
  56. free(found);
  57. if (p->next == NULL) { this->tail = p; }
  58. this->count --;
  59. return;
  60. }
  61. p = p->next;
  62. }
  63. }
  64. void append(Shape *s)
  65. {
  66. ChainList *theNew = (ChainList *)calloc(1, sizeof(ChainList));
  67. theNew->shape = s;
  68. ChainList *p = this->tail;
  69. this->tail = theNew;
  70. if (p != nullptr) { p->next = theNew; }
  71. else { this->head = theNew; } /* If the tail is empty, it mean the list IS empty. */
  72. this->count ++;
  73. }
  74. bool isEmpty()
  75. {
  76. return (this->count == 0);
  77. }
  78. bool doesInclude(Shape *s)
  79. {
  80. ChainList *p = this->head;
  81. while(p != nullptr)
  82. {
  83. if (p->shape == s) { return true; }
  84. p = p->next;
  85. }
  86. return false;
  87. }
  88. };
  89. #endif //DORAYME_LIST_H