/*
        State Space Exploration Program

        Common data types and macros.

        Module explore.h, Revision 2.0, Date 1996-08-19
*/

/*
        Useful macros
*/
#define MAX(a,b) ((a) > (b) ? (a) : (b))

/*
        These next few macros define a bag data type, a container
    of identically typed objects.
*/

/* type declaration template for a bag:

        typedef BAG_OF(<type>, capacity);

   creates a new type:
        struct {
            int card;
            <type> item[capacity];
        } bag_of_<type>;

   In general, bag b contains b.card items, which are accessed by
        b.item[0] ... b.item[b.card-1]

   For example

        typedef BAG_OF(int, 100);

   defines a new type bag_of_int of at most 100 ints.

        bag_of_int b;   

   declares b to be a bag of ints.  The code:

        MAKE_BAG_EMPTY(b);
        ADD_TO_BAG(b, 1);
        ADD_TO_BAG(b, 2);
        ADD_TO_BAG(b, 1);

   results in bag b containing 1, 1, 2 



*/
#define BAG_OF(type, capacity) \
    struct { \
        int card; \
        type item[capacity]; \
    } bag_of_ ## type


/* macro to add an object to a bag

   ADD_TO_BAG(bag, object);

   Pre: none 
   Post: none
   Side Effects: 
        if bag.card < bag capacity then
            object added to bag, cardinality increased by 1
        else
            assertion failure
*/
#define ADD_TO_BAG(bag, object) \
    assert( (bag).card < sizeof((bag).item)/sizeof((bag).item[0]) ); \
    (bag).item[(bag).card] = (object); (bag).card++;

/* macro to empty a bag 

   MAKE_BAG_EMPTY(bag);

   Pre: none
   Post: none
   Side Effects: sets bag cardinality to 0
*/
#define MAKE_BAG_EMPTY(bag) (bag).card = 0;


/* this may need to be increased depending on the particular problem */
#define MAX_BAG_SIZE 5

/* a state index is just an int */
typedef int state_index_t;

/* bag_of_state_index_t - a bag of system state indexes */
typedef BAG_OF(state_index_t, MAX_BAG_SIZE);

/* bag_of_bag_of_state_index_t - a bag of scheduling alternatives, 
   each alternative being a bag of possible next states 
*/
typedef BAG_OF(bag_of_state_index_t, MAX_BAG_SIZE);
