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