Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #pragma once
  2.  
  3. /** Define an "optional" value, where the first bit indicates
  4. * whether the value has been set and the remaining bits are used
  5. * for the actual value.
  6. *
  7. * The up-side of this definition is that it is equally space efficient
  8. * as simply storing an array of the underlying type.
  9. *
  10. * The down-side of this definition is that you lose a whole bit of possible
  11. * values. If it is important to be able to use the whole range of values, two
  12. * arrays (as in data_table.h) is more efficient (compared to using a larger
  13. * value type or relaxing the bit field width).
  14. *
  15. * An array of ints with an array for tracking which values have been set is
  16. * sizeof (int) * N + N bytes
  17. *
  18. * An array of optional (int) is
  19. *
  20. * sizeof (int) * N
  21. *
  22. * For, say, N = 1000, the first gives 5000 bytes, the second 4000.
  23. * Using optional (long) instead of int, however, would consume 8000 bytes,
  24. * more than doubling space usage compared to optional (int).
  25. */
  26. #define optional(T) \
  27. struct { \
  28. bool isset : 1; \
  29. T value : (sizeof (T) * 8 - 1); \
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement