]> Creatis software - CreaPhase.git/blob - octave_packages/general-1.3.1/@dict/subsref.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / general-1.3.1 / @dict / subsref.m
1 ## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn{Function File} {d =} subsref (d, s)
18 ## Overloaded subsref for dictionaries.
19 ## @end deftypefn
20
21 ## Author: Jaroslav Hajek <highegg@gmail.com>
22
23 function varargout = subsref (d, s)
24   if (isempty (s))
25     error ("dict: missing index");
26   endif
27
28   switch (s(1).type)
29     case "()"
30       ind = s(1).subs;
31       if (numel (ind) == 1)
32         ind = ind{1};
33       else
34         error ("dict: needs exactly one index");
35       endif
36       if (ischar (ind))
37         i = lookup (d.keys, ind, "m");
38         if (i)
39           e = d.values {i};
40         else
41           error ("key does not exist: %s", ind);
42         endif
43       elseif (iscellstr (ind))
44         i = lookup (d.keys, ind, "m");
45         if (all (i(:)))
46           e = reshape (d.values (i), size (ind)); # ensure correct shape.
47         else
48           ## Report the first non-existing key.
49           error ("key does not exist: %s", ind{find (i == 0, 1)});
50         endif
51       else
52         error ("invalid index");
53       endif
54     case "."
55       fld = s.subs;
56       switch (fld)
57       case 'keys'
58         e = d.keys;
59       case 'values'
60         e = d.values;
61       otherwise
62         error ("@dict/subsref: invalid property \"%s\"", fld);
63       endswitch
64     otherwise
65       error ("invalid subscript type");
66   endswitch
67
68   if (numel (s) > 1)
69     varargout = {subsref(e, s(2:end))};
70   else
71     varargout = {e};
72   endif
73 endfunction