Mercurial > octave
annotate src/oct-map.cc @ 10743:cb3ed842bd30
make the new interface more backward compatible
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 23 Jun 2010 12:21:49 +0200 |
parents | 604e13a89c7f |
children | 4716e2e17118 |
rev | line source |
---|---|
1278 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1995, 1996, 1997, 2002, 2003, 2004, 2005, 2006, 2007, |
4 2008, 2009 John W. Eaton | |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
5 Copyright (C) 2010 VZLU Prague |
1278 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
1278 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
1278 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
3932 | 29 #include "error.h" |
1755 | 30 #include "str-vec.h" |
31 | |
1278 | 32 #include "oct-map.h" |
33 #include "utils.h" | |
34 | |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
35 octave_fields::fields_rep octave_fields::nil_rep; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
36 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
37 octave_fields::octave_fields (const string_vector& fields) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
38 : rep (new fields_rep) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
39 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
40 octave_idx_type n = fields.numel (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
41 for (octave_idx_type i = 0; i < n; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
42 (*rep)[fields(i)] = i; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
43 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
44 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
45 bool |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
46 octave_fields::isfield (const std::string& field) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
47 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
48 return rep->find (field) != rep->end (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
49 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
50 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
51 octave_idx_type |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
52 octave_fields::getfield (const std::string& field) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
53 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
54 fields_rep::iterator p = rep->find (field); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
55 return (p != rep->end ()) ? p->second : -1; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
56 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
57 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
58 octave_idx_type |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
59 octave_fields::getfield (const std::string& field) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
60 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
61 fields_rep::iterator p = rep->find (field); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
62 if (p != rep->end ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
63 return p->second; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
64 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
65 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
66 make_unique (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
67 octave_idx_type n = rep->size (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
68 return (*rep)[field] = n; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
69 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
70 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
71 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
72 octave_idx_type |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
73 octave_fields::rmfield (const std::string& field) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
74 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
75 fields_rep::iterator p = rep->find (field); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
76 if (p == rep->end ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
77 return -1; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
78 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
79 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
80 octave_idx_type n = p->second; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
81 make_unique (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
82 rep->erase (field); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
83 for (fields_rep::iterator q = rep->begin (); q != rep->end (); q++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
84 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
85 if (q->second >= n) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
86 q->second--; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
87 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
88 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
89 return n; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
90 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
91 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
92 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
93 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
94 octave_fields::orderfields (Array<octave_idx_type>& perm) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
95 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
96 octave_idx_type n = rep->size (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
97 perm.clear (n, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
98 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
99 make_unique (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
100 octave_idx_type i = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
101 for (fields_rep::iterator q = rep->begin (); q != rep->end (); q++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
102 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
103 octave_idx_type j = q->second; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
104 q->second = i; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
105 perm(i++) = j; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
106 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
107 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
108 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
109 bool |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
110 octave_fields::equal_up_to_order (const octave_fields& other, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
111 Array<octave_idx_type>& perm) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
112 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
113 bool retval = true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
114 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
115 octave_idx_type n = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
116 if (perm.length () != n) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
117 perm.clear (1, n); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
118 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
119 perm.make_unique (); // optimization |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
120 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
121 iterator p = begin (), q = other.begin (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
122 for (; p != end () && q != other.end (); p++, q++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
123 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
124 if (p->first == q->first) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
125 perm.xelem(p->second) = q->second; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
126 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
127 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
128 retval = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
129 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
130 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
131 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
132 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
133 retval = (p == end () && q == other.end ()); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
134 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
135 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
136 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
137 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
138 string_vector |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
139 octave_fields::fieldnames (void) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
140 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
141 octave_idx_type n = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
142 string_vector retval(n); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
143 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
144 for (iterator p = begin (); p != end (); p++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
145 retval.xelem(p->second) = p->first; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
146 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
147 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
148 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
149 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
150 octave_value |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
151 octave_scalar_map::getfield (const std::string& k) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
152 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
153 octave_idx_type idx = keys.getfield (k); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
154 return (idx >= 0) ? vals[idx] : octave_value (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
155 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
156 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
157 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
158 octave_scalar_map::setfield (const std::string& k, const octave_value& val) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
159 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
160 octave_idx_type idx = keys.getfield (k); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
161 if (idx < static_cast<octave_idx_type> (vals.size ())) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
162 vals[idx] = val; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
163 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
164 vals.push_back (val); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
165 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
166 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
167 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
168 octave_scalar_map::rmfield (const std::string& k) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
169 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
170 octave_idx_type idx = keys.rmfield (k); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
171 if (idx >= 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
172 vals.erase (vals.begin () + idx); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
173 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
174 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
175 octave_scalar_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
176 octave_scalar_map::orderfields (void) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
177 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
178 Array<octave_idx_type> perm; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
179 return orderfields (perm); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
180 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
181 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
182 octave_scalar_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
183 octave_scalar_map::orderfields (Array<octave_idx_type>& perm) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
184 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
185 octave_scalar_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
186 retval.keys.orderfields (perm); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
187 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
188 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
189 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
190 retval.vals[i] = vals[perm.xelem(i)]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
191 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
192 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
193 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
194 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
195 octave_scalar_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
196 octave_scalar_map::orderfields (const octave_scalar_map& other, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
197 Array<octave_idx_type>& perm) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
198 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
199 if (keys.is_same (other.keys)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
200 return *this; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
201 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
202 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
203 octave_scalar_map retval (other.keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
204 if (other.keys.equal_up_to_order (keys, perm)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
205 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
206 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
207 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
208 retval.vals[i] = vals[perm.xelem(i)]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
209 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
210 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
211 error ("orderfields: structs must have same fields up to order"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
212 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
213 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
214 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
215 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
216 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
217 octave_value |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
218 octave_scalar_map::contents (const std::string& k) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
219 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
220 return getfield (k); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
221 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
222 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
223 octave_value& |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
224 octave_scalar_map::contents (const std::string& k) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
225 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
226 octave_idx_type idx = keys.getfield (k); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
227 if (idx >= static_cast<octave_idx_type> (vals.size ())) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
228 vals.resize (idx); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
229 return vals[idx]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
230 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
231 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
232 octave_map::octave_map (const octave_scalar_map& m) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
233 : keys (m.keys), vals (), dimensions (1, 1) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
234 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
235 octave_idx_type nf = m.nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
236 vals.reserve (nf); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
237 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
238 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
239 vals.push_back (Cell (dimensions)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
240 vals[i].xelem(0) = m.vals[i]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
241 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
242 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
243 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
244 octave_map::octave_map (const Octave_map& m) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
245 : keys (m.keys ()), vals (m.nfields ()), dimensions (m.dims ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
246 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
247 for (iterator p = begin (); p != end (); p++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
248 contents(p) = m.contents (key (p)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
249 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
250 optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
251 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
252 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
253 Cell |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
254 octave_map::getfield (const std::string& k) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
255 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
256 octave_idx_type idx = keys.getfield (k); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
257 return (idx >= 0) ? vals[idx] : Cell (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
258 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
259 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
260 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
261 octave_map::setfield (const std::string& k, const Cell& val) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
262 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
263 if (nfields () == 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
264 dimensions = val.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
265 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
266 if (val.dims () == dimensions) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
267 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
268 octave_idx_type idx = keys.getfield (k); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
269 if (idx < static_cast<octave_idx_type> (vals.size ())) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
270 vals[idx] = val; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
271 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
272 vals.push_back (val); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
273 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
274 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
275 error ("octave_map::setfield: internal error"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
276 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
277 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
278 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
279 octave_map::rmfield (const std::string& k) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
280 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
281 octave_idx_type idx = keys.rmfield (k); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
282 if (idx >= 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
283 vals.erase (vals.begin () + idx); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
284 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
285 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
286 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
287 octave_map::orderfields (void) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
288 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
289 Array<octave_idx_type> perm; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
290 return orderfields (perm); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
291 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
292 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
293 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
294 octave_map::orderfields (Array<octave_idx_type>& perm) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
295 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
296 octave_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
297 retval.keys.orderfields (perm); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
298 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
299 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
300 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
301 retval.vals[i] = vals[perm.xelem(i)]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
302 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
303 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
304 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
305 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
306 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
307 octave_map::orderfields (const octave_map& other, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
308 Array<octave_idx_type>& perm) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
309 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
310 if (keys.is_same (other.keys)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
311 return *this; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
312 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
313 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
314 octave_map retval (other.keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
315 if (other.keys.equal_up_to_order (keys, perm)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
316 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
317 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
318 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
319 retval.vals[i] = vals[perm.xelem(i)]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
320 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
321 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
322 error ("orderfields: structs must have same fields up to order"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
323 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
324 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
325 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
326 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
327 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
328 Cell |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
329 octave_map::contents (const std::string& k) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
330 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
331 return getfield (k); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
332 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
333 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
334 Cell& |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
335 octave_map::contents (const std::string& k) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
336 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
337 octave_idx_type idx = keys.getfield (k); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
338 if (idx >= static_cast<octave_idx_type> (vals.size ())) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
339 vals.push_back (Cell (dimensions)); // auto-set correct dims. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
340 return vals[idx]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
341 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
342 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
343 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
344 octave_map::extract_scalar (octave_scalar_map& dest, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
345 octave_idx_type idx) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
346 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
347 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
348 dest.vals.reserve (nf); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
349 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
350 dest.vals.push_back (vals[i](idx)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
351 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
352 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
353 octave_scalar_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
354 octave_map::checkelem (octave_idx_type n) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
355 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
356 octave_scalar_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
357 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
358 // Optimize this so that there is just one check. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
359 extract_scalar (retval, compute_index (n, dimensions)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
360 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
361 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
362 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
363 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
364 octave_scalar_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
365 octave_map::checkelem (octave_idx_type i, octave_idx_type j) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
366 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
367 octave_scalar_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
368 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
369 // Optimize this so that there is just one check. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
370 extract_scalar (retval, compute_index (i, j, dimensions)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
371 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
372 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
373 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
374 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
375 octave_scalar_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
376 octave_map::checkelem (const Array<octave_idx_type>& ra_idx) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
377 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
378 octave_scalar_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
379 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
380 // Optimize this so that there is just one check. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
381 extract_scalar (retval, compute_index (ra_idx, dimensions)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
382 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
383 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
384 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
385 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
386 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
387 octave_map::squeeze (void) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
388 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
389 octave_map retval (*this); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
390 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
391 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
392 retval.dimensions = dimensions.squeeze (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
393 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
394 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
395 retval.vals[i] = vals[i].squeeze (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
396 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
397 retval.optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
398 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
399 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
400 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
401 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
402 /* |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
403 %!# test preservation of keys by squeeze |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
404 %!test |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
405 %! x(1,1,1,1).d = 10; x(3,5,1,7).a = "b"; x(2,4,1,7).f = 27; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
406 %! assert (fieldnames (squeeze (x)), {"d"; "a"; "f"}); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
407 */ |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
408 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
409 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
410 octave_map::permute (const Array<int>& vec, bool inv) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
411 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
412 octave_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
413 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
414 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
415 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
416 retval.vals[i] = vals[i].permute (vec, inv); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
417 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
418 // FIXME: |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
419 // There is no dim_vector::permute for technical reasons. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
420 // We pick the dim vector from results if possible, otherwise use a dummy |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
421 // array to get it. Need (?) a better solution to this problem. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
422 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
423 retval.dimensions = retval.vals[0].dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
424 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
425 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
426 Array<char> dummy (dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
427 dummy = dummy.permute (vec, inv); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
428 retval.dimensions = dummy.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
429 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
430 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
431 retval.optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
432 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
433 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
434 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
435 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
436 /* |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
437 %!# test preservation of key order by permute |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
438 %!test |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
439 %! x(1,1,1,1).d = 10; x(3,5,1,7).a = "b"; x(2,4,1,7).f = 27; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
440 %! assert (fieldnames (permute (x, [3, 4, 1, 2])), {"d"; "a"; "f"}); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
441 */ |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
442 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
443 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
444 octave_map::transpose (void) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
445 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
446 assert (ndims () == 2); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
447 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
448 octave_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
449 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
450 retval.dimensions = dim_vector (dimensions (1), dimensions (0)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
451 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
452 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
453 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
454 retval.vals[i] = vals[i].transpose (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
455 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
456 retval.optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
457 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
458 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
459 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
460 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
461 /* |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
462 %!# test preservation of key order by transpose |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
463 %!test |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
464 %! x(1,1).d = 10; x(3,5).a = "b"; x(2,4).f = 27; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
465 %! assert (fieldnames (transpose (x)), {"d"; "a"; "f"}); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
466 %! assert (fieldnames (x'), {"d"; "a"; "f"}); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
467 %! assert (fieldnames (x.'), {"d"; "a"; "f"}); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
468 */ |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
469 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
470 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
471 octave_map::reshape (const dim_vector& dv) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
472 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
473 octave_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
474 retval.dimensions = dv; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
475 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
476 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
477 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
478 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
479 retval.vals.reserve (nf); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
480 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
481 retval.vals[i] = vals[i].reshape (dv); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
482 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
483 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
484 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
485 // FIXME: Do it with a dummy array, to reuse error message. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
486 // Need (?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
487 Array<char> dummy (dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
488 dummy.reshape (dv); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
489 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
490 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
491 retval.optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
492 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
493 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
494 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
495 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
496 /* |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
497 %!# test preservation of key order by reshape |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
498 %!test |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
499 %! x(1,1).d = 10; x(4,6).a = "b"; x(2,4).f = 27; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
500 %! assert (fieldnames (reshape (x, 3, 8)), {"d"; "a"; "f"}); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
501 */ |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
502 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
503 void |
10743
cb3ed842bd30
make the new interface more backward compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
504 octave_map::resize (const dim_vector& dv, bool fill) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
505 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
506 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
507 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
508 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
509 for (octave_idx_type i = 0; i < nf; i++) |
10743
cb3ed842bd30
make the new interface more backward compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
510 { |
cb3ed842bd30
make the new interface more backward compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
511 if (fill) |
cb3ed842bd30
make the new interface more backward compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
512 vals[i].resize (dv, Cell::resize_fill_value ()); |
cb3ed842bd30
make the new interface more backward compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
513 else |
cb3ed842bd30
make the new interface more backward compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
514 vals[i].resize (dv); |
cb3ed842bd30
make the new interface more backward compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
515 } |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
516 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
517 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
518 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
519 // FIXME: Do it with a dummy array, to reuse error message. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
520 // Need (?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
521 Array<char> dummy (dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
522 dummy.resize (dv); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
523 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
524 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
525 dimensions = dv; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
526 optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
527 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
528 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
529 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
530 octave_map::do_cat (int dim, octave_idx_type n, const octave_scalar_map *map_list, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
531 octave_map& retval) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
532 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
533 octave_idx_type nf = retval.nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
534 retval.vals.reserve (nf); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
535 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
536 dim_vector& rd = retval.dimensions; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
537 rd.resize (dim+1, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
538 rd(0) = rd(1) = 1; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
539 rd(dim) = n; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
540 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
541 for (octave_idx_type j = 0; j < nf; j++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
542 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
543 retval.vals.push_back (Cell (rd)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
544 assert (retval.vals[j].numel () == n); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
545 for (octave_idx_type i = 0; i < n; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
546 retval.vals[j].xelem(i) = map_list[i].vals[j]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
547 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
548 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
549 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
550 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
551 octave_map::do_cat (int dim, octave_idx_type n, const octave_map *map_list, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
552 octave_map& retval) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
553 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
554 octave_idx_type nf = retval.nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
555 retval.vals.reserve (nf); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
556 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
557 OCTAVE_LOCAL_BUFFER (Array<octave_value>, field_list, n); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
558 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
559 for (octave_idx_type j = 0; j < nf; j++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
560 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
561 for (octave_idx_type i = 0; i < n; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
562 field_list[i] = map_list[i].vals[j]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
563 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
564 retval.vals.push_back (Array<octave_value>::cat (dim, n, field_list)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
565 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
566 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
567 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
568 template <class map> |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
569 static void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
570 permute_to_correct_order (octave_idx_type n, octave_idx_type nf, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
571 const map *map_list, map *new_map_list) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
572 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
573 new_map_list[0] = map_list[0]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
574 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
575 Array<octave_idx_type> perm (1, nf); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
576 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
577 for (octave_idx_type i = 1; i < n; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
578 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
579 new_map_list[i] = map_list[i].orderfields (map_list[0], perm); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
580 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
581 if (error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
582 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
583 // Use liboctave exception to be consistent. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
584 (*current_liboctave_error_handler) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
585 ("cat: field names mismatch in concatenating structs"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
586 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
587 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
588 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
589 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
590 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
591 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
592 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
593 octave_map::cat (int dim, octave_idx_type n, const octave_scalar_map *map_list) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
594 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
595 octave_map retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
596 if (n > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
597 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
598 retval.keys = map_list[0].keys; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
599 octave_idx_type nf = map_list[0].nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
600 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
601 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
602 // Try the fast case. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
603 bool all_same = true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
604 for (octave_idx_type i = 1; i < n; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
605 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
606 all_same = map_list[0].keys.is_same (map_list[i].keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
607 if (! all_same) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
608 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
609 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
610 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
611 if (all_same) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
612 do_cat (dim, n, map_list, retval); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
613 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
614 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
615 // permute all structures to common order. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
616 OCTAVE_LOCAL_BUFFER (octave_scalar_map, new_map_list, n); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
617 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
618 permute_to_correct_order (n, nf, map_list, new_map_list); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
619 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
620 do_cat (dim, n, new_map_list, retval); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
621 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
622 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
623 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
624 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
625 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
626 dim_vector& rd = retval.dimensions; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
627 rd.resize (dim+1, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
628 rd(0) = rd(1) = 1; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
629 rd(dim) = n; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
630 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
631 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
632 retval.optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
633 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
634 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
635 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
636 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
637 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
638 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
639 octave_map::cat (int dim, octave_idx_type n, const octave_map *map_list) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
640 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
641 octave_map retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
642 if (n > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
643 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
644 retval.keys = map_list[0].keys; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
645 octave_idx_type nf = map_list[0].nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
646 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
647 // Try the fast case. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
648 bool all_same = true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
649 for (octave_idx_type i = 1; i < n; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
650 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
651 all_same = map_list[0].keys.is_same (map_list[i].keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
652 if (! all_same) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
653 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
654 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
655 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
656 if (all_same) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
657 do_cat (dim, n, map_list, retval); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
658 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
659 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
660 // permute all structures to correct order. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
661 OCTAVE_LOCAL_BUFFER (octave_map, new_map_list, n); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
662 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
663 permute_to_correct_order (n, nf, map_list, new_map_list); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
664 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
665 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
666 do_cat (dim, n, new_map_list, retval); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
667 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
668 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
669 // Use dummy arrays. FIXME: Need(?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
670 OCTAVE_LOCAL_BUFFER (Array<char>, dummy, n); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
671 for (octave_idx_type i = 0; i < n; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
672 dummy[i].clear (map_list[0].dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
673 Array<char>::cat (dim, n, dummy); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
674 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
675 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
676 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
677 retval.optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
678 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
679 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
680 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
681 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
682 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
683 /* |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
684 %!# test preservation of key order by concatenation |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
685 %!test |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
686 %! x(1, 1).d = 10; x(4, 6).a = "b"; x(2, 4).f = 27; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
687 %! y(1, 6).f = 11; y(1, 6).a = "c"; y(1, 6).d = 33; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
688 %! assert (fieldnames ([x; y]), {"d"; "a"; "f"}); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
689 */ |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
690 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
691 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
692 octave_map::index (const idx_vector& i, bool resize_ok) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
693 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
694 octave_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
695 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
696 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
697 for (octave_idx_type k = 0; k < nf; k++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
698 retval.vals[k] = vals[k].index (i, resize_ok); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
699 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
700 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
701 retval.dimensions = retval.vals[0].dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
702 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
703 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
704 // Use dummy array. FIXME: Need(?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
705 Array<char> dummy (dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
706 dummy = dummy.index (i, resize_ok); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
707 retval.dimensions = dummy.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
708 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
709 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
710 retval.optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
711 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
712 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
713 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
714 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
715 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
716 octave_map::index (const idx_vector& i, const idx_vector& j, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
717 bool resize_ok) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
718 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
719 octave_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
720 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
721 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
722 for (octave_idx_type k = 0; k < nf; k++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
723 retval.vals[k] = vals[k].index (i, j, resize_ok); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
724 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
725 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
726 retval.dimensions = retval.vals[0].dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
727 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
728 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
729 // Use dummy array. FIXME: Need(?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
730 Array<char> dummy (dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
731 dummy = dummy.index (i, j, resize_ok); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
732 retval.dimensions = dummy.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
733 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
734 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
735 retval.optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
736 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
737 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
738 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
739 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
740 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
741 octave_map::index (const Array<idx_vector>& ia, bool resize_ok) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
742 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
743 octave_map retval (keys); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
744 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
745 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
746 for (octave_idx_type k = 0; k < nf; k++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
747 retval.vals[k] = vals[k].index (ia, resize_ok); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
748 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
749 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
750 retval.dimensions = retval.vals[0].dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
751 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
752 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
753 // Use dummy array. FIXME: Need(?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
754 Array<char> dummy (dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
755 dummy = dummy.index (ia, resize_ok); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
756 retval.dimensions = dummy.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
757 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
758 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
759 retval.optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
760 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
761 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
762 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
763 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
764 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
765 octave_map::index (const octave_value_list& idx, bool resize_ok) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
766 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
767 octave_idx_type n_idx = idx.length (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
768 octave_map retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
769 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
770 switch (n_idx) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
771 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
772 case 1: |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
773 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
774 idx_vector i = idx(0).index_vector (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
775 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
776 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
777 retval = index (i, resize_ok); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
778 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
779 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
780 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
781 case 2: |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
782 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
783 idx_vector i = idx(0).index_vector (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
784 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
785 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
786 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
787 idx_vector j = idx(1).index_vector (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
788 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
789 retval = index (i, j, resize_ok); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
790 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
791 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
792 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
793 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
794 default: |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
795 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
796 Array<idx_vector> ia (n_idx, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
797 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
798 for (octave_idx_type i = 0; i < n_idx; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
799 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
800 ia(i) = idx(i).index_vector (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
801 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
802 if (error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
803 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
804 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
805 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
806 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
807 retval = index (ia, resize_ok); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
808 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
809 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
810 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
811 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
812 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
813 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
814 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
815 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
816 octave_map::assign (const idx_vector& i, const octave_map& rhs) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
817 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
818 if (rhs.keys.is_same (keys)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
819 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
820 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
821 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
822 for (octave_idx_type k = 0; k < nf; k++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
823 vals[k].assign (i, rhs.vals[k]); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
824 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
825 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
826 dimensions = vals[0].dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
827 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
828 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
829 // Use dummy array. FIXME: Need(?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
830 Array<char> dummy (dimensions), rhs_dummy (rhs.dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
831 dummy.assign (i, rhs_dummy);; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
832 dimensions = dummy.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
833 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
834 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
835 optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
836 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
837 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
838 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
839 Array<octave_idx_type> perm; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
840 octave_map rhs1 = rhs.orderfields (*this, perm); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
841 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
842 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
843 assert (rhs1.keys.is_same (rhs.keys)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
844 assign (i, rhs1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
845 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
846 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
847 error ("incompatible fields in struct assignment"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
848 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
849 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
850 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
851 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
852 octave_map::assign (const idx_vector& i, const idx_vector& j, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
853 const octave_map& rhs) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
854 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
855 if (rhs.keys.is_same (keys)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
856 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
857 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
858 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
859 for (octave_idx_type k = 0; k < nf; k++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
860 vals[k].assign (i, j, rhs.vals[k]); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
861 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
862 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
863 dimensions = vals[0].dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
864 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
865 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
866 // Use dummy array. FIXME: Need(?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
867 Array<char> dummy (dimensions), rhs_dummy (rhs.dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
868 dummy.assign (i, j, rhs_dummy);; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
869 dimensions = dummy.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
870 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
871 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
872 optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
873 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
874 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
875 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
876 Array<octave_idx_type> perm; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
877 octave_map rhs1 = rhs.orderfields (*this, perm); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
878 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
879 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
880 assert (rhs1.keys.is_same (rhs.keys)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
881 assign (i, j, rhs1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
882 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
883 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
884 error ("incompatible fields in struct assignment"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
885 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
886 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
887 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
888 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
889 octave_map::assign (const Array<idx_vector>& ia, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
890 const octave_map& rhs) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
891 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
892 if (rhs.keys.is_same (keys)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
893 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
894 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
895 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
896 for (octave_idx_type k = 0; k < nf; k++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
897 vals[k].assign (ia, rhs.vals[k]); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
898 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
899 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
900 dimensions = vals[0].dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
901 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
902 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
903 // Use dummy array. FIXME: Need(?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
904 Array<char> dummy (dimensions), rhs_dummy (rhs.dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
905 dummy.assign (ia, rhs_dummy);; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
906 dimensions = dummy.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
907 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
908 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
909 optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
910 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
911 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
912 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
913 Array<octave_idx_type> perm; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
914 octave_map rhs1 = rhs.orderfields (*this, perm); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
915 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
916 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
917 assert (rhs1.keys.is_same (rhs.keys)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
918 assign (ia, rhs1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
919 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
920 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
921 error ("incompatible fields in struct assignment"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
922 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
923 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
924 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
925 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
926 octave_map::assign (const octave_value_list& idx, const octave_map& rhs) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
927 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
928 octave_idx_type n_idx = idx.length (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
929 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
930 switch (n_idx) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
931 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
932 case 1: |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
933 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
934 idx_vector i = idx(0).index_vector (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
935 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
936 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
937 assign (i, rhs); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
938 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
939 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
940 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
941 case 2: |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
942 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
943 idx_vector i = idx(0).index_vector (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
944 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
945 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
946 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
947 idx_vector j = idx(1).index_vector (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
948 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
949 assign (i, j, rhs); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
950 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
951 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
952 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
953 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
954 default: |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
955 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
956 Array<idx_vector> ia (n_idx, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
957 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
958 for (octave_idx_type i = 0; i < n_idx; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
959 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
960 ia(i) = idx(i).index_vector (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
961 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
962 if (error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
963 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
964 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
965 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
966 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
967 assign (ia, rhs); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
968 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
969 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
970 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
971 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
972 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
973 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
974 octave_map::delete_elements (const idx_vector& i) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
975 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
976 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
977 for (octave_idx_type k = 0; k < nf; k++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
978 vals[k].delete_elements (i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
979 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
980 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
981 dimensions = vals[0].dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
982 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
983 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
984 // Use dummy array. FIXME: Need(?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
985 Array<char> dummy (dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
986 dummy.delete_elements (i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
987 dimensions = dummy.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
988 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
989 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
990 optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
991 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
992 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
993 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
994 octave_map::delete_elements (int dim, const idx_vector& i) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
995 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
996 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
997 for (octave_idx_type k = 0; k < nf; k++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
998 vals[k].delete_elements (dim, i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
999 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1000 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1001 dimensions = vals[0].dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1002 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1003 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1004 // Use dummy array. FIXME: Need(?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1005 Array<char> dummy (dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1006 dummy.delete_elements (dim, i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1007 dimensions = dummy.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1008 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1009 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1010 optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1011 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1012 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1013 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1014 octave_map::delete_elements (const Array<idx_vector>& ia) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1015 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1016 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1017 for (octave_idx_type k = 0; k < nf; k++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1018 vals[k].delete_elements (ia); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1019 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1020 if (nf > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1021 dimensions = vals[0].dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1022 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1023 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1024 // Use dummy array. FIXME: Need(?) a better solution. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1025 Array<char> dummy (dimensions); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1026 dummy.delete_elements (ia); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1027 dimensions = dummy.dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1028 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1029 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1030 optimize_dimensions (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1031 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1032 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1033 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1034 octave_map::delete_elements (const octave_value_list& idx) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1035 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1036 octave_idx_type n_idx = idx.length (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1037 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1038 Array<idx_vector> ia (n_idx, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1039 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1040 for (octave_idx_type i = 0; i < n_idx; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1041 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1042 ia(i) = idx(i).index_vector (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1043 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1044 if (error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1045 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1046 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1047 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1048 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1049 delete_elements (ia); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1050 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1051 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1052 /* |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1053 %!# test preservation of key order by indexing |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1054 %!test |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1055 %! x(1, 1).d = 10; x(4, 6).a = "b"; x(2, 4).f = 27; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1056 %! assert (fieldnames (x([1, 2], [2:5])), {"d"; "a"; "f"}); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1057 */ |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1058 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1059 octave_map |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1060 octave_map::concat (const octave_map& rb, const Array<octave_idx_type>& ra_idx) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1061 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1062 if (nfields () == rb.nfields ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1063 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1064 for (const_iterator pa = begin (); pa != end (); pa++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1065 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1066 const_iterator pb = rb.seek (key(pa)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1067 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1068 if (pb == rb.end ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1069 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1070 error ("field name mismatch in structure concatenation"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1071 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1072 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1073 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1074 contents(pa).insert (rb.contents(pb), ra_idx); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1075 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1076 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1077 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1078 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1079 dim_vector dv = dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1080 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1081 if (dv.all_zero ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1082 *this = rb; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1083 else if (! rb.dims ().all_zero ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1084 error ("invalid structure concatenation"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1085 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1086 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1087 return *this; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1088 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1089 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1090 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1091 octave_map::optimize_dimensions (void) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1092 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1093 octave_idx_type nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1094 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1095 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1096 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1097 if (! vals[i].optimize_dimensions (dimensions)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1098 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1099 error ("internal error: dimension mismatch across fields in struct"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1100 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1101 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1102 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1103 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1104 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1105 |
6959 | 1106 Octave_map::Octave_map (const dim_vector& dv, const Cell& key_vals) |
5880 | 1107 : map (), key_list (), dimensions (dv) |
1108 { | |
1109 Cell c (dv); | |
1110 | |
6959 | 1111 if (key_vals.is_cellstr ()) |
5880 | 1112 { |
6959 | 1113 for (octave_idx_type i = 0; i < key_vals.numel (); i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1114 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1115 std::string k = key_vals(i).string_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1116 map[k] = c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1117 key_list.push_back (k); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1118 } |
5880 | 1119 } |
6946 | 1120 else |
1121 error ("Octave_map: expecting keys to be cellstr"); | |
5880 | 1122 } |
1123 | |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1124 Octave_map::Octave_map (const octave_map& m) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1125 : map (), key_list (), dimensions (m.dims ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1126 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1127 for (octave_map::const_iterator p = m.begin (); p != m.end (); p++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1128 map[m.key (p)] = m.contents (p); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1129 const string_vector mkeys = m.fieldnames (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1130 for (octave_idx_type i = 0; i < mkeys.numel (); i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1131 key_list.push_back (mkeys(i)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1132 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10649
diff
changeset
|
1133 |
7046 | 1134 Octave_map |
1135 Octave_map::squeeze (void) const | |
1136 { | |
1137 Octave_map retval (dims ().squeeze ()); | |
1138 | |
1139 for (const_iterator pa = begin (); pa != end (); pa++) | |
1140 { | |
1141 Cell tmp = contents (pa).squeeze (); | |
1142 | |
1143 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1144 break; |
7046 | 1145 |
1146 retval.assign (key (pa), tmp); | |
1147 } | |
1148 | |
9379
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1149 // Preserve order of keys. |
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1150 retval.key_list = key_list; |
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1151 |
7046 | 1152 return retval; |
1153 } | |
1154 | |
1155 Octave_map | |
1156 Octave_map::permute (const Array<int>& vec, bool inv) const | |
1157 { | |
1158 Octave_map retval (dims ()); | |
1159 | |
1160 for (const_iterator pa = begin (); pa != end (); pa++) | |
1161 { | |
1162 Cell tmp = contents (pa).permute (vec, inv); | |
1163 | |
1164 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1165 break; |
7046 | 1166 |
1167 retval.assign (key (pa), tmp); | |
1168 } | |
1169 | |
9379
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1170 // Preserve order of keys. |
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1171 retval.key_list = key_list; |
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1172 |
7046 | 1173 return retval; |
1174 } | |
1175 | |
5328 | 1176 Cell& |
1177 Octave_map::contents (const std::string& k) | |
1178 { | |
5880 | 1179 maybe_add_to_key_list (k); |
1180 | |
5328 | 1181 return map[k]; |
1182 } | |
1183 | |
4513 | 1184 Cell |
4675 | 1185 Octave_map::contents (const std::string& k) const |
4197 | 1186 { |
4587 | 1187 const_iterator p = seek (k); |
4197 | 1188 |
4513 | 1189 return p != end () ? p->second : Cell (); |
4197 | 1190 } |
1191 | |
5156 | 1192 int |
1193 Octave_map::intfield (const std::string& k, int def_val) const | |
1194 { | |
1195 int retval = def_val; | |
1196 | |
1197 Cell c = contents (k); | |
1198 | |
1199 if (! c.is_empty ()) | |
1200 retval = c(0).int_value (); | |
1201 | |
1202 return retval; | |
1203 } | |
1204 | |
1205 std::string | |
1206 Octave_map::stringfield (const std::string& k, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1207 const std::string& def_val) const |
5156 | 1208 { |
1209 std::string retval = def_val; | |
1210 | |
1211 Cell c = contents (k); | |
1212 | |
1213 if (! c.is_empty ()) | |
1214 retval = c(0).string_value (); | |
1215 | |
1216 return retval; | |
1217 } | |
1218 | |
1755 | 1219 string_vector |
3933 | 1220 Octave_map::keys (void) const |
1278 | 1221 { |
6639 | 1222 assert (nfields () == key_list.size ()); |
5881 | 1223 |
5880 | 1224 return string_vector (key_list); |
1278 | 1225 } |
1226 | |
4567 | 1227 Octave_map |
5571 | 1228 Octave_map::transpose (void) const |
1229 { | |
1230 assert (ndims () == 2); | |
5593 | 1231 |
5571 | 1232 dim_vector dv = dims (); |
1233 | |
5593 | 1234 octave_idx_type nr = dv(0); |
1235 octave_idx_type nc = dv(1); | |
1236 | |
1237 dim_vector new_dims (nc, nr); | |
1238 | |
1239 Octave_map retval (new_dims); | |
5571 | 1240 |
1241 for (const_iterator p = begin (); p != end (); p++) | |
1242 retval.assign (key(p), Cell (contents(p).transpose ())); | |
1243 | |
9379
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1244 // Preserve order of keys. |
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1245 retval.key_list = key_list; |
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1246 |
5571 | 1247 return retval; |
1248 } | |
1249 | |
1250 Octave_map | |
4567 | 1251 Octave_map::reshape (const dim_vector& new_dims) const |
1252 { | |
1253 Octave_map retval; | |
1254 | |
1255 if (new_dims != dims ()) | |
1256 { | |
1257 for (const_iterator p = begin (); p != end (); p++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1258 retval.assign (key(p), contents(p).reshape (new_dims)); |
4567 | 1259 |
4936 | 1260 retval.dimensions = new_dims; |
9379
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1261 |
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1262 // Preserve order of keys. |
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1263 retval.key_list = key_list; |
4567 | 1264 } |
4936 | 1265 else |
1266 retval = *this; | |
4567 | 1267 |
1268 return retval; | |
1269 } | |
1270 | |
5781 | 1271 void |
1272 Octave_map::resize (const dim_vector& dv, bool fill) | |
4936 | 1273 { |
1274 if (dv != dims ()) | |
1275 { | |
6639 | 1276 if (nfields () == 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1277 dimensions = dv; |
6609 | 1278 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1279 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1280 for (const_iterator p = begin (); p != end (); p++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1281 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1282 Cell tmp = contents(p); |
5781 | 1283 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1284 if (fill) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1285 tmp.resize (dv, Cell::resize_fill_value ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1286 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1287 tmp.resize (dv); |
5781 | 1288 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1289 dimensions = dv; |
5781 | 1290 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1291 assign (key(p), tmp); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1292 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1293 } |
4936 | 1294 } |
1295 } | |
1296 | |
4915 | 1297 Octave_map |
5275 | 1298 Octave_map::concat (const Octave_map& rb, const Array<octave_idx_type>& ra_idx) |
4806 | 1299 { |
4937 | 1300 Octave_map retval; |
4936 | 1301 |
6639 | 1302 if (nfields () == rb.nfields ()) |
4936 | 1303 { |
5881 | 1304 for (const_iterator pa = begin (); pa != end (); pa++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1305 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1306 const_iterator pb = rb.seek (key(pa)); |
4937 | 1307 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1308 if (pb == rb.end ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1309 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1310 error ("field name mismatch in structure concatenation"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1311 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1312 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1313 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1314 retval.assign (key(pa), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1315 contents(pa).insert (rb.contents(pb), ra_idx)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1316 } |
9379
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1317 |
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1318 // Preserve order of keys. |
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1319 retval.key_list = key_list; |
4936 | 1320 } |
4937 | 1321 else |
7959
a73b80cd1f10
allow empty matrix by cell (or struct) concatentation
John W. Eaton <jwe@octave.org>
parents:
7046
diff
changeset
|
1322 { |
a73b80cd1f10
allow empty matrix by cell (or struct) concatentation
John W. Eaton <jwe@octave.org>
parents:
7046
diff
changeset
|
1323 dim_vector dv = dims (); |
a73b80cd1f10
allow empty matrix by cell (or struct) concatentation
John W. Eaton <jwe@octave.org>
parents:
7046
diff
changeset
|
1324 |
a73b80cd1f10
allow empty matrix by cell (or struct) concatentation
John W. Eaton <jwe@octave.org>
parents:
7046
diff
changeset
|
1325 if (dv.all_zero ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1326 retval = rb; |
7959
a73b80cd1f10
allow empty matrix by cell (or struct) concatentation
John W. Eaton <jwe@octave.org>
parents:
7046
diff
changeset
|
1327 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1328 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1329 dv = rb.dims (); |
7959
a73b80cd1f10
allow empty matrix by cell (or struct) concatentation
John W. Eaton <jwe@octave.org>
parents:
7046
diff
changeset
|
1330 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1331 if (dv.all_zero ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1332 retval = *this; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1333 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1334 error ("invalid structure concatenation"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1335 } |
7959
a73b80cd1f10
allow empty matrix by cell (or struct) concatentation
John W. Eaton <jwe@octave.org>
parents:
7046
diff
changeset
|
1336 } |
4937 | 1337 |
4915 | 1338 return retval; |
4806 | 1339 } |
1340 | |
6609 | 1341 static bool |
1342 keys_ok (const Octave_map& a, const Octave_map& b, string_vector& keys) | |
4197 | 1343 { |
6609 | 1344 bool retval = false; |
1345 | |
1346 keys = string_vector (); | |
4197 | 1347 |
6639 | 1348 if (a.nfields () == 0) |
6609 | 1349 { |
1350 keys = b.keys (); | |
1351 retval = true; | |
1352 } | |
1353 else | |
1354 { | |
8503
8ba2ee57c594
remove qsort in favor of sort
Jaroslav Hajek <highegg@gmail.com>
parents:
8294
diff
changeset
|
1355 string_vector a_keys = a.keys().sort (); |
8ba2ee57c594
remove qsort in favor of sort
Jaroslav Hajek <highegg@gmail.com>
parents:
8294
diff
changeset
|
1356 string_vector b_keys = b.keys().sort (); |
4197 | 1357 |
6609 | 1358 octave_idx_type a_len = a_keys.length (); |
1359 octave_idx_type b_len = b_keys.length (); | |
1360 | |
1361 if (a_len == b_len) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1362 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1363 for (octave_idx_type i = 0; i < a_len; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1364 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1365 if (a_keys[i] != b_keys[i]) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1366 goto done; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1367 } |
4197 | 1368 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1369 keys = a_keys; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1370 retval = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1371 } |
4197 | 1372 } |
6609 | 1373 |
1374 done: | |
4197 | 1375 return retval; |
1376 } | |
1377 | |
1378 Octave_map& | |
5592 | 1379 Octave_map::maybe_delete_elements (const octave_value_list& idx) |
1380 { | |
1381 string_vector t_keys = keys(); | |
1382 octave_idx_type len = t_keys.length (); | |
1383 | |
1384 if (len > 0) | |
1385 { | |
1386 for (octave_idx_type i = 0; i < len; i++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1387 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1388 std::string k = t_keys[i]; |
5592 | 1389 |
10649
64472dd48517
cosmetic changes in Cell interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
1390 contents(k).delete_elements (idx); |
5592 | 1391 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1392 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1393 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1394 } |
5592 | 1395 |
1396 if (!error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1397 dimensions = contents(t_keys[0]).dims(); |
5592 | 1398 } |
1399 | |
1400 return *this; | |
1401 } | |
1402 | |
1403 Octave_map& | |
4513 | 1404 Octave_map::assign (const octave_value_list& idx, const Octave_map& rhs) |
4197 | 1405 { |
6609 | 1406 string_vector t_keys; |
4197 | 1407 |
6609 | 1408 if (keys_ok (*this, rhs, t_keys)) |
4197 | 1409 { |
5275 | 1410 octave_idx_type len = t_keys.length (); |
4197 | 1411 |
6609 | 1412 if (len == 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1413 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1414 Cell tmp_lhs (dims ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1415 Cell tmp_rhs (rhs.dims ()); |
6609 | 1416 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1417 tmp_lhs.assign (idx, tmp_rhs, Matrix ()); |
4197 | 1418 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1419 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1420 resize (tmp_lhs.dims ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1421 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1422 error ("size mismatch in structure assignment"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1423 } |
6609 | 1424 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1425 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1426 for (octave_idx_type i = 0; i < len; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1427 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1428 std::string k = t_keys[i]; |
4197 | 1429 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1430 Cell t_rhs = rhs.contents (k); |
6609 | 1431 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1432 assign (idx, k, t_rhs); |
4197 | 1433 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1434 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1435 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1436 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1437 } |
4197 | 1438 } |
1439 else | |
1440 error ("field name mismatch in structure assignment"); | |
1441 | |
1442 return *this; | |
1443 } | |
1444 | |
3932 | 1445 Octave_map& |
4587 | 1446 Octave_map::assign (const octave_value_list& idx, const std::string& k, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1447 const Cell& rhs) |
3932 | 1448 { |
5881 | 1449 Cell tmp; |
1450 | |
1451 if (contains (k)) | |
1452 tmp = map[k]; | |
9127
5ec4dc52c131
fix empty struct indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1453 else |
5ec4dc52c131
fix empty struct indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1454 tmp = Cell (dimensions); |
3932 | 1455 |
9127
5ec4dc52c131
fix empty struct indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1456 tmp.assign (idx, rhs); |
3932 | 1457 |
1458 if (! error_state) | |
1459 { | |
9127
5ec4dc52c131
fix empty struct indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1460 dim_vector tmp_dims = tmp.dims (); |
3932 | 1461 |
9127
5ec4dc52c131
fix empty struct indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1462 if (tmp_dims != dimensions) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1463 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1464 for (iterator p = begin (); p != end (); p++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1465 contents(p).resize (tmp_dims, Cell::resize_fill_value ()); |
9127
5ec4dc52c131
fix empty struct indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1466 |
5ec4dc52c131
fix empty struct indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1467 dimensions = tmp_dims; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1468 } |
3932 | 1469 |
5880 | 1470 maybe_add_to_key_list (k); |
1471 | |
4587 | 1472 map[k] = tmp; |
3932 | 1473 } |
1474 | |
1475 return *this; | |
1476 } | |
1477 | |
3933 | 1478 Octave_map& |
4675 | 1479 Octave_map::assign (const std::string& k, const octave_value& rhs) |
1480 { | |
6639 | 1481 if (nfields () == 0) |
4675 | 1482 { |
5880 | 1483 maybe_add_to_key_list (k); |
1484 | |
4675 | 1485 map[k] = Cell (rhs); |
1486 | |
1487 dimensions = dim_vector (1, 1); | |
1488 } | |
1489 else | |
1490 { | |
1491 dim_vector dv = dims (); | |
1492 | |
1493 if (dv.all_ones ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1494 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1495 maybe_add_to_key_list (k); |
5880 | 1496 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1497 map[k] = Cell (rhs); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1498 } |
4675 | 1499 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1500 error ("invalid structure assignment"); |
4675 | 1501 } |
1502 | |
1503 return *this; | |
1504 } | |
1505 | |
1506 Octave_map& | |
4587 | 1507 Octave_map::assign (const std::string& k, const Cell& rhs) |
3933 | 1508 { |
6639 | 1509 if (nfields () == 0) |
4563 | 1510 { |
5880 | 1511 maybe_add_to_key_list (k); |
1512 | |
4587 | 1513 map[k] = rhs; |
4563 | 1514 |
4730 | 1515 dimensions = rhs.dims (); |
4563 | 1516 } |
3933 | 1517 else |
1518 { | |
4562 | 1519 if (dims () == rhs.dims ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1520 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1521 maybe_add_to_key_list (k); |
5880 | 1522 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1523 map[k] = rhs; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1524 } |
3933 | 1525 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1526 error ("invalid structure assignment"); |
3933 | 1527 } |
1528 | |
1529 return *this; | |
1530 } | |
1531 | |
1532 Octave_map | |
7046 | 1533 Octave_map::index (const octave_value_list& idx, bool resize_ok) const |
3933 | 1534 { |
1535 Octave_map retval; | |
9379
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1536 |
8682
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1537 octave_idx_type n_idx = idx.length (); |
9379
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1538 |
8682
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1539 if (n_idx > 0) |
3933 | 1540 { |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
1541 Array<idx_vector> ra_idx (n_idx, 1); |
9379
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1542 |
8682
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1543 for (octave_idx_type i = 0; i < n_idx; i++) |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1544 { |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1545 ra_idx(i) = idx(i).index_vector (); |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1546 if (error_state) |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1547 break; |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1548 } |
3933 | 1549 |
8682
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1550 if (! error_state) |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1551 { |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1552 for (const_iterator p = begin (); p != end (); p++) |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1553 { |
9379
d77fa87c47d8
oct-map.cc: preserve key order
John W. Eaton <jwe@octave.org>
parents:
9127
diff
changeset
|
1554 Cell tmp = contents (p); |
8682
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1555 |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9392
diff
changeset
|
1556 tmp = tmp.Array<octave_value>::index (ra_idx, resize_ok); |
3933 | 1557 |
8682
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1558 if (error_state) |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1559 break; |
8294
b2a6309b2d87
oct-map.cc: copy key_list in indexing functions
John W. Eaton <jwe@octave.org>
parents:
8175
diff
changeset
|
1560 |
8682
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1561 retval.assign (key(p), tmp); |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1562 } |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1563 |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1564 // Preserve order of keys. |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1565 retval.key_list = key_list; |
38968f09f7ca
optimize Octave_map::index
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
1566 } |
3933 | 1567 } |
5435 | 1568 else |
5539 | 1569 retval = *this; |
3933 | 1570 |
5539 | 1571 return retval; |
3933 | 1572 } |