Initializing help system before first use

Setops - Index sets


Type: Programming
Rating: 2 (easy-medium)
Description: A example showing the use of index sets and in particular, creating the union and intersection of index sets.
File(s): xbsetops.c


xbsetops.c
/********************************************************
  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;
}

© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.