Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************
- ********** sets.h *********
- ***************************/
- #ifndef SETS_H
- #define SETS_H
- /*
- * The following typedef establishes a 'handle'
- * for struct set_t* allowing for modules that
- * link against this module to be able to
- * handle pointers to struct set_t, but they
- * cannot access it's data members directly.
- */
- typedef struct set_t* set_h;
- void
- set_insert(int, struct set_t*);
- void
- set_remove(int, struct set_t*);
- #endif /* SETS_H */
- /***************************
- ********** sets.c *********
- ***************************/
- #include "sets.h"
- #include <assert.h>
- #define WORD_SIZE 15
- #define NBITS 32
- /*********************************
- *** Encapsulated Implentation ***
- *********************************/
- struct set_t {
- unsigned short bits[NBITS];
- };
- static unsigned short
- bit_or(unsigned short v1, unsigned short v2)
- {
- return (v1 | v2);
- }
- static unsigned short
- bit_and(unsigned short v1, unsigned short v2)
- {
- return (v1 & v2);
- }
- static void
- set_mutate( int val
- , unsigned short mask
- , struct set_t *pset
- , unsigned short (*bit_op)(unsigned short, unsigned short))
- {
- assert((val >= 0) && (val < 512));
- int target_ix = val / WORD_SIZE;
- int ix;
- for (ix = 0; ix <= target_ix; ++ix) {
- if (ix == target_ix) {
- pset->bits[ix] = bit_op(pset->bits[ix], mask);
- }
- }
- }
- /*****************************
- **** Interface Functions ****
- *****************************/
- void
- set_remove(int val, struct set_t *pset)
- {
- unsigned short mask = ~(1 << val / WORD_SIZE);
- set_mutate(val, mask, pset, bit_and);
- }
Add Comment
Please, Sign In to add comment