Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <stdbool.h>
  4.  
  5. /** A data table, with fast membership testing.
  6. *
  7. * Data tables are static arrays of data, with keys ranging from 0 to N-1. To
  8. * distinguish unset elements from 0 values, tables also contain an array
  9. * indicating whether a value has been set.
  10. *
  11. * The total size of a table is (N*sizeof(T))+N bytes. To avoid the overhead
  12. * of using two arrays, see optional.h, which trades value range for a
  13. * very compact representation of "optional" values.
  14. */
  15. #define data_table(T, N) \
  16. struct { bool isset[N]; int data[N]; }
  17.  
  18. #define data_table_create(T, N) \
  19. ((data_table (T, N)) { 0, 0 })
  20.  
  21. /** Set table value for key.
  22. */
  23. #define data_table_set(i, x, t) \
  24. ({ \
  25. let _i = i; \
  26. let _x = x; \
  27. (t)->data[_i] = _x; \
  28. (t)->isset[_i] = 1; \
  29. })
  30.  
  31. /** Unset table value for key.
  32. */
  33. #define data_table_unset(i, t) \
  34. ({ \
  35. let _i = i; \
  36. (t)->isset[_i] = 0; \
  37. })
  38.  
  39. /** Test whether key has value. */
  40. #define data_table_isset(i, t) \
  41. ({ let _i = i; (t)->isset[_i]; })
  42.  
  43. /** Retrieve key value. */
  44. #define data_table_get(i, t) \
  45. ({ let _i = i; (t)->data[_i]; })
  46.  
  47. /** Define a static table. */
  48. #define deftable(NAME, T, N) \
  49. static let tbl_ ## NAME = data_table_create (T, N)
  50.  
  51. /** Refer to a static table. */
  52. #define t(NAME) &(tbl_ ## NAME)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement