every.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef EVERY_H_INCLUDED
  2. #define EVERY_H_INCLUDED
  3. /*
  4. * Every is an iterator that allows to iterate over every element of
  5. * a collection.
  6. */
  7. struct Every_;
  8. typedef struct Every_ Every;
  9. struct EveryActs_;
  10. typedef struct EveryActs_ EveryActs;
  11. // types for the funtion pointers that every uses
  12. typedef Every * (EveryNext(Every * every));
  13. typedef void * (EveryPut(Every * every, void * element));
  14. typedef void * (EveryDone(Every * every));
  15. typedef Every * (EveryInit(Every * every));
  16. // Struct with all function pointers for Every
  17. struct EveryActs_ {
  18. EveryDone * done;
  19. EveryInit * init;
  20. EveryNext * next;
  21. EveryPut * put;
  22. };
  23. /*
  24. * Every is a struct that can be used with collections like Dynar to fetch
  25. * every element of the collection.
  26. */
  27. struct Every_ {
  28. void * now;
  29. void * on;
  30. int index;
  31. void * extra;
  32. EveryActs * acts;
  33. };
  34. struct Each_;
  35. typedef struct Each_ Each;
  36. /*
  37. * Each is an alternative iterator interface, that
  38. * requires a function callback in stead of an object.
  39. */
  40. struct Each_ {
  41. void * on;
  42. void * now;
  43. void * extra;
  44. int index;
  45. };
  46. /* Function pointer for the each iteration method. */
  47. typedef void * (EachDo(Each * element));
  48. /* An even simpler iterator interface, one for simply iterating and one
  49. for searching. Iteration is stopped if AllData returns not-NULL,
  50. and the returned data will be returned by the function that uses AllData
  51. too. Extra is used for passing in extra data if any. */
  52. typedef void * (AllData)(void * data, void * extra);
  53. /* Generic comparator function. Much like strcmp. */
  54. typedef int (AllCompare)(void * one, void * two);
  55. Every * every_alloc (void);
  56. Every * every_done (Every * self );
  57. Every * every_free (Every * self );
  58. Every * every_init (Every * self , EveryActs * acts );
  59. Every * every_new (EveryActs * acts );
  60. void * every_get (Every * self );
  61. void * every_put (Every * self , void * data );
  62. Every * every_next (Every * self , void * data );
  63. Each * each_init (Each * self , void * on , void * data );
  64. Each * each_next (Each * self , void * now );
  65. void * each_now (Each * self );
  66. void * each_on (Each * self );
  67. void * each_extra (Each * self );
  68. int each_index (Each * self);
  69. /* Yet another iteration interface: a Walker is a simple callback function. */
  70. typedef void * (Walker)(void * element, void * extra);
  71. // and some helper macros
  72. #define EACH_NOW(EACH, TYPE) ((TYPE *) each_now(EACH))
  73. #endif