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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
746
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_oct_map_h) |
|
25 #define octave_oct_map_h 1 |
|
26 |
6059
|
27 #include <algorithm> |
4219
|
28 #include <map> |
746
|
29 |
4513
|
30 #include "Cell.h" |
3931
|
31 #include "oct-obj.h" |
746
|
32 |
1755
|
33 class string_vector; |
|
34 |
746
|
35 class |
6109
|
36 OCTINTERP_API |
3931
|
37 Octave_map |
746
|
38 { |
|
39 public: |
4219
|
40 |
4513
|
41 typedef std::map<std::string, Cell>::iterator iterator; |
|
42 typedef std::map<std::string, Cell>::const_iterator const_iterator; |
4219
|
43 |
5880
|
44 typedef std::list<std::string>::iterator key_list_iterator; |
|
45 typedef std::list<std::string>::const_iterator const_key_list_iterator; |
|
46 |
4744
|
47 // Warning! You should always use at least two dimensions. |
|
48 |
5880
|
49 Octave_map (const dim_vector& dv = dim_vector (0, 0), |
|
50 const string_vector& key_list_arg = string_vector ()); |
746
|
51 |
4587
|
52 Octave_map (const std::string& k, const octave_value& value) |
5880
|
53 : map (), key_list (), dimensions (1, 1) |
|
54 { |
|
55 map[k] = value; |
|
56 key_list.push_back (k); |
|
57 } |
4513
|
58 |
4587
|
59 Octave_map (const std::string& k, const Cell& vals) |
5880
|
60 : map (), key_list (), dimensions (vals.dims ()) |
|
61 { |
|
62 map[k] = vals; |
|
63 key_list.push_back (k); |
|
64 } |
746
|
65 |
4587
|
66 Octave_map (const std::string& k, const octave_value_list& val_list) |
5880
|
67 : map (), key_list (), dimensions (1, val_list.length ()) |
|
68 { |
|
69 map[k] = val_list; |
|
70 key_list.push_back (k); |
|
71 } |
4435
|
72 |
5880
|
73 Octave_map (const Octave_map& m) |
|
74 : map (m.map), key_list (m.key_list), dimensions (m.dimensions) { } |
3931
|
75 |
|
76 Octave_map& operator = (const Octave_map& m) |
|
77 { |
|
78 if (this != &m) |
3932
|
79 { |
|
80 map = m.map; |
5880
|
81 key_list = m.key_list; |
4561
|
82 dimensions = m.dimensions; |
3932
|
83 } |
4561
|
84 |
3931
|
85 return *this; |
|
86 } |
746
|
87 |
|
88 ~Octave_map (void) { } |
1279
|
89 |
3932
|
90 // This is the number of keys. |
5275
|
91 octave_idx_type length (void) const { return map.size (); } |
3931
|
92 |
|
93 int empty (void) const { return map.empty (); } |
|
94 |
4587
|
95 void del (const std::string& k) |
4219
|
96 { |
4587
|
97 iterator p = map.find (k); |
5881
|
98 |
4219
|
99 if (p != map.end ()) |
5881
|
100 { |
|
101 map.erase (p); |
5880
|
102 |
5882
|
103 key_list_iterator q |
|
104 = std::find (key_list.begin (), key_list.end (), k); |
5881
|
105 |
|
106 assert (q != key_list.end ()); |
|
107 |
|
108 key_list.erase (q); |
|
109 } |
4219
|
110 } |
3931
|
111 |
4219
|
112 iterator begin (void) { return iterator (map.begin ()); } |
|
113 const_iterator begin (void) const { return const_iterator (map.begin ()); } |
|
114 |
|
115 iterator end (void) { return iterator (map.end ()); } |
|
116 const_iterator end (void) const { return const_iterator (map.end ()); } |
3931
|
117 |
4219
|
118 std::string key (const_iterator p) const { return p->first; } |
3931
|
119 |
5328
|
120 Cell& contents (const std::string& k); |
4675
|
121 Cell contents (const std::string& k) const; |
3931
|
122 |
5328
|
123 Cell& contents (const_iterator p) |
|
124 { return contents (key(p)); } |
|
125 |
4513
|
126 Cell contents (const_iterator p) const |
4675
|
127 { return contents (key(p)); } |
3931
|
128 |
5156
|
129 int intfield (const std::string& k, int def_val = 0) const; |
|
130 |
|
131 std::string stringfield (const std::string& k, |
|
132 const std::string& def_val = std::string ()) const; |
|
133 |
5328
|
134 iterator seek (const std::string& k) { return map.find (k); } |
4587
|
135 const_iterator seek (const std::string& k) const { return map.find (k); } |
4219
|
136 |
4817
|
137 bool contains (const std::string& k) const |
4587
|
138 { return (seek (k) != map.end ()); } |
3931
|
139 |
5925
|
140 void clear (void) |
|
141 { |
|
142 map.clear (); |
|
143 key_list.clear (); |
|
144 } |
3931
|
145 |
3933
|
146 string_vector keys (void) const; |
3931
|
147 |
5275
|
148 octave_idx_type rows (void) const { return dimensions(0); } |
4561
|
149 |
5275
|
150 octave_idx_type columns (void) const { return dimensions(1); } |
4200
|
151 |
4561
|
152 dim_vector dims (void) const { return dimensions; } |
4200
|
153 |
5435
|
154 int ndims (void) const { return dimensions.length (); } |
|
155 |
5571
|
156 Octave_map transpose (void) const; |
|
157 |
4567
|
158 Octave_map reshape (const dim_vector& new_dims) const; |
|
159 |
5781
|
160 void resize (const dim_vector& dv, bool fill = false); |
4936
|
161 |
5275
|
162 octave_idx_type numel (void) const; |
3932
|
163 |
5275
|
164 Octave_map concat (const Octave_map& rb, const Array<octave_idx_type>& ra_idx); |
4806
|
165 |
5592
|
166 Octave_map& maybe_delete_elements (const octave_value_list& idx); |
|
167 |
4513
|
168 Octave_map& assign (const octave_value_list& idx, const Octave_map& rhs); |
4197
|
169 |
4587
|
170 Octave_map& assign (const octave_value_list& idx, const std::string& k, |
4513
|
171 const Cell& rhs); |
3932
|
172 |
4675
|
173 Octave_map& assign (const std::string& k, const octave_value& rhs); |
|
174 |
4587
|
175 Octave_map& assign (const std::string& k, const Cell& rhs); |
3933
|
176 |
4513
|
177 Octave_map index (const octave_value_list& idx); |
3933
|
178 |
3931
|
179 private: |
|
180 |
|
181 // The map of names to values. |
4513
|
182 std::map<std::string, Cell> map; |
3932
|
183 |
5880
|
184 // An extra list of keys, so we can keep track of the order the keys |
|
185 // are added for compatibility with you know what. |
|
186 std::list<std::string> key_list; |
|
187 |
4561
|
188 // The current size. |
|
189 mutable dim_vector dimensions; |
5880
|
190 |
|
191 void maybe_add_to_key_list (const std::string& k) |
5925
|
192 { |
|
193 if (! contains (k)) |
|
194 key_list.push_back (k); |
|
195 } |
746
|
196 }; |
|
197 |
|
198 #endif |
|
199 |
|
200 /* |
|
201 ;;; Local Variables: *** |
|
202 ;;; mode: C++ *** |
|
203 ;;; End: *** |
|
204 */ |