every.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "mem.h"
  2. #include "every.h"
  3. /** This file contains two iterator or iterator interfaces,
  4. namely Every and Each. Every uses a struct to
  5. keep track of it's state, while Each uses a callback function. */
  6. /** Allocates an uninitialized every object. */
  7. Every * every_alloc() {
  8. return STRUCT_ALLOC(Every);
  9. }
  10. /** Deinitializes an every object. */
  11. Every * every_done(Every * self) {
  12. if(!self) return NULL;
  13. return self;
  14. }
  15. /** Frees an every object. */
  16. Every * every_free(Every * self) {
  17. every_done(self);
  18. mem_free(self);
  19. return NULL;
  20. }
  21. /** Initializes an every object. */
  22. Every * every_init(Every * self, EveryActs * acts) {
  23. if(!self) return NULL;
  24. self->acts = acts;
  25. if(self->acts->init)
  26. return self->acts->init(self);
  27. return self;
  28. }
  29. /** Creates a new every object. */
  30. Every * every_new(EveryActs * acts) {
  31. Every * self = every_alloc();
  32. return every_init(self, acts);
  33. }
  34. /** Gets the current object pointed to by the Every object. */
  35. void * every_get(Every * self) {
  36. if(!self) return NULL;
  37. return self->now;
  38. }
  39. /** Puts an object at the current position if possible.
  40. * Returns NULL if the put failed.
  41. */
  42. void * every_put(Every * self, void * data) {
  43. if(!self) return NULL;
  44. if(self->acts->put) {
  45. return self->acts->put(self, data);
  46. }
  47. return NULL;
  48. }
  49. /** Moves on to the next object and returns it.
  50. * Return NULL if no next object.
  51. */
  52. Every * every_next(Every * self, void * data) {
  53. if(self->acts->next) {
  54. self->acts->next(self);
  55. return every_get(self);
  56. }
  57. return NULL;
  58. }
  59. /** Initializes an EachElement */
  60. Each * each_init(Each * self, void * on, void * data) {
  61. if(!self) return NULL;
  62. self->on = on;
  63. self->extra = data;
  64. self->now = NULL;
  65. self->index = -1;
  66. return self;
  67. }
  68. /** Moves on to next element, incrementing index. */
  69. Each * each_next(Each * self, void * now) {
  70. if(!self) return NULL;
  71. self->now = now;
  72. self->index++;
  73. return self;
  74. }
  75. /** Gets now pointer of each struct */
  76. void * each_now(Each * self) {
  77. if(!self) return NULL;
  78. return self->now;
  79. }
  80. /** Gets on pointer of each struct */
  81. void * each_on(Each * self) {
  82. if(!self) return NULL;
  83. return self->on;
  84. }
  85. /** Gets extra data pointer of each struct */
  86. void * each_extra(Each * self) {
  87. if(!self) return NULL;
  88. return self->extra;
  89. }
  90. /** Gets index pointer of each struct, -1 if the struct is NULL. */
  91. int each_index(Each * self) {
  92. if(!self) return -1;
  93. return self->index;
  94. }