home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / src / Map.h < prev    next >
C/C++ Source or Header  |  1995-01-03  |  4KB  |  163 lines

  1. // Map.h                                                 -*- C++ -*-
  2. /*
  3.  
  4. Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. */
  23.  
  24. /*
  25.  
  26. The classes in this file are derived from the old `genclass' versions
  27. of Map and CHMap from libg++, originally:
  28.  
  29.   Copyright (C) 1988 Free Software Foundation
  30.     written by Doug Lea (dl@rocky.oswego.edu)
  31.  
  32. and distributed under the terms of the GNU Library General Public
  33. License as published by the Free Software Foundation.
  34.  
  35. */
  36.  
  37. #if ! defined (octave_Map_h)
  38. #define octave_Map_h 1
  39.  
  40. #include <Pix.h>
  41.  
  42. #include "utils.h"
  43.  
  44. template <class C>
  45. class Map
  46. {
  47. protected:
  48.   int count;
  49.   C def;
  50.  
  51. public:
  52.   Map (const C& dflt) : def (dflt) { count = 0; }
  53.  
  54.   virtual ~Map (void) { }
  55.  
  56.   int length (void) const { return count; }    // current number of items
  57.   int empty (void) const { return count == 0; }
  58.  
  59.   virtual int contains (const char *key) const;    // is key mapped?
  60.  
  61.   virtual void clear (void);            // delete all items
  62.           
  63.   virtual C& operator [] (const char *key) = 0;    // access contents by key
  64.           
  65.   virtual void del (const char *key) = 0;    // delete entry
  66.           
  67.   virtual Pix first (void) const = 0;        // Pix of first item or 0
  68.   virtual void next (Pix& i) const = 0;        // advance to next or 0
  69.   virtual const char *key (Pix i) const = 0;    // access key at i
  70.   virtual C& contents (Pix i) const = 0;    // access contents at i
  71.  
  72.   virtual int owns (Pix i) const;        // is i a valid Pix  ?
  73.   virtual Pix seek (const char *key) const;    // Pix of key
  74.  
  75.   C& dflt (void) { return def; }        // access default val
  76.  
  77.   void  error (const char* msg) const;
  78.  
  79.   virtual int OK (void) const = 0;        // rep invariant
  80. };
  81.  
  82. template <class C>
  83. struct CHNode
  84. {
  85.   CHNode *tl;
  86.   char *hd;
  87.   C cont;
  88.  
  89.   CHNode (void) : tl (0), hd (0) { }
  90.  
  91.   CHNode (const char *h, const C& c, CHNode *t = 0)
  92.     : tl (t), cont (c)
  93.       { hd = strsave (h); }
  94.  
  95.   ~CHNode (void)
  96.     { delete [] hd; }
  97. };
  98.  
  99. #ifndef DEFAULT_INITIAL_CAPACITY
  100. #define DEFAULT_INITIAL_CAPACITY 8
  101. #endif
  102.  
  103. template <class C>
  104. class CHMap : public Map<C>
  105. {
  106. protected:
  107.   CHNode<C> **tab;
  108.   unsigned int size;
  109.  
  110. public:
  111.   CHMap (const C& dflt, unsigned int sz = DEFAULT_INITIAL_CAPACITY);
  112.  
  113.   CHMap (const CHMap& a);
  114.  
  115.   ~CHMap (void)
  116.     {
  117.       clear ();
  118.       delete tab;
  119.     }
  120.  
  121.   C& operator [] (const char *key);
  122.  
  123.   void del (const char *key);
  124.  
  125.   Pix first (void) const;
  126.   void next (Pix& i) const;
  127.  
  128.   const char *key (Pix p) const
  129.     {
  130.       if (p == 0)
  131.     error ("null Pix");
  132.  
  133.       return ((CHNode<C> *) p)->hd;
  134.     }
  135.  
  136.   C& contents (Pix p) const
  137.     {
  138.       if (p == 0)
  139.     error ("null Pix");
  140.  
  141.      return ((CHNode<C> *) p)->cont;
  142.    }
  143.  
  144.   Pix seek (const char *key) const;
  145.  
  146.   int contains (const char *key) const
  147.     {
  148.       return seek (key) != 0;
  149.     }
  150.  
  151.   void clear (void);
  152.   int  OK (void) const;
  153. };
  154.  
  155. #endif
  156.  
  157. /*
  158. ;;; Local Variables: ***
  159. ;;; mode: C++ ***
  160. ;;; page-delimiter: "^/\\*" ***
  161. ;;; End: ***
  162. */
  163.