Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #ifndef ListH
  2. #define ListH
  3.  
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define LIST_DECLARE_EX(n,t,r) \
  8.     typedef struct list_##n { t* v; unsigned c, a; } list_##n; \
  9.     void list_init_##n (list_##n * list); \
  10.     void list_clear_##n (list_##n * list); \
  11.     int list_add_##n (list_##n * list, r item); \
  12.     void list_remove_##n (list_##n * list, r item); \
  13.     void list_remove_at_##n (list_##n * list, unsigned index); \
  14.     void list_pack_##n (list_##n * list); \
  15.     int list_find_##n (list_##n * list, r item, unsigned* idx); \
  16.     int list_find_using_##n (list_##n * list, unsigned* idx, int (*pred)(r, void*), void* data); \
  17.     void list_foreach_##n (list_##n * list, int (*func)(r, void*), void* data);
  18.  
  19. #define LIST_IMPLEMENT_EX(n,t,r,cmp,inc) \
  20.     void list_init_##n (list_##n * list) { memset(list, 0, sizeof(list_##n)); } \
  21.     void list_clear_##n (list_##n * list) { \
  22.         free(list->v); list->v = NULL; list->a = list->c = 0; } \
  23.     int list_add_##n (list_##n * list, r item) {\
  24.         if (list->c == list->a) { \
  25.             t* nt; \
  26.             list->a += inc; \
  27.             nt = realloc(list->v, sizeof(t)*list->a); \
  28.             if (!nt) { \
  29.                 list->a -= inc; \
  30.                 return 0; \
  31.             } \
  32.             list->v = nt; \
  33.         } \
  34.         list->v[list->c++] = item; \
  35.         return 1; \
  36.     } \
  37.     void list_remove_##n (list_##n * list, r item) { \
  38.         unsigned idx; \
  39.         if (list_find_##n(list, item, &idx)) \
  40.             list_remove_at_##n(list, idx); \
  41.     } \
  42.     void list_remove_at_##n (list_##n * list, unsigned index) { \
  43.         unsigned i; \
  44.         if (index >= list->c) return; \
  45.         list->c--; \
  46.         for (i=index; i < list->c; i++) list->v[i] = list->v[i + 1]; \
  47.     } \
  48.     void list_pack_##n (list_##n * list) { \
  49.         if (list->c && list->c != list->a) { \
  50.             t* nt = realloc(list->v, sizeof(t)*list->c); \
  51.             if (nt) { \
  52.                 list->a = list->c; \
  53.                 list->v = nt; \
  54.             } \
  55.             list->v = nt; \
  56.         } \
  57.     } \
  58.     int list_find_##n (list_##n * list, r item, unsigned* idx) { \
  59.         unsigned i; \
  60.         for (i=0; i < list->c; i++) \
  61.             cmp \
  62.                 { *idx = i; return 1; } \
  63.         return 0; \
  64.     } \
  65.     int list_find_using_##n (list_##n * list, unsigned* idx, int (*pred)(r, void*), void* data) { \
  66.         unsigned i; \
  67.         for (i=0; i < list->c; i++) \
  68.             if (pred(list->v[i], data)) { *idx = i; return 1; } \
  69.         return 0; \
  70.     } \
  71.     void list_foreach_##n (list_##n * list, int (*func)(r, void*), void* data) { \
  72.         unsigned i; \
  73.         for (i=0; i < list->c; i++) func(list->v[i], data); \
  74.     }
  75.  
  76. #define LIST_CMP_SIMPLE if (item == list->v[i])
  77. #define LIST_CMP_STRUCT if (!memcmp(&item, list->v + i, sizeof(item)))
  78.  
  79. #define LIST_DECLARE_SIMPLE(n,t) LIST_DECLARE_EX(n,t,t)
  80. #define LIST_DECLARE_STRUCT(n,t) LIST_DECLARE_EX(n,t,t)
  81. #define LIST_IMPLEMENT_SIMPLE(n,t) LIST_IMPLEMENT_EX(n,t,t,LIST_CMP_SIMPLE,16)
  82. #define LIST_IMPLEMENT_STRUCT(n,t) LIST_IMPLEMENT_EX(n,t,t,LIST_CMP_STRUCT,4)
  83.  
  84. LIST_DECLARE_SIMPLE(ptr,void*)
  85.  
  86. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement