GNU Octave  4.0.0
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-2015 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 <algorithm>
28 #include <map>
29 
30 #include "Cell.h"
31 #include "oct-obj.h"
32 
33 class string_vector;
34 
35 // A class holding a map field->index. Supports reference-counting.
38 {
39  class fields_rep : public std::map<std::string, octave_idx_type>
40  {
41  public:
42  fields_rep (void) : std::map<std::string, octave_idx_type> (), count (1) { }
43  fields_rep (const fields_rep& other)
44  : std::map<std::string, octave_idx_type> (other), count (1) { }
45 
47 
48  private:
49  fields_rep& operator = (const fields_rep&); // no assignment!
50  };
51 
53 
54  static fields_rep *nil_rep (void)
55  {
56  static fields_rep nr;
57  return &nr;
58  }
59 
60 public:
61 
62  octave_fields (void) : rep (nil_rep ()) { rep->count++; }
64  octave_fields (const char * const *);
65 
67  {
68  if (--rep->count == 0)
69  delete rep;
70  }
71 
72  void make_unique (void)
73  {
74  if (rep->count > 1)
75  {
76  fields_rep *r = new fields_rep (*rep);
77 
78  if (--rep->count == 0)
79  delete rep;
80 
81  rep = r;
82  }
83  }
84 
85  octave_fields (const octave_fields& o) : rep (o.rep) { rep->count++; }
86 
88  operator = (const octave_fields& o)
89  {
90  o.rep->count++;
91  if (--rep->count == 0)
92  delete rep;
93  rep = o.rep;
94 
95  return *this;
96  }
97 
98  // constant iteration support. non-const iteration intentionally unsupported.
99 
100  typedef std::map<std::string, octave_idx_type>::const_iterator const_iterator;
101  typedef const_iterator iterator;
102 
103  const_iterator begin (void) const { return rep->begin (); }
104  const_iterator end (void) const { return rep->end (); }
105 
106  std::string key (const_iterator p) const { return p->first; }
107  octave_idx_type index (const_iterator p) const { return p->second; }
108 
109  const_iterator seek (const std::string& k) const
110  { return rep->find (k); }
111 
112  // high-level methods.
113 
114  // number of fields.
115  octave_idx_type nfields (void) const { return rep->size (); }
116 
117  // check whether a field exists.
118  bool isfield (const std::string& name) const;
119 
120  // get index of field. return -1 if not exist
121  octave_idx_type getfield (const std::string& name) const;
122  // get index of field. add if not exist
123  octave_idx_type getfield (const std::string& name);
124  // remove field and return the index. -1 if didn't exist.
125  octave_idx_type rmfield (const std::string& name);
126 
127  // order the fields of this map. creates a permutation
128  // used to order the fields.
129  void orderfields (Array<octave_idx_type>& perm);
130 
131  // compares two instances for equality up to order of fields.
132  // returns a permutation needed to bring the fields of *other*
133  // into the order of *this*.
134  bool equal_up_to_order (const octave_fields& other,
135  octave_idx_type* perm) const;
136 
137  bool equal_up_to_order (const octave_fields& other,
138  Array<octave_idx_type>& perm) const;
139 
140  bool is_same (const octave_fields& other) const
141  { return rep == other.rep; }
142 
143  // Returns the fields as a vector of strings.
144  string_vector fieldnames (void) const;
145 
146  void clear (void)
147  {
148  *this = octave_fields ();
149  }
150 };
151 
152 
155 {
156 public:
157 
159  : xkeys (k), xvals (k.nfields ()) { }
160 
161  octave_scalar_map (void) : xkeys (), xvals () { }
162 
164  : xkeys (k), xvals (k.length ()) { }
165 
167  : xkeys (m.xkeys), xvals(m.xvals) { }
168 
169  octave_scalar_map& operator = (const octave_scalar_map& m)
170  {
171  xkeys = m.xkeys;
172  xvals = m.xvals;
173 
174  return *this;
175  }
176 
177  // iteration support. note that both const and non-const iterators are the
178  // same. The const/non-const distinction is made by the key & contents method.
180  typedef const_iterator iterator;
181 
182  const_iterator begin (void) const { return xkeys.begin (); }
183  const_iterator end (void) const { return xkeys.end (); }
184 
185  const_iterator seek (const std::string& k) const { return xkeys.seek (k); }
186 
187  std::string key (const_iterator p) const
188  { return xkeys.key (p); }
189  octave_idx_type index (const_iterator p) const
190  { return xkeys.index (p); }
191 
192  const octave_value& contents (const_iterator p) const
193  { return xvals[xkeys.index (p)]; }
194 
195  octave_value& contents (iterator p)
196  { return xvals[xkeys.index (p)]; }
197 
199  { return xvals[i]; }
200 
202  { return xvals[i]; }
203 
204  // number of fields.
205  octave_idx_type nfields (void) const { return xkeys.nfields (); }
206 
207  // check whether a field exists.
208  bool isfield (const std::string& name) const
209  { return xkeys.isfield (name); }
210 
211  bool contains (const std::string& name) const
212  { return isfield (name); }
213 
215  { return xkeys.fieldnames (); }
216 
217  string_vector keys (void) const
218  { return fieldnames (); }
219 
220  // get contents of a given field. empty value if not exist.
221  octave_value getfield (const std::string& key) const;
222 
223  // set contents of a given field. add if not exist.
224  void setfield (const std::string& key, const octave_value& val);
225  void assign (const std::string& k, const octave_value& val)
226  { setfield (k, val); }
227 
228  // remove a given field. do nothing if not exist.
229  void rmfield (const std::string& key);
230  void del (const std::string& k) { rmfield (k); }
231 
232  // return a copy with fields ordered, optionally along with permutation.
233  octave_scalar_map orderfields (void) const;
234  octave_scalar_map orderfields (Array<octave_idx_type>& perm) const;
235  octave_scalar_map orderfields (const octave_scalar_map& other,
236  Array<octave_idx_type>& perm) const;
237 
238  // aka getfield/setfield, but the latter returns a reference.
239  octave_value contents (const std::string& k) const;
240  octave_value& contents (const std::string& k);
241 
242  void clear (void)
243  {
244  xkeys.clear ();
245  xvals.clear ();
246  }
247 
248  friend class octave_map;
249 
250 private:
251 
253  std::vector<octave_value> xvals;
254 
255 };
256 
257 template<>
258 inline octave_scalar_map
260 { return v.scalar_map_value (); }
261 
264 {
265 public:
266 
268  : xkeys (k), xvals (k.nfields ()), dimensions () { }
269 
270  octave_map (const dim_vector& dv, const octave_fields& k)
271  : xkeys (k), xvals (k.nfields (), Cell (dv)), dimensions (dv) { }
272 
274 
275  octave_map (void) : xkeys (), xvals (), dimensions () { }
276 
277  octave_map (const dim_vector& dv) : xkeys (), xvals (), dimensions (dv) { }
278 
280  : xkeys (k), xvals (k.length (), Cell (1, 1)), dimensions (1, 1) { }
281 
282  octave_map (const dim_vector& dv, const string_vector& k)
283  : xkeys (k), xvals (k.length (), Cell (dv)), dimensions (dv) { }
284 
286  : xkeys (m.xkeys), xvals (m.xvals), dimensions (m.dimensions) { }
287 
288  octave_map (const octave_scalar_map& m);
289 
290  octave_map& operator = (const octave_map& m)
291  {
292  xkeys = m.xkeys;
293  xvals = m.xvals;
294  dimensions = m.dimensions;
295 
296  return *this;
297  }
298 
299  // iteration support. note that both const and non-const iterators are the
300  // same. The const/non-const distinction is made by the key & contents method.
302  typedef const_iterator iterator;
303 
304  const_iterator begin (void) const { return xkeys.begin (); }
305  const_iterator end (void) const { return xkeys.end (); }
306 
307  const_iterator seek (const std::string& k) const { return xkeys.seek (k); }
308 
309  std::string key (const_iterator p) const
310  { return xkeys.key (p); }
311  octave_idx_type index (const_iterator p) const
312  { return xkeys.index (p); }
313 
314  const Cell& contents (const_iterator p) const
315  { return xvals[xkeys.index (p)]; }
316 
317  Cell& contents (iterator p)
318  { return xvals[xkeys.index (p)]; }
319 
320  const Cell& contents (octave_idx_type i) const
321  { return xvals[i]; }
322 
324  { return xvals[i]; }
325 
326  // number of fields.
327  octave_idx_type nfields (void) const { return xkeys.nfields (); }
328 
329  // check whether a field exists.
330  bool isfield (const std::string& name) const
331  { return xkeys.isfield (name); }
332 
333  bool contains (const std::string& name) const
334  { return isfield (name); }
335 
337  { return xkeys.fieldnames (); }
338 
339  string_vector keys (void) const
340  { return fieldnames (); }
341 
342  // get contents of a given field. empty value if not exist.
343  Cell getfield (const std::string& key) const;
344 
345  // set contents of a given field. add if not exist. checks for
346  // correct dimensions.
347  void setfield (const std::string& key, const Cell& val);
348  void assign (const std::string& k, const Cell& val)
349  { setfield (k, val); }
350 
351  // remove a given field. do nothing if not exist.
352  void rmfield (const std::string& key);
353  void del (const std::string& k) { rmfield (k); }
354 
355  // return a copy with fields ordered, optionally along with permutation.
356  octave_map orderfields (void) const;
357  octave_map orderfields (Array<octave_idx_type>& perm) const;
358  octave_map orderfields (const octave_map& other,
359  Array<octave_idx_type>& perm) const;
360 
361  // aka getfield/setfield, but the latter returns a reference.
362  Cell contents (const std::string& k) const;
363  Cell& contents (const std::string& k);
364 
365  void clear (void)
366  {
367  xkeys.clear ();
368  xvals.clear ();
369  }
370 
371  // The Array-like methods.
372  octave_idx_type numel (void) const { return dimensions.numel (); }
373  octave_idx_type length (void) const { return numel (); }
374  bool is_empty (void) const { return dimensions.any_zero (); }
375 
376  octave_idx_type rows (void) const { return dimensions(0); }
377  octave_idx_type cols (void) const { return dimensions(1); }
378  octave_idx_type columns (void) const { return dimensions(1); }
379 
380  // Extract a scalar substructure.
381  octave_scalar_map checkelem (octave_idx_type n) const;
382  octave_scalar_map checkelem (octave_idx_type i, octave_idx_type j) const;
383 
385  checkelem (const Array<octave_idx_type>& ra_idx) const;
386 
387  octave_scalar_map operator () (octave_idx_type n) const
388  { return checkelem (n); }
390  { return checkelem (i, j); }
391 
393  operator () (const Array<octave_idx_type>& ra_idx) const
394  { return checkelem (ra_idx); }
395 
396  octave_map squeeze (void) const;
397 
398  octave_map permute (const Array<int>& vec, bool inv = false) const;
399 
400  dim_vector dims (void) const { return dimensions; }
401 
402  int ndims (void) const { return dimensions.length (); }
403 
404  octave_map transpose (void) const;
405 
406  octave_map reshape (const dim_vector& dv) const;
407 
408  void resize (const dim_vector& dv, bool fill = false);
409 
410  static octave_map
411  cat (int dim, octave_idx_type n, const octave_scalar_map *map_list);
412 
413  static octave_map
414  cat (int dim, octave_idx_type n, const octave_map *map_list);
415 
416  octave_map index (const idx_vector& i, bool resize_ok = false) const;
417 
418  octave_map index (const idx_vector& i, const idx_vector& j,
419  bool resize_ok = false) const;
420 
421  octave_map index (const Array<idx_vector>& ia,
422  bool resize_ok = false) const;
423 
424  octave_map index (const octave_value_list&, bool resize_ok = false) const;
425 
426  octave_map column (octave_idx_type k) const;
427  octave_map page (octave_idx_type k) const;
428 
429  void assign (const idx_vector& i, const octave_map& rhs);
430 
431  void assign (const idx_vector& i, const idx_vector& j, const octave_map& rhs);
432 
433  void assign (const Array<idx_vector>& ia, const octave_map& rhs);
434 
435  void assign (const octave_value_list&, const octave_map& rhs);
436 
437  void assign (const octave_value_list& idx, const std::string& k,
438  const Cell& rhs);
439 
440  void delete_elements (const idx_vector& i);
441 
442  void delete_elements (int dim, const idx_vector& i);
443 
444  void delete_elements (const Array<idx_vector>& ia);
445 
446  void delete_elements (const octave_value_list&);
447 
448  octave_map concat (const octave_map& rb,
450 
451  // like checkelem, but no check.
452  octave_scalar_map fast_elem_extract (octave_idx_type n) const;
453 
454  // element assignment, no bounds check
455  bool fast_elem_insert (octave_idx_type n, const octave_scalar_map& rhs);
456 
457 private:
458 
460  std::vector<Cell> xvals;
462 
463  void optimize_dimensions (void);
464  void extract_scalar (octave_scalar_map& dest,
465  octave_idx_type index) const;
466  static void do_cat (int dim, octave_idx_type n,
467  const octave_scalar_map *map_list, octave_map& retval);
468  static void do_cat (int dim, octave_idx_type n,
469  const octave_map *map_list, octave_map& retval);
470 };
471 
472 template<>
474 { return v.map_value (); }
475 
476 #endif
const_iterator seek(const std::string &k) const
Definition: oct-map.h:109
string_vector keys(void) const
Definition: oct-map.h:339
const Cell & contents(const_iterator p) const
Definition: oct-map.h:314
dim_vector dimensions
Definition: oct-map.h:461
octave_value & contents(iterator p)
Definition: oct-map.h:195
octave_value & contents(octave_idx_type i)
Definition: oct-map.h:201
octave_scalar_map element_type
Definition: oct-map.h:273
bool isfield(const std::string &name) const
Definition: oct-map.h:208
octave_map(const dim_vector &dv)
Definition: oct-map.h:277
Definition: Cell.h:35
octave_idx_type nfields(void) const
Definition: oct-map.h:205
Cell & contents(octave_idx_type i)
Definition: oct-map.h:323
string_vector keys(void) const
Definition: oct-map.h:217
const octave_base_value const Array< octave_idx_type > & ra_idx
Cell & contents(iterator p)
Definition: oct-map.h:317
void assign(const std::string &k, const Cell &val)
Definition: oct-map.h:348
octave_map(const string_vector &k)
Definition: oct-map.h:279
octave_refcount< int > count
Definition: oct-map.h:46
const octave_value & contents(const_iterator p) const
Definition: oct-map.h:192
const_iterator iterator
Definition: oct-map.h:302
bool is_same(const octave_fields &other) const
Definition: oct-map.h:140
bool contains(const std::string &name) const
Definition: oct-map.h:333
bool contains(const std::string &name) const
Definition: oct-map.h:211
octave_idx_type cols(void) const
Definition: oct-map.h:377
const Cell & contents(octave_idx_type i) const
Definition: oct-map.h:320
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:664
string_vector fieldnames(void) const
Definition: oct-map.h:214
STL namespace.
octave_map(const octave_fields &k)
Definition: oct-map.h:267
const_iterator end(void) const
Definition: oct-map.h:104
octave_map(const octave_map &m)
Definition: oct-map.h:285
octave_idx_type index(const_iterator p) const
Definition: oct-map.h:189
octave_fields(void)
Definition: oct-map.h:62
octave_idx_type numel(void) const
Definition: oct-map.h:372
const octave_value & contents(octave_idx_type i) const
Definition: oct-map.h:198
octave_scalar_map(const octave_scalar_map &m)
Definition: oct-map.h:166
std::vector< Cell > xvals
Definition: oct-map.h:460
octave_scalar_map octave_value_extract< octave_scalar_map >(const octave_value &v)
Definition: oct-map.h:259
octave_idx_type index(const_iterator p) const
Definition: oct-map.h:107
const_iterator end(void) const
Definition: oct-map.h:305
octave_fields xkeys
Definition: oct-map.h:252
void clear(void)
Definition: oct-map.h:146
std::vector< octave_value > xvals
Definition: oct-map.h:253
octave_map(const dim_vector &dv, const string_vector &k)
Definition: oct-map.h:282
void clear(void)
Definition: oct-map.h:365
octave_fields::const_iterator const_iterator
Definition: oct-map.h:179
octave_fields xkeys
Definition: oct-map.h:459
#define OCTINTERP_API
Definition: mexproto.h:66
const_iterator begin(void) const
Definition: oct-map.h:182
std::string key(const_iterator p) const
Definition: oct-map.h:187
bool isfield(const std::string &name) const
Definition: oct-map.h:330
octave_idx_type nfields(void) const
Definition: oct-map.h:327
std::string key(const_iterator p) const
Definition: oct-map.h:106
octave_map(const dim_vector &dv, const octave_fields &k)
Definition: oct-map.h:270
dim_vector dims(void) const
Definition: oct-map.h:400
const_iterator end(void) const
Definition: oct-map.h:183
static octave_value do_cat(const octave_value_list &xargs, int dim, std::string fname)
Definition: data.cc:1914
std::string key(const_iterator p) const
Definition: oct-map.h:309
fields_rep * rep
Definition: oct-map.h:52
const_iterator begin(void) const
Definition: oct-map.h:304
octave_map(void)
Definition: oct-map.h:275
void make_unique(void)
Definition: oct-map.h:72
octave_idx_type nfields(void) const
Definition: oct-map.h:115
octave_fields(const octave_fields &o)
Definition: oct-map.h:85
octave_scalar_map(const octave_fields &k)
Definition: oct-map.h:158
~octave_fields(void)
Definition: oct-map.h:66
void del(const std::string &k)
Definition: oct-map.h:230
int ndims(void) const
Definition: oct-map.h:402
octave_map octave_value_extract< octave_map >(const octave_value &v)
Definition: oct-map.h:473
const_iterator iterator
Definition: oct-map.h:101
octave_idx_type rows(void) const
Definition: oct-map.h:376
std::map< std::string, octave_idx_type >::const_iterator const_iterator
Definition: oct-map.h:100
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:225
const_iterator begin(void) const
Definition: oct-map.h:103
static fields_rep * nil_rep(void)
Definition: oct-map.h:54
const_iterator seek(const std::string &k) const
Definition: oct-map.h:307
octave_idx_type length(void) const
Definition: oct-map.h:373
octave_idx_type columns(void) const
Definition: oct-map.h:378
fields_rep(const fields_rep &other)
Definition: oct-map.h:43
bool is_empty(void) const
Definition: oct-map.h:374
octave_idx_type index(const_iterator p) const
Definition: oct-map.h:311
octave_fields::const_iterator const_iterator
Definition: oct-map.h:301
const_iterator seek(const std::string &k) const
Definition: oct-map.h:185
octave_scalar_map(const string_vector &k)
Definition: oct-map.h:163
const_iterator iterator
Definition: oct-map.h:180
string_vector fieldnames(void) const
Definition: oct-map.h:336
void del(const std::string &k)
Definition: oct-map.h:353
octave_scalar_map(void)
Definition: oct-map.h:161
void clear(void)
Definition: oct-map.h:242