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