ClassS04CS141/AssocList.Cc

Top | ClassS04CS141 | recent changes | Preferences

//AssocList.cc

#ifndef _ASSOCLIST_CC
#define _ASSOCLIST_CC

#include "AssocList.h"
#include "Comparisons.h"

#include <iostream.h>

// destructor
template <class KEY, class VALUE>
AssocList<KEY,VALUE>::~AssocList() {
}

// constructors

template <class KEY, class VALUE>
AssocList<KEY,VALUE>::AssocList() :
  use_default(0)
{ 
}

template <class KEY, class VALUE>
AssocList<KEY,VALUE>::AssocList(VALUE const & def) :
  use_default(1), default_init(def) 
{
}

template <class KEY, class VALUE>
AssocList<KEY,VALUE>::AssocList(AssocList const & a) :
  pairs(a.pairs),
  use_default (a.use_default),
  default_init (a.default_init)
{ 
}

// operator=

template <class KEY, class VALUE>
AssocList<KEY,VALUE>& AssocList<KEY,VALUE>::operator=(const AssocList& a) {
  pairs = a.pairs;		// deep copy
  use_default = a.use_default;
  default_init = a.default_init;
  return *this;
}


// find

template <class KEY, class VALUE>
int AssocList<KEY,VALUE>::find(KEY const & k) const {
  for (int i = 0;  i < pairs.size();  ++i)
    if (! pairs[i].removed && Equal(pairs[i].k, k))	return i;
  return -1;
}

// clear

template <class KEY, class VALUE>
void AssocList<KEY,VALUE>::clear() { 
  pairs.clear(); 
}

// find

template <class KEY, class VALUE>
int AssocList<KEY,VALUE>::exists(KEY const & k) const { 
  return find(k) != -1; 
}

// remove

template <class KEY, class VALUE>
void AssocList<KEY,VALUE>::remove(KEY const & k) {
  int i = find(k);
  if (i == -1) return;
  pairs[i].removed = 1;
}

// operator[] and const operator[]

template <class KEY, class VALUE>
VALUE & 
AssocList<KEY,VALUE>::operator[](KEY const & k) {

  int i = find(k);
  if (i >= 0) return pairs[i].v;

  // find next free pair pointer
  for (i = 0;  i < pairs.size() && ! pairs[i].removed;  ++i)
    ;
    
  // insert pair for key k and return v

  pairs[i].k = k;
  if (use_default) pairs[i].v = default_init;
  pairs[i].removed = 0;

  return pairs[i].v;
}

template <class KEY, class VALUE>
VALUE AssocList<KEY,VALUE>::operator[](KEY const & k) const {
  int i = find(k);
  if (i >= 0) return pairs[i].v;

  cerr << "AssocList index out of range\n";
  exit(1);
}

// keys() and values()

template <class KEY, class VALUE>
Array<KEY> AssocList<KEY,VALUE>::keys() const {
  Array<KEY> a;
  for (int i = 0;  i < pairs.size();  ++i)
    if (! pairs[i].removed) a[a.size()] = pairs[i].k;
  return a;
}

template <class KEY, class VALUE>
Array<VALUE> AssocList<KEY,VALUE>::values() const {
  Array<VALUE> a;
  for (int i = 0;  i < pairs.size();  ++i)
    if (! pairs[i].removed) a[a.size()] = pairs[i].v;
  return a;
}

// operator<<

template <class KEY, class VALUE>
ostream & 
operator<<(ostream & out, const AssocList<KEY,VALUE> & a) {

  //  out << "use_default = " << a.use_default;
  //  if (a.use_default) {
  //     out << ", default = " << a.default_init;
  //  }
  // out << "\npairs = [";

  out << "[";
  for (int i = 0;  i < a.pairs.size();  ++i) {
    if (i != 0) out << ", ";
    out << "(" << a.pairs[i].k << ", " << a.pairs[i].v << ", " << a.pairs[i].removed << ")";
  }
  out << "]";

  return out;
}

#endif

Top | ClassS04CS141 | recent changes | Preferences
This page is read-only | View other revisions
Last edited April 5, 2004 2:57 pm by Neal (diff)
Search: