746
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1994, 1995, 1996, 1997, 2000, 2002, 2003, 2004, 2005, |
|
4 2006, 2007 John W. Eaton |
746
|
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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
746
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
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), |
6959
|
50 const Cell& key_vals = Cell ()); |
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 |
7046
|
90 Octave_map squeeze (void) const; |
|
91 |
|
92 Octave_map permute (const Array<int>& vec, bool inv = false) const; |
|
93 |
3932
|
94 // This is the number of keys. |
6639
|
95 octave_idx_type nfields (void) const { return map.size (); } |
3931
|
96 |
4587
|
97 void del (const std::string& k) |
4219
|
98 { |
4587
|
99 iterator p = map.find (k); |
5881
|
100 |
4219
|
101 if (p != map.end ()) |
5881
|
102 { |
|
103 map.erase (p); |
5880
|
104 |
5882
|
105 key_list_iterator q |
|
106 = std::find (key_list.begin (), key_list.end (), k); |
5881
|
107 |
|
108 assert (q != key_list.end ()); |
|
109 |
|
110 key_list.erase (q); |
|
111 } |
4219
|
112 } |
3931
|
113 |
4219
|
114 iterator begin (void) { return iterator (map.begin ()); } |
|
115 const_iterator begin (void) const { return const_iterator (map.begin ()); } |
|
116 |
|
117 iterator end (void) { return iterator (map.end ()); } |
|
118 const_iterator end (void) const { return const_iterator (map.end ()); } |
3931
|
119 |
4219
|
120 std::string key (const_iterator p) const { return p->first; } |
3931
|
121 |
5328
|
122 Cell& contents (const std::string& k); |
4675
|
123 Cell contents (const std::string& k) const; |
3931
|
124 |
5328
|
125 Cell& contents (const_iterator p) |
|
126 { return contents (key(p)); } |
|
127 |
4513
|
128 Cell contents (const_iterator p) const |
4675
|
129 { return contents (key(p)); } |
3931
|
130 |
5156
|
131 int intfield (const std::string& k, int def_val = 0) const; |
|
132 |
|
133 std::string stringfield (const std::string& k, |
|
134 const std::string& def_val = std::string ()) const; |
|
135 |
5328
|
136 iterator seek (const std::string& k) { return map.find (k); } |
4587
|
137 const_iterator seek (const std::string& k) const { return map.find (k); } |
4219
|
138 |
4817
|
139 bool contains (const std::string& k) const |
4587
|
140 { return (seek (k) != map.end ()); } |
3931
|
141 |
5925
|
142 void clear (void) |
|
143 { |
|
144 map.clear (); |
|
145 key_list.clear (); |
|
146 } |
3931
|
147 |
3933
|
148 string_vector keys (void) const; |
3931
|
149 |
5275
|
150 octave_idx_type rows (void) const { return dimensions(0); } |
4561
|
151 |
5275
|
152 octave_idx_type columns (void) const { return dimensions(1); } |
4200
|
153 |
4561
|
154 dim_vector dims (void) const { return dimensions; } |
4200
|
155 |
5435
|
156 int ndims (void) const { return dimensions.length (); } |
|
157 |
5571
|
158 Octave_map transpose (void) const; |
|
159 |
4567
|
160 Octave_map reshape (const dim_vector& new_dims) const; |
|
161 |
5781
|
162 void resize (const dim_vector& dv, bool fill = false); |
4936
|
163 |
6639
|
164 octave_idx_type numel (void) const { return dimensions.numel (); } |
3932
|
165 |
5275
|
166 Octave_map concat (const Octave_map& rb, const Array<octave_idx_type>& ra_idx); |
4806
|
167 |
5592
|
168 Octave_map& maybe_delete_elements (const octave_value_list& idx); |
|
169 |
4513
|
170 Octave_map& assign (const octave_value_list& idx, const Octave_map& rhs); |
4197
|
171 |
4587
|
172 Octave_map& assign (const octave_value_list& idx, const std::string& k, |
4513
|
173 const Cell& rhs); |
3932
|
174 |
4675
|
175 Octave_map& assign (const std::string& k, const octave_value& rhs); |
|
176 |
4587
|
177 Octave_map& assign (const std::string& k, const Cell& rhs); |
3933
|
178 |
7046
|
179 Octave_map index (const octave_value_list& idx, |
|
180 bool resize_ok = false) const; |
|
181 |
3931
|
182 private: |
|
183 |
|
184 // The map of names to values. |
4513
|
185 std::map<std::string, Cell> map; |
3932
|
186 |
5880
|
187 // An extra list of keys, so we can keep track of the order the keys |
|
188 // are added for compatibility with you know what. |
|
189 std::list<std::string> key_list; |
|
190 |
4561
|
191 // The current size. |
|
192 mutable dim_vector dimensions; |
5880
|
193 |
|
194 void maybe_add_to_key_list (const std::string& k) |
5925
|
195 { |
|
196 if (! contains (k)) |
|
197 key_list.push_back (k); |
|
198 } |
746
|
199 }; |
|
200 |
|
201 #endif |
|
202 |
|
203 /* |
|
204 ;;; Local Variables: *** |
|
205 ;;; mode: C++ *** |
|
206 ;;; End: *** |
|
207 */ |