Guest User

Untitled

a guest
Dec 11th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. /***************************
  2.  ********** sets.h *********
  3.  ***************************/
  4.  
  5. #ifndef SETS_H
  6. #define SETS_H
  7.  
  8.  
  9. /*
  10.  * The following typedef establishes a 'handle'
  11.  * for struct set_t* allowing for modules that
  12.  * link against this module to be able to
  13.  * handle pointers to struct set_t, but they
  14.  * cannot access it's data members directly.
  15.  */
  16.  
  17. typedef struct set_t* set_h;
  18.  
  19.  
  20. void
  21. set_insert(int, struct set_t*);
  22.  
  23. void
  24. set_remove(int, struct set_t*);
  25.  
  26.  
  27. #endif /* SETS_H */
  28.  
  29. /***************************
  30.  ********** sets.c *********
  31.  ***************************/
  32.  
  33. #include "sets.h"
  34. #include <assert.h>
  35. #define WORD_SIZE 15
  36. #define NBITS 32
  37.  
  38.  
  39. /*********************************
  40.  *** Encapsulated Implentation ***
  41.  *********************************/
  42.  
  43.  
  44. struct set_t {
  45.  
  46.   unsigned short bits[NBITS];
  47.  
  48. };
  49.  
  50. static unsigned short
  51. bit_or(unsigned short v1, unsigned short v2)
  52. {
  53.   return (v1 | v2);
  54. }
  55.  
  56. static unsigned short
  57. bit_and(unsigned short v1, unsigned short v2)
  58. {
  59.   return (v1 & v2);
  60. }
  61.  
  62.  
  63. static void
  64. set_mutate( int val
  65.       , unsigned short mask
  66.       , struct set_t *pset
  67.       , unsigned short (*bit_op)(unsigned short, unsigned short))
  68. {
  69.   assert((val >= 0) && (val < 512));
  70.  
  71.   int target_ix = val / WORD_SIZE;
  72.   int ix;
  73.  
  74.   for (ix = 0; ix <= target_ix; ++ix) {
  75.     if (ix == target_ix) {
  76.       pset->bits[ix] = bit_op(pset->bits[ix], mask);
  77.     }
  78.   }
  79. }
  80.  
  81. /*****************************
  82.  **** Interface Functions ****
  83.  *****************************/
  84.  
  85.  
  86. void
  87. set_remove(int val, struct set_t *pset)
  88. {
  89.   unsigned short mask = ~(1 << val / WORD_SIZE);
  90.  
  91.   set_mutate(val, mask, pset, bit_and);
  92.  
  93. }
Add Comment
Please, Sign In to add comment