// Martian Adam Smyth
// CS510 Assignment 1
// Element Class code file

#include "element.h"
#include <string.h>

Element::~Element(void)
{
  if(key)
    delete key;
}

int Element::setkey(const char *str)
{
  if(key)
    delete(key);
  return (int) (key=strdup(str));
}

// For comparison purposes, an element that doesn't have a key is less than one that does.
// If neither does, it doesn't matter.

// Compare to another Element

int Element::operator<(const Element & other)
{
  if(key && other.key)
    return (strcmp(key, other.key)) < 0;
  else if (key)
    return 0;
  else
    return 1;
}

int Element::operator>(const Element & other)
{
  if(key && other.key)
    return (strcmp(key, other.key)) > 0;
  else if (key)
    return 1;
  else
    return 0;
}

int Element::operator==(const Element & other)
{
  if(key && other.key)
    return !(strcmp(key, other.key));
  else
    return 0;
}

int Element::operator!=(const Element & other)
{
  if(key && other.key)
    return (strcmp(key, other.key));
  else
    return 1;
}

// Compare to string

int Element::operator<(const char *other)
{
  if(key && other)
    return (strcmp(key, other)) < 0;
  else if (key)
    return 0;
  else
    return 1;
}

int Element::operator>(const char *other)
{
  if(key && other)
    return (strcmp(key, other)) > 0;
  else if (key)
    return 1;
  else
    return 0;
}

int Element::operator==(const char *other)
{
  if(key && other)
    return !(strcmp(key, other));
  else
    return 0;
}

int Element::operator!=(const char *other)
{
  if(key && other)
    return (strcmp(key, other));
  else
    return 1;
}

