// C++ code Copyright (C) David R. Evans G4AMJ/NQ0I

#include <set.h>

#ifdef macintosh
// constructor -- Mac
set::set(void)
{ _max_size = 256;
  _current_size = 0;
  heap_check(_data = new int [_max_size]);
}
#else
// constructor -- non-Mac
set::set(const int size)
{ _max_size = size;
  _current_size = 0;
  heap_check(_data = new int [_max_size]);
}
#endif

// copy constructor (needed because a deep copy is required)
set::set(const set& s)
{ _max_size = s._max_size;
  _current_size = s._current_size;
  heap_check(_data = new int [_max_size]);
  for (int n = 0; n < _current_size; n++)
    _data[n] = s._data[n];
}

// set = set
void set::operator=(const set& s)
{ clear();
  for (int n = 0; n < s.size(); n++)
    *this += s[n];
}

// int < set; is an integer a member of the set?
boolean operator<(int v, const set& rhs)
{ boolean found = false;
  for (int n = 0; n < rhs._current_size; n++)
    if (v == rhs._data[n]) then
      found = true;
  return found;
}

// set += int; put an integer in the set
void set::operator+=(const int new_element)
{ if (!(new_element < *this)) then
  { if (_current_size == _max_size) then
      fatal_error("Attempt to add member to a full set");
    _data[_current_size++] = new_element;
  }
}

// set += set; add one set to another
void set::operator+=(set& s)
{ for (int n = 0; n < s.size(); n++)
    *this += s[n];
}

// set -= int; remove an element from a set
void set::operator-=(const int old_element)
{ boolean found = false;
  for (int n = 0; (n < _current_size) && !found; n++)
  { if (old_element == _data[n]) then
    { for (int m = n; m < _current_size - 1; m++)
        _data[m] = _data[m + 1];
      _current_size--;
      found = true;
    }
  } 
}

// set < set; is one set a subset of another?
boolean set::operator<(const set& rhs) const
{ boolean subset = true;
  for (int n = 0; ((n < size()) && (subset = ((*this)[n] < rhs))); n++)
    { }
  return subset;
}
