list.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. while(p->next != nullptr)
  42. {
  43. if (p->next->shape == s)
  44. {
  45. this->count --;
  46. p->next = p->next->next;
  47. free(p->next);
  48. return;
  49. }
  50. p = p->next;
  51. }
  52. }
  53. void append(Shape *s)
  54. {
  55. ChainList *theNew = (ChainList *)calloc(1, sizeof(ChainList));
  56. theNew->shape = s;
  57. ChainList *p = this->tail;
  58. tail = theNew;
  59. if (p != nullptr) { p->next = theNew; }
  60. else { head = theNew; } /* If the tail is empty, it mean the list IS empty. */
  61. this->count ++;
  62. }
  63. bool isEmpty()
  64. {
  65. return (this->count == 0);
  66. }
  67. bool doesInclude(Shape *s)
  68. {
  69. ChainList *p = this->head;
  70. while(p != nullptr)
  71. {
  72. if (p->shape == s) { return true; }
  73. p = p->next;
  74. }
  75. return false;
  76. }
  77. };
  78. #endif //DORAYME_LIST_H