home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdarg.h>
- #include "sets.h"
- /***************************************************************************/
- int set_intersect(set *intersect, set *set1, set *set2)
- /***************************************************************************/
- /* This procedure computes the intersection (set-product) of two sets. The
- sets must have the same base_type in order for this to work. The input
- parameters are left unchanged.
- */
- {
- int i;
- defset(temp,UNIVERSAL,MAX_MEMBERS,0);
- set *temp2,*temp3;
-
- /* first check for the same base_types and tags */
- if(!cmp_base_types(set1,set2) || !cmp_base_types(set1,intersect) ||
- !cmp_set_tags(set1,set2) || !cmp_set_tags(set1,intersect))
- return FAILURE;
-
- /* The intersection of two sets is a set whose members are members only of
- both the intersecting sets. Determine the intersection and place it in
- the output set called 'intersect.'
- */
-
- /* next clear the output set */
- set_clear(&temp);
-
- /* next check for empty sets on the input. If so, return empty set. */
- if(set1->nmembers == 0 || set2->nmembers == 0)
- return SUCCESS;
-
- /* Since all sets include member locations starting at 0 and increasing
- toward set_size - 1, the intersection of two sets can be found by
- comparing the bits in the member records starting at bit 0 or, in this
- implementation, starting with the first member word. The matching
- contents of member words can be determined simply by a bitwise-and
- operation.
- */
- /* get the set with the smallest set_size */
- temp2 = (set1->set_size > set2->set_size) ? set2 : set1;
- if(temp2 == set1)
- temp3 = set2;
- else
- temp3 = set1;
-
- /* temp2 points to the smallest set, temp3, the larger. The set,
- 'intersect' must be at least the size of temp2. Check this.
- */
- if(temp.set_size < temp2->set_size)
- return FAILURE;
-
- /* Bitwise-and the respective member words of the intersecting sets
- and assign the result to the output set. Note that the smaller set
- will have ALL bits at member locations higher than nmembers reset
- to zero.
- */
- for(i=0;i<temp2->member_recs;i++)
- temp.word[i] = temp2->word[i] & temp3->word[i];
-
- /* correct the member count in the set record */
- temp.nmembers = set_member_count(&temp);
-
- /* assign temp to intersect */
- set_assign(intersect,&temp);
-
- /* done. */
- return SUCCESS;
-
- } /* end set_intersect */
-
-