746
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
746
|
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. |
746
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_oct_map_h) |
|
24 #define octave_oct_map_h 1 |
|
25 |
4192
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
27 #pragma interface |
|
28 #endif |
|
29 |
4219
|
30 #include <map> |
746
|
31 |
4513
|
32 #include "Cell.h" |
3931
|
33 #include "oct-obj.h" |
746
|
34 |
1755
|
35 class string_vector; |
|
36 |
746
|
37 class |
3931
|
38 Octave_map |
746
|
39 { |
|
40 public: |
4219
|
41 |
4513
|
42 typedef std::map<std::string, Cell>::iterator iterator; |
|
43 typedef std::map<std::string, Cell>::const_iterator const_iterator; |
4219
|
44 |
4561
|
45 Octave_map (void) : map (), dimensions (0, 0) { } |
746
|
46 |
3523
|
47 Octave_map (const std::string& key, const octave_value& value) |
4561
|
48 : map (), dimensions (1, 1) |
|
49 { map[key] = Cell (value); } |
4513
|
50 |
|
51 Octave_map (const std::string& key, const Cell& vals) |
4561
|
52 : map (), dimensions (vals.dims ()) |
|
53 { map[key] = vals; } |
746
|
54 |
4435
|
55 Octave_map (const std::string& key, const octave_value_list& val_list) |
4561
|
56 : map (), dimensions (1, val_list.length ()) |
|
57 { map[key] = val_list; } |
4435
|
58 |
4561
|
59 Octave_map (const Octave_map& m) : map (m.map), dimensions (m.dimensions) { } |
3931
|
60 |
|
61 Octave_map& operator = (const Octave_map& m) |
|
62 { |
|
63 if (this != &m) |
3932
|
64 { |
|
65 map = m.map; |
4561
|
66 dimensions = m.dimensions; |
3932
|
67 } |
4561
|
68 |
3931
|
69 return *this; |
|
70 } |
746
|
71 |
|
72 ~Octave_map (void) { } |
1279
|
73 |
3932
|
74 // This is the number of keys. |
4219
|
75 int length (void) const { return map.size (); } |
3931
|
76 |
|
77 int empty (void) const { return map.empty (); } |
|
78 |
4513
|
79 Cell& operator [] (const std::string& key) { return map[key]; } |
3931
|
80 |
4513
|
81 Cell operator [] (const std::string& key) const; |
4197
|
82 |
4219
|
83 void del (const std::string& key) |
|
84 { |
|
85 iterator p = map.find (key); |
|
86 if (p != map.end ()) |
|
87 map.erase (p); |
|
88 } |
3931
|
89 |
4219
|
90 iterator begin (void) { return iterator (map.begin ()); } |
|
91 const_iterator begin (void) const { return const_iterator (map.begin ()); } |
|
92 |
|
93 iterator end (void) { return iterator (map.end ()); } |
|
94 const_iterator end (void) const { return const_iterator (map.end ()); } |
3931
|
95 |
4219
|
96 std::string key (const_iterator p) const { return p->first; } |
3931
|
97 |
4513
|
98 Cell& contents (const_iterator p) |
4219
|
99 { return operator [] (key(p)); } |
3931
|
100 |
4513
|
101 Cell contents (const_iterator p) const |
4219
|
102 { return operator [] (key(p)); } |
3931
|
103 |
4219
|
104 const_iterator seek (const std::string& key) const { return map.find (key); } |
|
105 |
|
106 int contains (const std::string& key) const |
|
107 { return (seek (key) != map.end ()); } |
3931
|
108 |
|
109 void clear (void) { map.clear (); } |
|
110 |
3933
|
111 string_vector keys (void) const; |
3931
|
112 |
4561
|
113 int rows (void) const { return dimensions(0); } |
|
114 |
|
115 int columns (void) const { return dimensions(1); } |
4200
|
116 |
4561
|
117 dim_vector dims (void) const { return dimensions; } |
4200
|
118 |
4561
|
119 int numel (void) const; |
3932
|
120 |
4513
|
121 Octave_map& assign (const octave_value_list& idx, const Octave_map& rhs); |
4197
|
122 |
4513
|
123 Octave_map& assign (const octave_value_list& idx, const std::string& key, |
|
124 const Cell& rhs); |
3932
|
125 |
4513
|
126 Octave_map& assign (const std::string& key, const Cell& rhs); |
3933
|
127 |
4513
|
128 Octave_map index (const octave_value_list& idx); |
3933
|
129 |
3931
|
130 private: |
|
131 |
|
132 // The map of names to values. |
4513
|
133 std::map<std::string, Cell> map; |
3932
|
134 |
4561
|
135 // The current size. |
|
136 mutable dim_vector dimensions; |
746
|
137 }; |
|
138 |
|
139 #endif |
|
140 |
|
141 /* |
|
142 ;;; Local Variables: *** |
|
143 ;;; mode: C++ *** |
|
144 ;;; End: *** |
|
145 */ |