Re: SET ADT
From: Ben Pfaff (blp_at_cs.stanford.edu)
Date: 09/29/04
- Next message: Dave Vandervies: "Re: Clearly, it is too late to fix c99 - C is dead"
- Previous message: Mark McIntyre: "Re: Clearly, it is too late to fix c99 - C is dead"
- In reply to: dieymir: "SET ADT"
- Next in thread: Xenos: "Re: SET ADT"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Sep 2004 14:48:59 -0700
dieymir@yahoo.es (dieymir) writes:
> Anybody has any idea about how to define a mathematical set in C (like
> the Pascal one SET ... OF ...) with operations like union,
> intersection, pertenence ...
Normally this is done with a bitmap. You assign each member of
the set to a bit. Then union can be taken with the | operator,
intersection with &. I don't know what "pertenence" is.
Here's a worked example. Suppose you have a set of fruit that
might contain an apple, an orange, or a pear. Then give each of
those a bit:
#define APPLE (1u << 0)
#define ORANGE (1u << 1)
#define PEAR (1u << 2)
unsigned basket1 = APPLE; /* Just an apple. */
unsigned basket2 = ORANGE | PEAR; /* An orange and a pear. */
unsigned both_baskets = basket1 | basket2; /* Union. */
/* Is there an apple in basket2? */
if (basket2 & APPLE) { ... }
/* Remove an orange from basket2. */
basket2 &= ~ORANGE;
...etc...
-- "Some people *are* arrogant, and others read the FAQ." --Chris Dollin
- Next message: Dave Vandervies: "Re: Clearly, it is too late to fix c99 - C is dead"
- Previous message: Mark McIntyre: "Re: Clearly, it is too late to fix c99 - C is dead"
- In reply to: dieymir: "SET ADT"
- Next in thread: Xenos: "Re: SET ADT"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|