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