//----------------------------------------------------------------------
//  IMPLEMENTATION FILE (cset.cpp)
//  This module exports a character set ADT.
//  Set representation: a Boolean vector.
//----------------------------------------------------------------------
#include "cset.h"

// Private members of class:
//    Boolean inSet[128];     One array element per ASCII char
//
// CLASSINV:
//    (inSet[n] == TRUE) --> char with int value n is in the set

CharSet::CharSet()
    //..............................................................
    // Constructor
    // POST: inSet[0..127] == FALSE
    //..............................................................
{
    int i;
    for (i = 0; i < 128; i++)
        inSet[i] = FALSE;
}

Boolean CharSet::IsFull() const
    //..............................................................
    // POST: FCTVAL == FALSE
    //       (With the 128-element vector preallocated, it
    //        cannot overflow)
    //..............................................................
{
    return FALSE;
}

Boolean CharSet::IsEmpty() const
    //..............................................................
    // POST: FCTVAL == TRUE, if (NOT inSet[i]) for all 0<=i<=127
    //              == FALSE, otherwise
    //..............................................................
{
    int i = 0;
    while (i < 128) {  // INV (prior to test):
                       //     NOT inSet[j], for all 0<=j<=i-1
                       //  && i <= 128
        if (inSet[i])
            return FALSE;
        i++;
    }
    return TRUE;
}

void CharSet::Insert( /* in */ char ch )
    //..............................................................
    // PRE:  Assigned(ch)
    // POST: inSet[ch] == TRUE
    //..............................................................
{
    inSet[int(ch)] = TRUE;
}

void CharSet::Delete( /* in */ char ch )
    //..............................................................
    // PRE:  Assigned(ch)
    // POST: inSet[ch] == FALSE
    //..............................................................
{
    inSet[int(ch)] = FALSE;
}

Boolean CharSet::IsElt( /* in */ char ch ) const
    //..............................................................
    // PRE:  Assigned(ch)
    // POST: FCTVAL == inSet[ch]
    //..............................................................
{
    return inSet[int(ch)];
}

CharSet CharSet::Intersect( /* in */ CharSet set2 ) const
    //..................................................................
    // POST: For all i, (0 <= i <= 127),
    //       FCTVAL.inSet[i] == (inSet[i] && set2.inSet[i])
    //..................................................................
{
    CharSet result;
    int     i;

    for (i = 0; i < 128; i++)
        result.inSet[i] =(inSet[i] && set2.inSet[i]);
    return result;
}

CharSet CharSet::Union( /* in */ CharSet set2 ) const
    //..................................................................
    // POST: For all i, (0 <= i <= 127),
    //       FCTVAL.inSet[i] == (inSet[i] || set2.inSet[i])
    //..................................................................
{
    CharSet result;
    int     i;

    for (i = 0; i < 128; i++)
        result.inSet[i] = (inSet[i] || set2.inSet[i]);
    return result;
}

