home *** CD-ROM | disk | FTP | other *** search
/ Quark 3 / Quark3.iso / KATALOG / ARCHIV / TOOL / T001.ZIP / SOURCE.ZIP / ndictionary.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-27  |  3.8 KB  |  110 lines

  1. /*
  2. Copyright (C) Matthew 'pagan' Baranowski & Sander 'FireStorm' van Rossen
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17. */
  18.  
  19. #ifndef _NPDICTIONARY_H_
  20. #define _NPDICTIONARY_H_
  21.  
  22. #include "nsequence.h"
  23.  
  24.  typedef class NodeLocatorInfo            * NodeLocator;
  25.  typedef class NodeDictionaryInfo         * NodeDictionary;
  26.  typedef class KeyComparatorInfo          * KeyComparator;
  27.  typedef class StrKeyComparatorInfo       * StrKeyComparator;
  28.  typedef class IntKeyComparatorInfo       * IntKeyComparator;
  29.   
  30.   /********************************************************************
  31.   * CLASS NAME: KeyComparator
  32.   *  abstract class defining the interface for KeyComparators
  33.   ********************************************************************/
  34.  
  35. #ifndef BOOL
  36. #define BOOL int
  37. #endif
  38.  
  39. class KeyComparatorInfo
  40. {
  41.   public:
  42.   virtual BOOL equal( Object key1, Object key2 )=0;
  43.    virtual char *printKey( Object key )=0;
  44. };
  45.  
  46. /* integer comparator */
  47. class IntKeyComparatorInfo: public KeyComparatorInfo
  48. {
  49.   public:
  50.    virtual BOOL equal( Object key1, Object key2 ) { return ((int)key1 == (int)key2); }
  51.    virtual char *printKey( Object key ) { return NULL; }
  52. };
  53.  
  54. /* string comparator */
  55. class StrKeyComparatorInfo: public KeyComparatorInfo
  56. {
  57.  public:
  58.   virtual BOOL equal( Object key1, Object key2 );
  59.   virtual char *printKey( Object key ) { return (char*)key; }
  60. };
  61.  
  62.  
  63.  /********************************************************************
  64.   * CLASS NAME: NodeLocator
  65.   *  extends NodePosition class to include a key for indexing
  66.   ********************************************************************/
  67.  
  68.  class NodeLocatorInfo: public NodePositionInfo {
  69.      private:
  70.          Object m_key;
  71.          
  72.      public:
  73.          NodeLocatorInfo( NodeSequence container, Object element, Object key ): 
  74.             NodePositionInfo( container, element) 
  75.          { m_key = key; };        
  76.  
  77.          void setKey( Object key ) { m_key = key; }
  78.          Object key() { return m_key; }
  79.  };
  80.          
  81.  /******************************************************************** 
  82.   * CLASS NAME: NodeDictionary
  83.   *  stores a series of locators that are acessed using their keys, 
  84.   *  a key comparator class defines how keys are used
  85.   ********************************************************************/
  86.  
  87. class NodeDictionaryInfo: public NodeSequenceInfo {
  88. private:
  89.     KeyComparator m_keyComp;
  90. public:
  91.     NodeDictionaryInfo( KeyComparator kC ): NodeSequenceInfo() { m_keyComp = kC; };
  92.     
  93.     /* appends the key, element pair to the end of the list */
  94.     void        insert( Object element, Object key );
  95.     /* if the key already exists, replaces the returned element, otherwise inserts new pair */
  96.     Object      insertReplace( Object element, Object key );
  97.     /* returns an object mapped to the key */
  98.     Object      find( Object key );
  99.     /* removes the key, element pair and returns the element */
  100.     Object      remove( Object key );
  101.     /* replaces the element mapped to the key */
  102.     Object      replace( Object element, Object key );   
  103.     /* debugging dump */
  104.     void        dumpDictionary();
  105. };
  106.  
  107.  
  108. #endif
  109.  
  110.