// $Source: /usr/local/SimSoft/SoftShop/Basics/Structur/Map.h,v $
// $Revision: 1.20 $
// $Author: baum $
// $Date: 2001/11/29 15:52:23 $
//
// Login name of the user who locked the revision
// $Locker:  $
// Tag name used to checkout the file
// $Name: Release-20 $
// State assigned to revision
// $State: Exp $
//
//
// $Log: Map.h,v $
// Revision 1.20  2001/11/29 15:52:23  baum
// -In Kalender.cpp und Kalender.h Handsteuerung eingebaut
//

#ifndef _Map_h
#define _Map_h 1

#if defined(_MSC_VER)
#pragma warning(disable:4251)
#endif  // _MSC_VER

#include "DynArray.h"

#if defined __GNUG__ || defined __hpux
#  pragma interface
#endif

//==================================================================
//		Klassen-Header
//		fuer class Map<class TKey, class TVal>
//==================================================================

//##ModelId=3FBCBCC500EA
template <class TKey, class TVal>
class Map {
  public:
    // constructors
	//##ModelId=3FBCBCC500FC
    Map (unsigned size = 10);

    // Copyconstructor
	//##ModelId=3FBCBCC500FE
    Map (const Map<TKey,TVal>&);

    // destructor
	//##ModelId=3FBCBCC5010A
    ~Map () { };

    // assignment operators
	//##ModelId=3FBCBCC5010B
    Map<TKey,TVal>& operator = (const Map<TKey,TVal>&);

    // inserts 2 associated Objs (Key and AssocObj)
	//##ModelId=3FBCBCC50128
    TVal& insert (const TKey&, const TVal&);

    // remove First or Last Object
	//##ModelId=3FBCBCC5013A
    TVal* removeFirst ();
	//##ModelId=3FBCBCC5013B
    TVal* removeLast ();

    // be careful, 'remove' may invalidate the Iterator
    // remove Key and associated Value
	//##ModelId=3FBCBCC50148
    TVal* remove (const TKey&);

    // remove Key and associated Value by Value
	//##ModelId=3FBCBCC5014A
    TKey* removeByVal (const TVal&);

    // remove all Objects
	//##ModelId=3FBCBCC50157
    void removeAll ();

    // find Value associated to Key
	//##ModelId=3FBCBCC50158
    TVal* find (const TKey&) const;

    // find Key associated to Value
	//##ModelId=3FBCBCC5015B
    TKey* findKey (const TVal&) const;

    // getting current Value under Cursor
	//##ModelId=3FBCBCC50169
    TVal* operator () () const;

    // increment & decrement operators
	//##ModelId=3FBCBCC5016B
    TKey* operator ++ () const;        // Praefix-Notation (++x)
	//##ModelId=3FBCBCC50177
    TKey* operator ++ (int) const;     // Postfix-Notation (x++)
	//##ModelId=3FBCBCC5017A
    TKey* operator -- () const;        // Praefix-Notation (--x)
	//##ModelId=3FBCBCC50186
    TKey* operator -- (int) const;     // Postfix-Notation (x--)

    // getting first Key and set Cursor
	//##ModelId=3FBCBCC50189
    TKey* First () const;

    // getting last Key and set Cursor
	//##ModelId=3FBCBCC5018B
    TKey* Last () const;

    // getting next Key and set Cursor
	//##ModelId=3FBCBCC50197
    TKey* Next () const;

    // getting previous Key and set Cursor
	//##ModelId=3FBCBCC50199
    TKey* Prev () const;

    // proof if there are some Objects in the Array
	//##ModelId=3FBCBCC501A5
    bool isEmpty () const;

    // returns the Count of Items
	//##ModelId=3FBCBCC501A7
    unsigned Count () const;

  private:
    // data members
	//##ModelId=3FBCBCC501B8
    DynArray<TKey> ObjKey;
	//##ModelId=3FBCBCC501C8
    DynArray<TVal> ObjVal;

	//##ModelId=3FBCBCC501D4
    TKey* searchKey (const TVal&) const;
	//##ModelId=3FBCBCC501E4
    TVal* searchVal (const TKey&) const;

};	// class Map

//==================================================================
//		Inline-Quellcode
//		fuer class Map<T>
//==================================================================

//##ModelId=3FBCBCC500FC
template <class TKey, class TVal>
inline Map<TKey,TVal>::Map (unsigned size /* = 10 */)
  : ObjKey (size), ObjVal (size)
  { }

//##ModelId=3FBCBCC500FE
template <class TKey, class TVal>
inline Map<TKey,TVal>::Map (const Map<TKey,TVal>& m)
  : ObjKey (m.ObjKey), ObjVal (m.ObjVal)
  { }

// assignment operators
//##ModelId=3FBCBCC5010B
template <class TKey, class TVal>
Map<TKey,TVal>& Map<TKey,TVal>::operator = (const Map<TKey,TVal>& m) {
  ObjKey = m.ObjKey;
  ObjVal = m.ObjVal;
  return *this;
}

