/*------------------------------------------------------------------------------
 src/lib/dj/djMap_.h
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

namespace dj {

template <class Key, class Value>
void Map<Key, Value>::set(const Key & in, const Value & value)
{
  for (ListIt<Pair<Key, Value> > it = _list; it.isValid(); ++it)
    if (in == it().a) {
      it().b = value;
      return;
    }
  
  _list += Pair<Key, Value>(in, value);
}

template <class Key, class Value>
Value & Map<Key, Value>::operator [] (const Key & in)
{
  for (ListIt<Pair<Key, Value> > it = _list; it.isValid(); ++it)
    if (in == it().a) return it().b;

  _list += Pair<Key, Value>(in, Value());
  return _list[_list.size() - 1].b;
}

template <class Key, class Value>
const Value & Map<Key, Value>::get(const Key & in) const
{
  ConstListIt<Pair<Key, Value> > it = _list;
  for (; it.isValid(); ++it)
    if (in == it().a) break; 
  return it().b;
}

template <class Key, class Value>
const Value & Map<Key, Value>::operator [] (const Key & in) const
{
  return get(in);
}

template <class Key, class Value>
const List<Key> Map<Key, Value>::keyList() const
{
  List<Key> out;
  for (ConstListIt<Pair<Key, Value> > it = _list; it.isValid(); ++it)
    out += it().a;
  return out;
}

template <class Key, class Value>
const List<Value> Map<Key, Value>::valueList() const
{
  List<Value> out;
  for (ConstListIt<Pair<Key, Value> > it = _list; it.isValid(); ++it)
    out += it().b;
  return out;
}

template <class Key, class Value>
bool Map<Key, Value>::find(const Key & in) const
{
  for (ConstListIt<Pair<Key, Value> > it = _list; it.isValid(); ++it)
    if (in == it().a) return true;
  return false;
}

template <class Key, class Value>
const Value Map<Key, Value>::del(const Key & in)
{
  for (ListIt<Pair<Key, Value> > it = _list; it.isValid(); ++it)
    if (in == it().a)
      return _list.del(it.count()).b;
  return Value();
}

template <class Key, class Value>
void Map<Key, Value>::del()
{
  _list.del();
}

}
