collection.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * collection.h
  3. *
  4. * Copyright (C) 2009 Hector Martin <hector@marcansoft.com>
  5. * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef COLLECTION_H
  22. #define COLLECTION_H
  23. struct collection {
  24. void **list;
  25. int capacity;
  26. };
  27. void collection_init(struct collection *col);
  28. void collection_add(struct collection *col, void *element);
  29. void collection_remove(struct collection *col, void *element);
  30. int collection_count(struct collection *col);
  31. void collection_free(struct collection *col);
  32. #define FOREACH(var, col) \
  33. do { \
  34. int _iter; \
  35. for(_iter=0; _iter<(col)->capacity; _iter++) { \
  36. if(!(col)->list[_iter]) continue; \
  37. var = (col)->list[_iter];
  38. #define ENDFOREACH \
  39. } \
  40. } while(0);
  41. #endif