//##ModelId=3FBCBCC50128
template <class TKey, class TVal>
inline TVal& Map<TKey,TVal>::insert (const TKey& key, const TVal& val) {
  ObjKey.append (key);
  return ObjVal.append (val);
}

//##ModelId=3FBCBCC5013A
template <class TKey, class TVal>
inline TVal* Map<TKey,TVal>::removeFirst () {
  ObjKey.removeFirst ();
  return ObjVal.removeFirst ();
}

//##ModelId=3FBCBCC5013B
template <class TKey, class TVal>
inline TVal* Map<TKey,TVal>::removeLast () {
  ObjKey.removeLast ();
  return ObjVal.removeLast ();
}

//##ModelId=3FBCBCC50148
template <class TKey, class TVal>
inline TVal* Map<TKey,TVal>::remove (const TKey& key) {
  register TVal* val = searchVal (key);
  if (val) {
    ObjKey.remove (key);
    return ObjVal.remove (*val);
  }
  return NULL;
}

//##ModelId=3FBCBCC5014A
template <class TKey, class TVal>
inline TKey* Map<TKey,TVal>::removeByVal (const TVal& val) {
  register TKey* key = searchKey (val);
  if (key) {
    ObjVal.remove (val);
    return ObjKey.remove (*key);
  }
  return NULL;
}

//##ModelId=3FBCBCC50157
template <class TKey, class TVal>
inline void Map<TKey,TVal>::removeAll () {
  ObjKey.removeAll ();
  ObjVal.removeAll ();
}

//##ModelId=3FBCBCC50158
template <class TKey, class TVal>
inline TVal* Map<TKey,TVal>::find (const TKey& key) const {
  return Map<TKey,TVal>::searchVal (key);
}

//##ModelId=3FBCBCC5015B
template <class TKey, class TVal>
inline TKey* Map<TKey,TVal>::findKey (const TVal& val) const {
  return Map<TKey,TVal>::searchKey (val);
}

//##ModelId=3FBCBCC50169
template <class TKey, class TVal>
inline TVal* Map<TKey,TVal>::operator () () const {
  return ObjVal ();
}

// increment & decrement operator
//##ModelId=3FBCBCC5016B
template <class TKey, class TVal>
inline TKey* Map<TKey,TVal>::operator ++ () const {
  ++ObjVal;
  return ++ObjKey;
}

//##ModelId=3FBCBCC50177
template <class TKey, class TVal>
inline TKey* Map<TKey,TVal>::operator ++ (int) const {
  ObjVal++;
  return ObjKey++;
}

//##ModelId=3FBCBCC5017A
template <class TKey, class TVal>
inline TKey* Map<TKey,TVal>::operator -- () const {
  --ObjVal;
  return --ObjKey;
}

//##ModelId=3FBCBCC50186
template <class TKey, class TVal>
inline TKey* Map<TKey,TVal>::operator -- (int) const {
  ObjVal--;
  return ObjKey--;
}

// getting first Object and set Cursor
//##ModelId=3FBCBCC50189
template <class TKey, class TVal>
inline TKey* Map<TKey,TVal>::First () const {
  ObjVal.First ();
  return ObjKey.First ();
}

// getting last Object and set Cursor
//##ModelId=3FBCBCC5018B
template <class TKey, class TVal>
inline TKey* Map<TKey,TVal>::Last () const {
  ObjVal.Last ();
  return ObjKey.Last ();
}

// getting next Object after current and set Cursor
//##ModelId=3FBCBCC50197
template <class TKey, class TVal>
inline TKey* Map<TKey,TVal>::Next () const {
  ObjVal.Next ();
  return ObjKey.Next ();
}

// getting previous Object before current and set Cursor
//##ModelId=3FBCBCC50199
template <class TKey, class TVal>
inline TKey* Map<TKey,TVal>::Prev () const {
  ObjVal.Prev ();
  return ObjKey.Prev ();
}

//##ModelId=3FBCBCC501A7
template <class TKey, class TVal>
inline unsigned Map<TKey,TVal>::Count () const {
  return ObjKey.Count ();
}

//##ModelId=3FBCBCC501A5
template <class TKey, class TVal>
inline bool Map<TKey,TVal>::isEmpty () const {
  return ObjKey.isEmpty ();
}

//##ModelId=3FBCBCC501D4
template <class TKey, class TVal>
TKey* Map<TKey,TVal>::searchKey (const TVal& val) const {
  for (ObjKey.First (), ObjVal.First (); ObjKey (); ObjKey++, ObjVal++)
    if (ObjVal () == &val)  return ObjKey ();
  return NULL;
}

//##ModelId=3FBCBCC501E4
template <class TKey, class TVal>
TVal* Map<TKey,TVal>::searchVal (const TKey& key) const {
  for (ObjKey.First (), ObjVal.First (); ObjKey (); ObjKey++, ObjVal++)
    if (ObjKey () == &key)  return ObjVal ();
  return NULL;
}

#endif	// !defined _Map_h

