home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdarg.h>
- #include "sets.h"
- /***************************************************************************/
- int set_union(set *xunion, set *set1, set *set2)
- /***************************************************************************/
- /* This procedure computes the union (set-sum) 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 */
- if(!cmp_base_types(set1,set2) || !cmp_base_types(set1,xunion) ||
- !cmp_set_tags(set1,set2) || !cmp_set_tags(set1,xunion))
- return FAILURE;
-
- /* next clear the output set */
- set_clear(&temp);
-
- /* The union of two sets is a set whose members are all the members of
- both the sets. The union is a set having the same base_type as
- the two adding sets and having a set_size equal to the largest
- set_size of the two sets.
- */
-
- /* Assign the largest input set to the output set then add the smaller
- set to the output. 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,
- 'temp' must be at least the size of temp3. Check this.
- */
- if(temp.member_recs < temp3->member_recs)
- return FAILURE;
-
- /* do the assignment */
- if(set_assign(&temp, temp3) == FAILURE)
- return FAILURE;
-
- /* Add temp2 to union by bitwise-oring the respective member words of the
- two sets and assign the result to the output set, union. 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] = temp.word[i] | temp2->word[i];
-
- /* correct the member count in the set record */
- temp.nmembers = set_member_count(&temp);
-
- /* now copy temp to the output set */
- if(set_assign(xunion,&temp) == FAILURE)
- return FAILURE;
-
- /* done. */
- return SUCCESS;
-
- } /* end set_union */
-
-