Mercurial > hg > octave-jordi
annotate src/oct-map.h @ 8874:bd1b1fe9c6e9 ss-3-1-53
bump version info for snapshot
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 25 Feb 2009 18:35:47 -0500 |
parents | 70f5a0375afd |
children | eb63fbe60fab |
rev | line source |
---|---|
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 |
8785
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
59 Octave_map (const string_vector& sv) |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
60 : map (), key_list (), dimensions (0, 0) |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
61 { |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
62 for (octave_idx_type i = 0; i < sv.length (); i++) |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
63 { |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
64 std::string k = sv[i]; |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
65 map[k] = Cell (); |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
66 key_list.push_back (k); |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
67 } |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
68 } |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
69 |
4587 | 70 Octave_map (const std::string& k, const Cell& vals) |
5880 | 71 : map (), key_list (), dimensions (vals.dims ()) |
72 { | |
73 map[k] = vals; | |
74 key_list.push_back (k); | |
75 } | |
746 | 76 |
4587 | 77 Octave_map (const std::string& k, const octave_value_list& val_list) |
5880 | 78 : map (), key_list (), dimensions (1, val_list.length ()) |
79 { | |
80 map[k] = val_list; | |
81 key_list.push_back (k); | |
82 } | |
4435 | 83 |
5880 | 84 Octave_map (const Octave_map& m) |
85 : map (m.map), key_list (m.key_list), dimensions (m.dimensions) { } | |
3931 | 86 |
87 Octave_map& operator = (const Octave_map& m) | |
88 { | |
89 if (this != &m) | |
3932 | 90 { |
91 map = m.map; | |
5880 | 92 key_list = m.key_list; |
4561 | 93 dimensions = m.dimensions; |
3932 | 94 } |
4561 | 95 |
3931 | 96 return *this; |
97 } | |
746 | 98 |
99 ~Octave_map (void) { } | |
1279 | 100 |
7046 | 101 Octave_map squeeze (void) const; |
102 | |
103 Octave_map permute (const Array<int>& vec, bool inv = false) const; | |
104 | |
3932 | 105 // This is the number of keys. |
6639 | 106 octave_idx_type nfields (void) const { return map.size (); } |
3931 | 107 |
4587 | 108 void del (const std::string& k) |
4219 | 109 { |
4587 | 110 iterator p = map.find (k); |
5881 | 111 |
4219 | 112 if (p != map.end ()) |
5881 | 113 { |
114 map.erase (p); | |
5880 | 115 |
5882 | 116 key_list_iterator q |
117 = std::find (key_list.begin (), key_list.end (), k); | |
5881 | 118 |
119 assert (q != key_list.end ()); | |
120 | |
121 key_list.erase (q); | |
122 } | |
4219 | 123 } |
3931 | 124 |
4219 | 125 iterator begin (void) { return iterator (map.begin ()); } |
126 const_iterator begin (void) const { return const_iterator (map.begin ()); } | |
127 | |
128 iterator end (void) { return iterator (map.end ()); } | |
129 const_iterator end (void) const { return const_iterator (map.end ()); } | |
3931 | 130 |
4219 | 131 std::string key (const_iterator p) const { return p->first; } |
3931 | 132 |
5328 | 133 Cell& contents (const std::string& k); |
4675 | 134 Cell contents (const std::string& k) const; |
3931 | 135 |
5328 | 136 Cell& contents (const_iterator p) |
137 { return contents (key(p)); } | |
138 | |
4513 | 139 Cell contents (const_iterator p) const |
4675 | 140 { return contents (key(p)); } |
3931 | 141 |
5156 | 142 int intfield (const std::string& k, int def_val = 0) const; |
143 | |
144 std::string stringfield (const std::string& k, | |
145 const std::string& def_val = std::string ()) const; | |
146 | |
5328 | 147 iterator seek (const std::string& k) { return map.find (k); } |
4587 | 148 const_iterator seek (const std::string& k) const { return map.find (k); } |
4219 | 149 |
4817 | 150 bool contains (const std::string& k) const |
4587 | 151 { return (seek (k) != map.end ()); } |
3931 | 152 |
5925 | 153 void clear (void) |
154 { | |
155 map.clear (); | |
156 key_list.clear (); | |
157 } | |
3931 | 158 |
3933 | 159 string_vector keys (void) const; |
3931 | 160 |
5275 | 161 octave_idx_type rows (void) const { return dimensions(0); } |
4561 | 162 |
5275 | 163 octave_idx_type columns (void) const { return dimensions(1); } |
4200 | 164 |
4561 | 165 dim_vector dims (void) const { return dimensions; } |
4200 | 166 |
5435 | 167 int ndims (void) const { return dimensions.length (); } |
168 | |
5571 | 169 Octave_map transpose (void) const; |
170 | |
4567 | 171 Octave_map reshape (const dim_vector& new_dims) const; |
172 | |
5781 | 173 void resize (const dim_vector& dv, bool fill = false); |
4936 | 174 |
6639 | 175 octave_idx_type numel (void) const { return dimensions.numel (); } |
3932 | 176 |
5275 | 177 Octave_map concat (const Octave_map& rb, const Array<octave_idx_type>& ra_idx); |
4806 | 178 |
5592 | 179 Octave_map& maybe_delete_elements (const octave_value_list& idx); |
180 | |
4513 | 181 Octave_map& assign (const octave_value_list& idx, const Octave_map& rhs); |
4197 | 182 |
4587 | 183 Octave_map& assign (const octave_value_list& idx, const std::string& k, |
4513 | 184 const Cell& rhs); |
3932 | 185 |
4675 | 186 Octave_map& assign (const std::string& k, const octave_value& rhs); |
187 | |
4587 | 188 Octave_map& assign (const std::string& k, const Cell& rhs); |
3933 | 189 |
7046 | 190 Octave_map index (const octave_value_list& idx, |
191 bool resize_ok = false) const; | |
192 | |
3931 | 193 private: |
194 | |
195 // The map of names to values. | |
4513 | 196 std::map<std::string, Cell> map; |
3932 | 197 |
5880 | 198 // An extra list of keys, so we can keep track of the order the keys |
199 // are added for compatibility with you know what. | |
200 std::list<std::string> key_list; | |
201 | |
4561 | 202 // The current size. |
203 mutable dim_vector dimensions; | |
5880 | 204 |
205 void maybe_add_to_key_list (const std::string& k) | |
5925 | 206 { |
207 if (! contains (k)) | |
208 key_list.push_back (k); | |
209 } | |
746 | 210 }; |
211 | |
212 #endif | |
213 | |
214 /* | |
215 ;;; Local Variables: *** | |
216 ;;; mode: C++ *** | |
217 ;;; End: *** | |
218 */ |