/********************************************************
BCL Example Problems
====================
file xbsetops.c
```````````````
Set operations.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Jan. 2000
********************************************************/
#include <stdio.h>
#include "xprb.h"
/* Two C functions are defined for union and intersection of (index) sets.
Firstly, the new index set is created, then the elements are added,
here by using function XPRBaddidxel. BCL also offers the possibility
to attribute names to the new index sets. */
/* Define arrays with index names */
char *city_names[]={"rome", "bristol", "london", "paris", "liverpool"};
char *port_names[]={"plymouth", "bristol", "glasgow", "london", "calais", "liverpool"};
/***********************************************************************/
/**** Create the union of two index sets ****/
void createunion(XPRBprob prob, XPRBidxset a, XPRBidxset b, XPRBidxset *c,char *name)
{
int i;
*c=XPRBnewidxset(prob,name,XPRBgetidxsetsize(a)+XPRBgetidxsetsize(b));
for(i=0;i<XPRBgetidxsetsize(a);i++)
XPRBaddidxel(*c,XPRBgetidxelname(a,i));
for(i=0;i<XPRBgetidxsetsize(b);i++)
XPRBaddidxel(*c,XPRBgetidxelname(b,i));
}
/**** Create the intersection of two index sets ****/
void createinter(XPRBprob prob, XPRBidxset a, XPRBidxset b, XPRBidxset *c,char *name)
{
int i;
*c=XPRBnewidxset(prob,name,
XPRBgetidxsetsize(a)<XPRBgetidxsetsize(b)?XPRBgetidxsetsize(a):XPRBgetidxsetsize(b));
for(i=0;i<XPRBgetidxsetsize(a);i++)
if(XPRBgetidxel(b,XPRBgetidxelname(a,i))>=0)
XPRBaddidxel(*c,XPRBgetidxelname(a,i));
}
/***********************************************************************/
int main(int argc, char **argv)
{
XPRBidxset ports, cities, both, places;
int i;
XPRBprob prob;
prob=XPRBnewprob("Setops"); /* Initialize BCL */
/* Create sets "cities" and "ports" and add the indices */
cities=XPRBnewidxset(prob,"cities",5);
for(i=0;i<5;i++) XPRBaddidxel(cities,city_names[i]);
ports=XPRBnewidxset(prob,"ports",6);
for(i=0;i<6;i++) XPRBaddidxel(ports,port_names[i]);
/* Create the union of "cities" and "ports" and print it */
createunion(prob,cities,ports,&places,"places");
XPRBprintidxset(places);
/* Create the intersection of "cities" and "ports", print it */
createinter(prob,cities,ports,&both,"bothsets");
XPRBprintidxset(both);
return 0;
}
|