home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / ASSOC.H < prev    next >
C/C++ Source or Header  |  1996-01-05  |  4KB  |  138 lines

  1. /****************************************************************************
  2.     $Id: assoc.h 501.0 1995/03/07 12:26:40 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:    Ron van der Wal
  8.  
  9.     Declarations of classes TLAssoc<K,V> and TLDictionary<K,V>.
  10.  
  11.     $Log: assoc.h $
  12.     Revision 501.0  1995/03/07 12:26:40  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.7  1995/01/31 16:29:14  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.6  1994/10/12  10:06:57  ron
  18.     Moved template source code to a separate file
  19.     Changed base for TLDictIter<K,V>
  20.  
  21.     Revision 1.5  1994/10/11  19:06:33  ron
  22.     Added dictionary iterator
  23.     Made dictionary keys and values private
  24.  
  25.     Revision 1.4  1994/09/28  14:25:05  ron
  26.     Removed Macintosh-style #include references
  27.  
  28.     Revision 1.3  1994/09/27  20:24:49  ron
  29.     Changed path separator from / to \
  30.  
  31.     Revision 1.2  1994/09/26  15:14:30  ron
  32.     Changed include file references
  33.  
  34.     Revision 1.1  1994/08/16  18:06:44  ron
  35.     Initial revision
  36.  
  37. ****************************************************************************/
  38.  
  39. #ifndef _TLX_ASSOC_H
  40. #define _TLX_ASSOC_H
  41.  
  42. #ifndef _TLX_ARRAYS_H
  43. #include <tlx\501\arrays.h>
  44. #endif
  45. #ifndef _TLX_ITER_H
  46. #include <tlx\501\iter.h>
  47. #endif
  48.  
  49. /*---------------------------------------------------------------------------
  50.     TLAssoc<K,V> -
  51.  
  52.     Association between a key type K and a value type V.
  53. ---------------------------------------------------------------------------*/
  54.  
  55. template<class K, class V> class TLAssoc
  56. {
  57.     K             mKey;
  58.     V             mValue;
  59.  
  60. public:
  61.     TLAssoc() {}
  62.     TLAssoc(const K &k, const V &v): mKey(k), mValue(v) {}
  63.  
  64.     K &            Key() { return mKey; }
  65.     const K &        Key() const { return mKey; }
  66.     V &            Value() { return mValue; }
  67.     const V &        Value() const { return mValue; }
  68.  
  69.     friend int         operator ==(const TLAssoc<K,V> &, const TLAssoc<K,V> &);
  70.     friend int         operator ==(const TLAssoc<K,V> &, const K &);
  71.     friend int         operator ==(const K &, const TLAssoc<K,V> &);
  72.  
  73.     friend ostream &    operator <<(ostream &, const TLAssoc<K,V> &);
  74. };
  75.  
  76. /*---------------------------------------------------------------------------
  77.     TLDictionary<K,V> -
  78.  
  79.     TLDictionary with keys of type K and values of type V.
  80. ---------------------------------------------------------------------------*/
  81.  
  82. template<class K, class V> class TLDictIter;
  83.  
  84. template<class K, class V> class TLDictionary
  85. {
  86.     friend class TLDictIter<K,V>;
  87.  
  88.     TLSeq<K>         mKeys;
  89.     TLSeq<V>         mValues;
  90.  
  91. public:
  92.     TLDictionary(size_t size, size_t delta);
  93.  
  94.     size_t        Count() const;
  95.     bool         Contains(const K &) const;
  96.     void         Insert(const K &, const V &);
  97.     void        RemoveAll();
  98.     V &            operator [](const K &);
  99.     const V &        operator [](const K &) const;
  100.  
  101.     friend ostream &    operator <<(ostream &, const TLDictionary<K,V> &);
  102. };
  103.  
  104. /*---------------------------------------------------------------------------
  105.     TLDictionary<K,V> -
  106.  
  107.     TLDictionary with keys of type K and values of type V.
  108. ---------------------------------------------------------------------------*/
  109.  
  110. template<class K, class V> class TLDictIter: public TLValueIter<V>
  111. {
  112.     TLDictionary<K,V> &    mDict;
  113.     index_t        mPos;
  114.  
  115. public:
  116.     TLDictIter(TLDictionary<K,V> &);
  117.  
  118.     // A dictionary iterator allows access to both the key and the value
  119.     // at the current iterator position; the inherited Peek() function
  120.     // corresponds to the value.
  121.  
  122.     K &            PeekKey() const;
  123.     virtual V &        Peek() const;
  124.  
  125.     // Overridden iterator functions
  126.  
  127.     virtual size_t    Count() const;
  128.     virtual bool    Contains(V &);
  129.  
  130. private:
  131.     // Overridden implementation functions
  132.  
  133.     virtual bool    FirstPos();
  134.     virtual bool    NextPos();
  135. };
  136.  
  137. #endif    // _TLX_ASSOC_H
  138.