Re: SET ADT

From: Ben Pfaff (blp_at_cs.stanford.edu)
Date: 09/29/04


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


Relevant Pages

  • SET ADT
    ... Anybody has any idea about how to define a mathematical set in C (like ... with operations like union, ... intersection, pertenence ... ...
    (comp.lang.c)
  • Re: How to decide on the meet operator in Data-flow Analysis?
    ... In an iterative data flow analysis the solution obtained will be less ... where meet operator is U (union) ... where the meet operator is (intersection) ... Note that the algorithm gives you "MUST" information. ...
    (comp.compilers)
  • Re: Corners in metric spaces
    ... and a ball are both smooth in an intuitive sense. ... Neither has their union ... the intersection of B_i and boundary of S is empty ... to get scetch of proof or counterexample of Sentence 2 ...
    (sci.math)
  • Re: Junk Dna!
    ... elements are not present at internal nodes in a nested hierarchy. ... consequence of the definition of intersection. ... A, can be in union with B,C & D at the same time! ...
    (talk.origins)
  • a set family indexed by an empty set
    ... intersection of A indexed by i. ... The textbook says that "if you read the definitions of union ... belong to *at least* one A_i. ... to belong to the resulting set, a member m ...
    (sci.math)