home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdarg.h>
- #include "sets.h"
- /***************************************************************************/
- int set_of(set *aset,...)
- /***************************************************************************/
- /* This function assigns a set of values to a set variable after clearing
- any contents of that variable. The variable's parameters must be able
- to accomodate the set of values.
-
- Base_Type Set_Size Nmembers Member Notes:
- Expression
- --------------------------------------------------------------------------
- CHARACTER 256 0 - Nmembers=0 means empty set.
- CHARACTER 256 1-256 Subranges See SUBRANGE, below.
- CHARACTER 256 1-256 member[i],member[j],...
- List members. Combine with
- Subrange(s).
- BOOLEAN 2 0 - Nmembers=0 means empty set.
- BOOLEAN 2 1-2 TRUE/FALSE One or two members containing
- the values for TRUE/FALSE.
- ENUMERATED m 0 - Nmembers=0 means empty set.
- ENUMERATED m n member[i],member[j],...
- List members.
- SUBRANGE m 0 - Nmembers=0 means empty set.
- SUBRANGE m n member[i]__member[j]
- Pascal subrange notation (almost).
- For CHARACTER, ENUMERATED, and
- decimal base_types. i..j is
- specified contiguously.
- SUBRANGE m n member[i],...,member[j]
- List members.
- -----------------------------------------------------------------------------
-
- General parameter list configuration:
-
- set_of(set-pointer,...members_list...,_E)
-
- where:- set-pointer is a pointer to a set structure defined by the
- macro, defset.
- - tag-value is a unique value for use by the programmer to force
- set type checking with each tag value representing a unique
- set-type.
- - members_list is any combination of members list and subrange.
- - nmembers is the number of members specified in the members_list.
- - nmembers = 0 means empty set and requires no members_list.
- - set_size is the largest possible number of members in the set.
- - Base_type is one of the four members of the enumerated type, base,
- and is the type against which checking is done in set operations.
- - a set variable is an empty set.
- - the term, "_E", is the end-of-set delimiter and must be used to
- terminate a members_list.
-
- ----------------------------------------------------------------------------
- */
- {
- va_list ap;
-
- /* clear the destination set */
- set_clear(aset);
-
- /* Enter the members into the member records. Parse
- the remainder of the parameter list, determine the actual parameter
- locations in the member records and set the appropriate bits to record
- the presence of the member in the set. NOTE: All member types are
- assumed to be integer. For compilers that treat char as integer, this
- will be no change. Some compilers will keep a char parameter at char
- length and some will "promote" a char variable to an integer parameter.
- ANSI C (X3J11) as defined in "C Wizard's Programming Reference" by
- W. David Schwaderer, Wiley Press, 1985, automatically promotes char or
- short variables to integer in actual parameter lists. (Note that byte
- type is not mentioned). We treat all members the same since all the
- other base_types can be treated as type integer.
-
- The parameter list is of unknown length. We don't know how the user
- arranged the set members or if he used subrange notation which greatly
- compresses the number of actual parameters despite a possibly large
- number of members.
-
- For subrange notation, the notation, "__", is converted to ",-1," so
- as to appear to be another integer parameter and 'a'__'z' becomes
- 'a',-1,'z'. This change is made by the #define statement in the header
- file, 'sets.h'. Since members can only be positive integers, the -1
- acts as a flag denoting a subrange bounded by the preceding and
- following members.
-
- To end the list of set members, the end of list symbol, _E, is used.
- This is converted by a #define statement in the header file to -2.
- NOTE: To enter an empty set, include the _E symbol without any preceding
- set members.
- */
- aset->nmembers = 0;
- va_start(ap,aset);
- if(set_args(aset,ap) == FAILURE)
- return FAILURE;
- va_end(ap);
-
- /* We're done. Return SUCCESS. */
- return SUCCESS;
-
- } /* end set_of */
-
-
- /***************************************************************************/
- int set_args(set *aset, va_list ap)
- /***************************************************************************/
- /* Extracts all the set members from a parameter list passed to the
- caller of this function.
- */
- {
- int j,parm,lastparm;
-
- j = 0;
- do {
- /* get a parameter -- NOTE: C-Enum types start at the zeroth member */
- if((parm = va_arg(ap,int)) >= 0)
- if(add_member(aset,parm) == FAILURE) /* individual member */
- return FAILURE;
- else
- lastparm = parm;
-
- else if(parm == -1)
- { /* "__" notation, a subrange */
- parm = va_arg(ap,int);
- for(j=lastparm+1;j<=parm;j++)
- if(add_member(aset,j) == FAILURE)
- return FAILURE;
- }
- }
- while(parm != -2);
- return SUCCESS;
-
- } /* end set_args */
-
-