annotate main/general/inst/@dict/get.m @ 9687:9df0cf7217ae octave-forge

general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
author carandraug
date Tue, 13 Mar 2012 22:39:03 +0000
parents ee31d2bacd42
children 52bed4508230
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5910
e349f5586eac add the dict class
highegg
parents:
diff changeset
1 ## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic
e349f5586eac add the dict class
highegg
parents:
diff changeset
2 ##
9687
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
3 ## This program is free software; you can redistribute it and/or modify it under
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
4 ## the terms of the GNU General Public License as published by the Free Software
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
5 ## Foundation; either version 3 of the License, or (at your option) any later
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
6 ## version.
5910
e349f5586eac add the dict class
highegg
parents:
diff changeset
7 ##
9687
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
11 ## details.
5910
e349f5586eac add the dict class
highegg
parents:
diff changeset
12 ##
9687
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
13 ## You should have received a copy of the GNU General Public License along with
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
5910
e349f5586eac add the dict class
highegg
parents:
diff changeset
15
e349f5586eac add the dict class
highegg
parents:
diff changeset
16 ## -*- texinfo -*-
e349f5586eac add the dict class
highegg
parents:
diff changeset
17 ## @deftypefn{Function File} {} get (d, key, defv)
e349f5586eac add the dict class
highegg
parents:
diff changeset
18 ## Queries for the values of specified key(s). Unlike indexing, however,
e349f5586eac add the dict class
highegg
parents:
diff changeset
19 ## this does not throw an error if a key is missing but rather substitutes
e349f5586eac add the dict class
highegg
parents:
diff changeset
20 ## a default value. If @var{key} is a cell array, @var{defv} should be either
e349f5586eac add the dict class
highegg
parents:
diff changeset
21 ## a cell array of the same shape as @var{key}, or a singleton cell.
e349f5586eac add the dict class
highegg
parents:
diff changeset
22 ## Non-cell values will be converted to a singleton cell.
e349f5586eac add the dict class
highegg
parents:
diff changeset
23 ## @end deftypefn
e349f5586eac add the dict class
highegg
parents:
diff changeset
24
9687
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
25 ## Author: Jaroslav Hajek <highegg@gmail.com>
9df0cf7217ae general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents: 6660
diff changeset
26
5910
e349f5586eac add the dict class
highegg
parents:
diff changeset
27 function val = get (d, key, defv = [])
e349f5586eac add the dict class
highegg
parents:
diff changeset
28 if (nargin < 2 || nargin > 3)
e349f5586eac add the dict class
highegg
parents:
diff changeset
29 print_usage ();
e349f5586eac add the dict class
highegg
parents:
diff changeset
30 endif
e349f5586eac add the dict class
highegg
parents:
diff changeset
31
6077
8b0e4cf0906e make general/@dict compatible with 3.2.x
highegg
parents: 5910
diff changeset
32 lookup = __lookup_compat__; # FIXME: remove when 3.3.x is required.
8b0e4cf0906e make general/@dict compatible with 3.2.x
highegg
parents: 5910
diff changeset
33
5910
e349f5586eac add the dict class
highegg
parents:
diff changeset
34 if (ischar (key))
e349f5586eac add the dict class
highegg
parents:
diff changeset
35 i = lookup (d.keys, key, "m");
e349f5586eac add the dict class
highegg
parents:
diff changeset
36 if (i)
e349f5586eac add the dict class
highegg
parents:
diff changeset
37 val = d.values{i};
e349f5586eac add the dict class
highegg
parents:
diff changeset
38 else
e349f5586eac add the dict class
highegg
parents:
diff changeset
39 val = defv;
e349f5586eac add the dict class
highegg
parents:
diff changeset
40 endif
e349f5586eac add the dict class
highegg
parents:
diff changeset
41 elseif (iscellstr (key))
6580
79fac0a4ee57 more fixes for dict
highegg
parents: 6105
diff changeset
42 if (! iscell (defv))
79fac0a4ee57 more fixes for dict
highegg
parents: 6105
diff changeset
43 val = repmat ({defv}, size (key));
79fac0a4ee57 more fixes for dict
highegg
parents: 6105
diff changeset
44 elseif (numel (defv) == 1)
79fac0a4ee57 more fixes for dict
highegg
parents: 6105
diff changeset
45 val = repmat (defv, size (key));
6660
ee31d2bacd42 check dimensions in @dict/get
highegg
parents: 6580
diff changeset
46 elseif (size_equal (key, defv))
ee31d2bacd42 check dimensions in @dict/get
highegg
parents: 6580
diff changeset
47 val = defv;
6580
79fac0a4ee57 more fixes for dict
highegg
parents: 6105
diff changeset
48 else
6660
ee31d2bacd42 check dimensions in @dict/get
highegg
parents: 6580
diff changeset
49 error ("get: sizes of key & defv must match");
6580
79fac0a4ee57 more fixes for dict
highegg
parents: 6105
diff changeset
50 endif
5910
e349f5586eac add the dict class
highegg
parents:
diff changeset
51 i = lookup (d.keys, key, "m");
e349f5586eac add the dict class
highegg
parents:
diff changeset
52 mask = i != 0;
6580
79fac0a4ee57 more fixes for dict
highegg
parents: 6105
diff changeset
53 val(mask) = d.values(i(mask));
5910
e349f5586eac add the dict class
highegg
parents:
diff changeset
54 else
6580
79fac0a4ee57 more fixes for dict
highegg
parents: 6105
diff changeset
55 error ("get: invalid key value");
5910
e349f5586eac add the dict class
highegg
parents:
diff changeset
56 endif
e349f5586eac add the dict class
highegg
parents:
diff changeset
57 endfunction
e349f5586eac add the dict class
highegg
parents:
diff changeset
58