GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-map.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2017 John W. Eaton
4 Copyright (C) 2010 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if ! defined (octave_oct_map_h)
25 #define octave_oct_map_h 1
26 
27 #include "octave-config.h"
28 
29 #include <algorithm>
30 #include <map>
31 
32 #include "Cell.h"
33 #include "ovl.h"
34 
35 class string_vector;
36 
37 // A class holding a map field->index. Supports reference-counting.
40 {
41  class fields_rep : public std::map<std::string, octave_idx_type>
42  {
43  public:
44  fields_rep (void) : std::map<std::string, octave_idx_type> (), count (1) { }
45  fields_rep (const fields_rep& other)
46  : std::map<std::string, octave_idx_type> (other), count (1) { }
47 
49 
50  private:
51  fields_rep& operator = (const fields_rep&); // no assignment!
52  };
53 
55 
56  static fields_rep *nil_rep (void);
57 
58 public:
59 
60  octave_fields (void) : rep (nil_rep ()) { rep->count++; }
62  octave_fields (const char * const *);
63 
65  {
66  if (--rep->count == 0)
67  delete rep;
68  }
69 
70  void make_unique (void)
71  {
72  if (rep->count > 1)
73  {
74  fields_rep *r = new fields_rep (*rep);
75 
76  if (--rep->count == 0)
77  delete rep;
78 
79  rep = r;
80  }
81  }
82 
83  octave_fields (const octave_fields& o) : rep (o.rep) { rep->count++; }
84 
86  operator = (const octave_fields& o)
87  {
88  o.rep->count++;
89  if (--rep->count == 0)
90  delete rep;
91  rep = o.rep;
92 
93  return *this;
94  }
95 
96  // constant iteration support. non-const iteration intentionally unsupported.
97 
98  typedef std::map<std::string, octave_idx_type>::const_iterator const_iterator;
99  typedef const_iterator iterator;
100 
101  const_iterator begin (void) const { return rep->begin (); }
102  const_iterator end (void) const { return rep->end (); }
103 
104  std::string key (const_iterator p) const { return p->first; }
105  octave_idx_type index (const_iterator p) const { return p->second; }
106 
107  const_iterator seek (const std::string& k) const
108  { return rep->find (k); }
109 
110  // high-level methods.
111 
112  // number of fields.
113  octave_idx_type nfields (void) const { return rep->size (); }
114 
115  // check whether a field exists.
116  bool isfield (const std::string& name) const;
117 
118  // get index of field. return -1 if not exist
119  octave_idx_type getfield (const std::string& name) const;
120  // get index of field. add if not exist
122  // remove field and return the index. -1 if didn't exist.
124 
125  // order the fields of this map.
126  // creates a permutation used to order the fields.
127  void orderfields (Array<octave_idx_type>& perm);
128 
129  // compares two instances for equality up to order of fields.
130  // returns a permutation needed to bring the fields of *other*
131  // into the order of *this*.
132  bool equal_up_to_order (const octave_fields& other,
133  octave_idx_type* perm) const;
134 
135  bool equal_up_to_order (const octave_fields& other,
136  Array<octave_idx_type>& perm) const;
137 
138  bool is_same (const octave_fields& other) const
139  { return rep == other.rep; }
140 
141  // Returns the fields as a vector of strings.
142  string_vector fieldnames (void) const;
143 
144  void clear (void)
145  {
146  *this = octave_fields ();
147  }
148 };
149 
152 {
153 public:
154 
156  : xkeys (k), xvals (k.nfields ()) { }
157 
158  octave_scalar_map (void) : xkeys (), xvals () { }
159 
161  : xkeys (k), xvals (k.numel ()) { }
162 
164  : xkeys (m.xkeys), xvals(m.xvals) { }
165 
166  octave_scalar_map& operator = (const octave_scalar_map& m)
167  {
168  xkeys = m.xkeys;
169  xvals = m.xvals;
170 
171  return *this;
172  }
173 
174  // iteration support.
175  // note that both const and non-const iterators are the same.
176  // The const/non-const distinction is made by the key & contents method.
178  typedef const_iterator iterator;
179 
180  const_iterator begin (void) const { return xkeys.begin (); }
181  const_iterator end (void) const { return xkeys.end (); }
182 
183  const_iterator seek (const std::string& k) const { return xkeys.seek (k); }
184 
185  std::string key (const_iterator p) const
186  { return xkeys.key (p); }
187  octave_idx_type index (const_iterator p) const
188  { return xkeys.index (p); }
189 
190  const octave_value& contents (const_iterator p) const
191  { return xvals[xkeys.index (p)]; }
192 
193  octave_value& contents (iterator p)
194  { return xvals[xkeys.index (p)]; }
195 
197  { return xvals[i]; }
198 
200  { return xvals[i]; }
201 
202  // number of fields.
203  octave_idx_type nfields (void) const { return xkeys.nfields (); }
204 
205  // check whether a field exists.
206  bool isfield (const std::string& name) const
207  { return xkeys.isfield (name); }
208 
209  bool contains (const std::string& name) const
210  { return isfield (name); }
211 
213  { return xkeys.fieldnames (); }
214 
215  string_vector keys (void) const
216  { return fieldnames (); }
217 
218  // get contents of a given field. empty value if not exist.
219  octave_value getfield (const std::string& key) const;
220 
221  // set contents of a given field. add if not exist.
222  void setfield (const std::string& key, const octave_value& val);
223  void assign (const std::string& k, const octave_value& val)
224  { setfield (k, val); }
225 
226  // remove a given field. do nothing if not exist.
227  void rmfield (const std::string& key);
228  void del (const std::string& k) { rmfield (k); }
229 
230  // return a copy with fields ordered, optionally along with permutation.
231  octave_scalar_map orderfields (void) const;
234  Array<octave_idx_type>& perm) const;
235 
236  // aka getfield/setfield, but the latter returns a reference.
237  octave_value contents (const std::string& k) const;
238  octave_value& contents (const std::string& k);
239 
240  void clear (void)
241  {
242  xkeys.clear ();
243  xvals.clear ();
244  }
245 
246  friend class octave_map;
247 
248 private:
249 
251  std::vector<octave_value> xvals;
252 
253 };
254 
255 template <>
256 inline octave_scalar_map
258 { return v.scalar_map_value (); }
259 
262 {
263 public:
264 
266  : xkeys (k), xvals (k.nfields ()), dimensions () { }
267 
269  : xkeys (k), xvals (k.nfields (), Cell (dv)), dimensions (dv) { }
270 
272 
273  octave_map (void) : xkeys (), xvals (), dimensions () { }
274 
275  octave_map (const dim_vector& dv) : xkeys (), xvals (), dimensions (dv) { }
276 
278  : xkeys (k), xvals (k.numel (), Cell (1, 1)), dimensions (1, 1) { }
279 
281  : xkeys (k), xvals (k.numel (), Cell (dv)), dimensions (dv) { }
282 
284  : xkeys (m.xkeys), xvals (m.xvals), dimensions (m.dimensions) { }
285 
286  octave_map (const octave_scalar_map& m);
287 
288  octave_map& operator = (const octave_map& m)
289  {
290  xkeys = m.xkeys;
291  xvals = m.xvals;
292  dimensions = m.dimensions;
293 
294  return *this;
295  }
296 
297  // iteration support.
298  // note that both const and non-const iterators are the same.
299  // The const/non-const distinction is made by the key & contents method.
301  typedef const_iterator iterator;
302 
303  const_iterator begin (void) const { return xkeys.begin (); }
304  const_iterator end (void) const { return xkeys.end (); }
305 
306  const_iterator seek (const std::string& k) const { return xkeys.seek (k); }
307 
308  std::string key (const_iterator p) const
309  { return xkeys.key (p); }
310  octave_idx_type index (const_iterator p) const
311  { return xkeys.index (p); }
312 
313  const Cell& contents (const_iterator p) const
314  { return xvals[xkeys.index (p)]; }
315 
316  Cell& contents (iterator p)
317  { return xvals[xkeys.index (p)]; }
318 
320  { return xvals[i]; }
321 
323  { return xvals[i]; }
324 
325  // number of fields.
326  octave_idx_type nfields (void) const { return xkeys.nfields (); }
327 
328  // check whether a field exists.
329  bool isfield (const std::string& name) const
330  { return xkeys.isfield (name); }
331 
332  bool contains (const std::string& name) const
333  { return isfield (name); }
334 
336  { return xkeys.fieldnames (); }
337 
338  string_vector keys (void) const
339  { return fieldnames (); }
340 
341  // get contents of a given field. empty value if not exist.
342  Cell getfield (const std::string& key) const;
343 
344  // set contents of a given field. add if not exist. checks for
345  // correct dimensions.
346  void setfield (const std::string& key, const Cell& val);
347  void assign (const std::string& k, const Cell& val)
348  { setfield (k, val); }
349 
350  // remove a given field. do nothing if not exist.
351  void rmfield (const std::string& key);
352  void del (const std::string& k) { rmfield (k); }
353 
354  // return a copy with fields ordered, optionally along with permutation.
355  octave_map orderfields (void) const;
357  octave_map orderfields (const octave_map& other,
358  Array<octave_idx_type>& perm) const;
359 
360  // aka getfield/setfield, but the latter returns a reference.
361  Cell contents (const std::string& k) const;
362  Cell& contents (const std::string& k);
363 
364  void clear (void)
365  {
366  xkeys.clear ();
367  xvals.clear ();
368  }
369 
370  // The Array-like methods.
371  octave_idx_type numel (void) const { return dimensions.numel (); }
372  octave_idx_type length (void) const { return numel (); }
373  bool is_empty (void) const { return dimensions.any_zero (); }
374 
375  octave_idx_type rows (void) const { return dimensions(0); }
376  octave_idx_type cols (void) const { return dimensions(1); }
377  octave_idx_type columns (void) const { return dimensions(1); }
378 
379  // Extract a scalar substructure.
380  octave_scalar_map checkelem (octave_idx_type n) const;
381  octave_scalar_map checkelem (octave_idx_type i, octave_idx_type j) const;
382 
384  checkelem (const Array<octave_idx_type>& ra_idx) const;
385 
386  octave_scalar_map operator () (octave_idx_type n) const
387  { return checkelem (n); }
389  { return checkelem (i, j); }
390 
392  operator () (const Array<octave_idx_type>& ra_idx) const
393  { return checkelem (ra_idx); }
394 
395  octave_map squeeze (void) const;
396 
397  octave_map permute (const Array<int>& vec, bool inv = false) const;
398 
399  dim_vector dims (void) const { return dimensions; }
400 
401  int ndims (void) const { return dimensions.ndims (); }
402 
403  octave_map transpose (void) const;
404 
405  octave_map reshape (const dim_vector& dv) const;
406 
407  void resize (const dim_vector& dv, bool fill = false);
408 
409  static octave_map
410  cat (int dim, octave_idx_type n, const octave_scalar_map *map_list);
411 
412  static octave_map
413  cat (int dim, octave_idx_type n, const octave_map *map_list);
414 
415  octave_map index (const idx_vector& i, bool resize_ok = false) const;
416 
417  octave_map index (const idx_vector& i, const idx_vector& j,
418  bool resize_ok = false) const;
419 
420  octave_map index (const Array<idx_vector>& ia,
421  bool resize_ok = false) const;
422 
423  octave_map index (const octave_value_list&, bool resize_ok = false) const;
424 
427 
428  void assign (const idx_vector& i, const octave_map& rhs);
429 
430  void assign (const idx_vector& i, const idx_vector& j, const octave_map& rhs);
431 
432  void assign (const Array<idx_vector>& ia, const octave_map& rhs);
433 
434  void assign (const octave_value_list&, const octave_map& rhs);
435 
436  void assign (const octave_value_list& idx, const std::string& k,
437  const Cell& rhs);
438 
439  void delete_elements (const idx_vector& i);
440 
441  void delete_elements (int dim, const idx_vector& i);
442 
443  void delete_elements (const Array<idx_vector>& ia);
444 
445  void delete_elements (const octave_value_list&);
446 
447  octave_map concat (const octave_map& rb,
449 
450  // like checkelem, but no check.
451  octave_scalar_map fast_elem_extract (octave_idx_type n) const;
452 
453  // element assignment, no bounds check
454  bool fast_elem_insert (octave_idx_type n, const octave_scalar_map& rhs);
455 
456 private:
457 
459  std::vector<Cell> xvals;
461 
462  void optimize_dimensions (void);
463  void extract_scalar (octave_scalar_map& dest,
464  octave_idx_type index) const;
465  static void do_cat (int dim, octave_idx_type n,
466  const octave_scalar_map *map_list, octave_map& retval);
467  static void do_cat (int dim, octave_idx_type n,
468  const octave_map *map_list, octave_map& retval);
469 };
470 
471 template <>
473 { return v.map_value (); }
474 
475 #endif
const_iterator seek(const std::string &k) const
Definition: oct-map.h:107
string_vector keys(void) const
Definition: oct-map.h:338
const Cell & contents(const_iterator p) const
Definition: oct-map.h:313
dim_vector dimensions
Definition: oct-map.h:460
octave_value & contents(iterator p)
Definition: oct-map.h:193
octave_value & contents(octave_idx_type i)
Definition: oct-map.h:199
octave_scalar_map element_type
Definition: oct-map.h:271
bool isfield(const std::string &name) const
Definition: oct-map.h:206
octave_map(const dim_vector &dv)
Definition: oct-map.h:275
OCTAVE_EXPORT octave_value_list column
Definition: sparse.cc:123
Definition: Cell.h:37
octave_idx_type nfields(void) const
Definition: oct-map.h:203
Cell & contents(octave_idx_type i)
Definition: oct-map.h:322
string_vector keys(void) const
Definition: oct-map.h:215
const octave_base_value const Array< octave_idx_type > & ra_idx
Cell & contents(iterator p)
Definition: oct-map.h:316
void assign(const std::string &k, const Cell &val)
Definition: oct-map.h:347
octave_map(const string_vector &k)
Definition: oct-map.h:277
scalar structure containing the one value The second produces an empty struct array with one field and no values since being passed an empty cell array of struct array values When the value is a cell array containing a single entry this becomes a scalar struct with that single entry as the value of the field That single entry happens to be an empty cell array Finally if the value is a non scalar cell array then ode fieldnames
octave_refcount< int > count
Definition: oct-map.h:48
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
const octave_value & contents(const_iterator p) const
Definition: oct-map.h:190
const_iterator iterator
Definition: oct-map.h:301
bool is_same(const octave_fields &other) const
Definition: oct-map.h:138
bool contains(const std::string &name) const
Definition: oct-map.h:332
bool contains(const std::string &name) const
Definition: oct-map.h:209
for large enough k
Definition: lu.cc:606
OCTAVE_EXPORT octave_value_list page
Definition: sub2ind.cc:107
octave_idx_type cols(void) const
Definition: oct-map.h:376
const Cell & contents(octave_idx_type i) const
Definition: oct-map.h:319
static void transpose(octave_idx_type N, const octave_idx_type *ridx, const octave_idx_type *cidx, octave_idx_type *ridx2, octave_idx_type *cidx2)
Definition: symrcm.cc:382
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition: CNDArray.cc:655
cat(2, A, B) esult
Definition: data.cc:2417
string_vector fieldnames(void) const
Definition: oct-map.h:212
STL namespace.
octave_map(const octave_fields &k)
Definition: oct-map.h:265
const_iterator end(void) const
Definition: oct-map.h:102
octave_map(const octave_map &m)
Definition: oct-map.h:283
octave_idx_type index(const_iterator p) const
Definition: oct-map.h:187
octave_fields(void)
Definition: oct-map.h:60
octave_idx_type numel(void) const
Definition: oct-map.h:371
const octave_value & contents(octave_idx_type i) const
Definition: oct-map.h:196
octave_scalar_map(const octave_scalar_map &m)
Definition: oct-map.h:163
std::vector< Cell > xvals
Definition: oct-map.h:459
scalar structure containing the one value The second produces an empty struct array with one field and no values since being passed an empty cell array of struct array values When the value is a cell array containing a single entry this becomes a scalar struct with that single entry as the value of the field That single entry happens to be an empty cell array Finally if the value is a non scalar cell array then ode orderfields
octave_scalar_map octave_value_extract< octave_scalar_map >(const octave_value &v)
Definition: oct-map.h:257
scalar structure containing the one value The second produces an empty struct array with one field and no values since being passed an empty cell array of struct array values When the value is a cell array containing a single entry this becomes a scalar struct with that single entry as the value of the field That single entry happens to be an empty cell array Finally if the value is a non scalar cell array then ode rmfield
octave_idx_type index(const_iterator p) const
Definition: oct-map.h:105
const_iterator end(void) const
Definition: oct-map.h:304
octave_fields xkeys
Definition: oct-map.h:250
void clear(void)
Definition: oct-map.h:144
std::vector< octave_value > xvals
Definition: oct-map.h:251
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
octave_map(const dim_vector &dv, const string_vector &k)
Definition: oct-map.h:280
void clear(void)
Definition: oct-map.h:364
octave_fields::const_iterator const_iterator
Definition: oct-map.h:177
octave_fields xkeys
Definition: oct-map.h:458
#define OCTINTERP_API
Definition: mexproto.h:69
const_iterator begin(void) const
Definition: oct-map.h:180
std::string key(const_iterator p) const
Definition: oct-map.h:185
bool isfield(const std::string &name) const
Definition: oct-map.h:329
octave_idx_type nfields(void) const
Definition: oct-map.h:326
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
std::string key(const_iterator p) const
Definition: oct-map.h:104
octave_map(const dim_vector &dv, const octave_fields &k)
Definition: oct-map.h:268
dim_vector dims(void) const
Definition: oct-map.h:399
const_iterator end(void) const
Definition: oct-map.h:181
octave_value retval
Definition: data.cc:6294
std::string key(const_iterator p) const
Definition: oct-map.h:308
fields_rep * rep
Definition: oct-map.h:54
const_iterator begin(void) const
Definition: oct-map.h:303
octave_map(void)
Definition: oct-map.h:273
void make_unique(void)
Definition: oct-map.h:70
octave_idx_type nfields(void) const
Definition: oct-map.h:113
octave_fields(const octave_fields &o)
Definition: oct-map.h:83
octave_scalar_map(const octave_fields &k)
Definition: oct-map.h:155
T::size_type numel(const T &str)
Definition: oct-string.cc:61
~octave_fields(void)
Definition: oct-map.h:64
scalar structure containing the one value The second produces an empty struct array with one field and no values since being passed an empty cell array of struct array values When the value is a cell array containing a single entry this becomes a scalar struct with that single entry as the value of the field That single entry happens to be an empty cell array Finally if the value is a non scalar cell array then ode isfield
void del(const std::string &k)
Definition: oct-map.h:228
int ndims(void) const
Definition: oct-map.h:401
octave_map octave_value_extract< octave_map >(const octave_value &v)
Definition: oct-map.h:472
const_iterator iterator
Definition: oct-map.h:99
octave_idx_type rows(void) const
Definition: oct-map.h:375
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
std::map< std::string, octave_idx_type >::const_iterator const_iterator
Definition: oct-map.h:98
p
Definition: lu.cc:138
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:223
const_iterator begin(void) const
Definition: oct-map.h:101
octave_map map(dims)
scalar structure containing the one value The second produces an empty struct array with one field and no values since being passed an empty cell array of struct array values When the value is a cell array containing a single entry this becomes a scalar struct with that single entry as the value of the field That single entry happens to be an empty cell array Finally if the value is a non scalar cell array then ode setfield
scalar structure containing the one value The second produces an empty struct array with one field and no values since being passed an empty cell array of struct array values When the value is a cell array containing a single entry this becomes a scalar struct with that single entry as the value of the field That single entry happens to be an empty cell array Finally if the value is a non scalar cell array then ode getfield
const_iterator seek(const std::string &k) const
Definition: oct-map.h:306
octave_idx_type length(void) const
Definition: oct-map.h:372
octave_idx_type columns(void) const
Definition: oct-map.h:377
fields_rep(const fields_rep &other)
Definition: oct-map.h:45
bool is_empty(void) const
Definition: oct-map.h:373
octave_idx_type index(const_iterator p) const
Definition: oct-map.h:310
octave_fields::const_iterator const_iterator
Definition: oct-map.h:300
const_iterator seek(const std::string &k) const
Definition: oct-map.h:183
octave_scalar_map(const string_vector &k)
Definition: oct-map.h:160
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
const_iterator iterator
Definition: oct-map.h:178
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
string_vector fieldnames(void) const
Definition: oct-map.h:335
void del(const std::string &k)
Definition: oct-map.h:352
dim_vector dv
Definition: sub2ind.cc:263
octave_scalar_map(void)
Definition: oct-map.h:158
void clear(void)
Definition: oct-map.h:240