object.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * File: object.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 7 décembre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "object.h"
  10. /* --- Fonctions publiques --- */
  11. void object_ini(Object* this){
  12. this->first = NULL;
  13. this->last = NULL;
  14. this->size = 0;
  15. }
  16. obj_node* object_add(Object* this, int type, int x, int y){
  17. obj_node* objn;
  18. //Ajout des vthiseurs
  19. objn = malloc(sizeof(obj_node));
  20. objn->type = type;
  21. objn->x = x;
  22. objn->y = y;
  23. //Lien entre les noeuds
  24. objn->next = NULL;
  25. if (this->first == NULL) {
  26. objn->prev = NULL;
  27. this->first = objn;
  28. this->last = objn;
  29. } else {
  30. objn->prev = this->last;
  31. this->last->next = objn;
  32. this->last = objn;
  33. }
  34. this->size++;
  35. return objn;
  36. }
  37. obj_node* object_search(Object* this, int type, int x, int y){
  38. //Si liste vide
  39. if(this->size == 0){
  40. return NULL;
  41. }
  42. //Recherche
  43. obj_node* objn = this->first;
  44. while(objn != NULL){
  45. if(objn->type == type && objn->x == x && objn->y == y){
  46. return objn;
  47. }
  48. objn = objn->next;
  49. }
  50. return NULL;
  51. }
  52. void object_delete(Object* this, obj_node* objn){
  53. //Liste vide
  54. if(this->first == NULL){
  55. return;
  56. }
  57. //Si 1er et seul
  58. if (objn->prev == NULL && objn->next == NULL) {
  59. this->first = NULL;
  60. this->last = NULL;
  61. }
  62. //Si 1er et non seul
  63. else if (objn->prev == NULL && objn->next != NULL) {
  64. objn->next->prev = NULL;
  65. this->first = objn->next;
  66. }
  67. //Si dernier
  68. else if (objn->next == NULL) {
  69. objn->prev->next = NULL;
  70. this->last = objn->prev;
  71. }
  72. //Sinon si il est au milieu
  73. else {
  74. objn->prev->next = objn->next;
  75. objn->next->prev = objn->prev;
  76. }
  77. //Free
  78. free(objn);
  79. this->size--;
  80. }
  81. void object_clean(Object* this){
  82. //Liste vide
  83. if(this->first == NULL){
  84. return;
  85. }
  86. //Vide liste
  87. obj_node* tmp, * objn = this->first;
  88. while (objn != NULL) {
  89. tmp = objn->next;
  90. object_delete(this, objn);
  91. objn = tmp;
  92. }
  93. this->first = NULL;
  94. this->last = NULL;
  95. this->size = 0;
  96. }