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 |
4219
|
27 #include <map> |
746
|
28 |
4513
|
29 #include "Cell.h" |
3931
|
30 #include "oct-obj.h" |
746
|
31 |
1755
|
32 class string_vector; |
|
33 |
746
|
34 class |
3931
|
35 Octave_map |
746
|
36 { |
|
37 public: |
4219
|
38 |
4513
|
39 typedef std::map<std::string, Cell>::iterator iterator; |
|
40 typedef std::map<std::string, Cell>::const_iterator const_iterator; |
4219
|
41 |
4744
|
42 // Warning! You should always use at least two dimensions. |
|
43 |
|
44 Octave_map (const dim_vector& dv = dim_vector (0, 0)) |
|
45 : map (), dimensions (dv) { } |
746
|
46 |
4587
|
47 Octave_map (const std::string& k, const octave_value& value) |
4561
|
48 : map (), dimensions (1, 1) |
4587
|
49 { map[k] = value; } |
4513
|
50 |
4587
|
51 Octave_map (const std::string& k, const Cell& vals) |
4561
|
52 : map (), dimensions (vals.dims ()) |
4587
|
53 { map[k] = vals; } |
746
|
54 |
4587
|
55 Octave_map (const std::string& k, const octave_value_list& val_list) |
4561
|
56 : map (), dimensions (1, val_list.length ()) |
4587
|
57 { map[k] = val_list; } |
4435
|
58 |
4561
|
59 Octave_map (const Octave_map& m) : map (m.map), dimensions (m.dimensions) { } |
3931
|
60 |
|
61 Octave_map& operator = (const Octave_map& m) |
|
62 { |
|
63 if (this != &m) |
3932
|
64 { |
|
65 map = m.map; |
4561
|
66 dimensions = m.dimensions; |
3932
|
67 } |
4561
|
68 |
3931
|
69 return *this; |
|
70 } |
746
|
71 |
|
72 ~Octave_map (void) { } |
1279
|
73 |
3932
|
74 // This is the number of keys. |
5275
|
75 octave_idx_type length (void) const { return map.size (); } |
3931
|
76 |
|
77 int empty (void) const { return map.empty (); } |
|
78 |
4587
|
79 void del (const std::string& k) |
4219
|
80 { |
4587
|
81 iterator p = map.find (k); |
4219
|
82 if (p != map.end ()) |
|
83 map.erase (p); |
|
84 } |
3931
|
85 |
4219
|
86 iterator begin (void) { return iterator (map.begin ()); } |
|
87 const_iterator begin (void) const { return const_iterator (map.begin ()); } |
|
88 |
|
89 iterator end (void) { return iterator (map.end ()); } |
|
90 const_iterator end (void) const { return const_iterator (map.end ()); } |
3931
|
91 |
4219
|
92 std::string key (const_iterator p) const { return p->first; } |
3931
|
93 |
5328
|
94 Cell& contents (const std::string& k); |
4675
|
95 Cell contents (const std::string& k) const; |
3931
|
96 |
5328
|
97 Cell& contents (const_iterator p) |
|
98 { return contents (key(p)); } |
|
99 |
4513
|
100 Cell contents (const_iterator p) const |
4675
|
101 { return contents (key(p)); } |
3931
|
102 |
5156
|
103 int intfield (const std::string& k, int def_val = 0) const; |
|
104 |
|
105 std::string stringfield (const std::string& k, |
|
106 const std::string& def_val = std::string ()) const; |
|
107 |
5328
|
108 iterator seek (const std::string& k) { return map.find (k); } |
4587
|
109 const_iterator seek (const std::string& k) const { return map.find (k); } |
4219
|
110 |
4817
|
111 bool contains (const std::string& k) const |
4587
|
112 { return (seek (k) != map.end ()); } |
3931
|
113 |
|
114 void clear (void) { map.clear (); } |
|
115 |
3933
|
116 string_vector keys (void) const; |
3931
|
117 |
5275
|
118 octave_idx_type rows (void) const { return dimensions(0); } |
4561
|
119 |
5275
|
120 octave_idx_type columns (void) const { return dimensions(1); } |
4200
|
121 |
4561
|
122 dim_vector dims (void) const { return dimensions; } |
4200
|
123 |
5435
|
124 int ndims (void) const { return dimensions.length (); } |
|
125 |
5571
|
126 Octave_map transpose (void) const; |
|
127 |
4567
|
128 Octave_map reshape (const dim_vector& new_dims) const; |
|
129 |
5781
|
130 void resize (const dim_vector& dv, bool fill = false); |
4936
|
131 |
5275
|
132 octave_idx_type numel (void) const; |
3932
|
133 |
5275
|
134 Octave_map concat (const Octave_map& rb, const Array<octave_idx_type>& ra_idx); |
4806
|
135 |
5592
|
136 Octave_map& maybe_delete_elements (const octave_value_list& idx); |
|
137 |
4513
|
138 Octave_map& assign (const octave_value_list& idx, const Octave_map& rhs); |
4197
|
139 |
4587
|
140 Octave_map& assign (const octave_value_list& idx, const std::string& k, |
4513
|
141 const Cell& rhs); |
3932
|
142 |
4675
|
143 Octave_map& assign (const std::string& k, const octave_value& rhs); |
|
144 |
4587
|
145 Octave_map& assign (const std::string& k, const Cell& rhs); |
3933
|
146 |
4513
|
147 Octave_map index (const octave_value_list& idx); |
3933
|
148 |
3931
|
149 private: |
|
150 |
|
151 // The map of names to values. |
4513
|
152 std::map<std::string, Cell> map; |
3932
|
153 |
4561
|
154 // The current size. |
|
155 mutable dim_vector dimensions; |
746
|
156 }; |
|
157 |
|
158 #endif |
|
159 |
|
160 /* |
|
161 ;;; Local Variables: *** |
|
162 ;;; mode: C++ *** |
|
163 ;;; End: *** |
|
164 */ |