1 ## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic
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
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
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/>.
17 ## @deftypefn{Function File} {d =} dict (@var{keys}, @var{values})
18 ## @deftypefnx{Function File} {d =} dict ()
19 ## @deftypefnx{Function File} {d =} dict (@var{str})
20 ## Creates a dictionary object with given keys and values. @var{keys}
21 ## should be a cell array of strings; @var{values} should be a cell array
22 ## with matching size. @var{values} can also be a singleton array, in
23 ## which case it is expanded to the proper size; or omitted, in which case
24 ## the default value of empty matrix is used.
25 ## If neither @var{keys} nor @var{values} are supplied, an empty dictionary
27 ## If a scalar structure is supplied as an argument, it is converted to
28 ## a dictionary using field names as keys.
30 ## A dictionary can be indexed either by a single string or cell array of
31 ## strings, like this:
34 ## d = dict (keys, values);
35 ## d(str) # result is a single value
36 ## d(cellstr) # result is a cell array
39 ## In the first case, the stored value is returned directly; in the second case,
40 ## a cell array is returned. The cell array returned inherits the shape of the index.
42 ## Similarly, indexed assignment works like this:
45 ## d = dict (keys, values);
46 ## d(str) = val; # store a single value
47 ## d(cellstr) = vals; # store a cell array
48 ## d(cellstr) = []; # delete a range of keys
51 ## Any keys that are not present in the dictionary are added. The values of
52 ## existing keys are overwritten. In the second case, the lengths of index and
53 ## rhs should match or rhs should be a singleton array, in which case it is
56 ## It is also possible to retrieve keys and values as cell arrays, using the
57 ## "keys" and "values" properties. These properties are read-only.
61 ## Author: Jaroslav Hajek <highegg@gmail.com>
63 function d = dict (keys, values)
66 keys = values = cell (0, 1);
69 keys = sort (keys(:));
70 values = cell (numel (keys), 1);
71 elseif (isstruct (keys))
72 values = struct2cell (keys)(:,:);
73 if (columns (values) != 1)
74 error ("dict: structure must be a scalar");
76 [keys, ind] = sort (fieldnames (keys));
79 error ("dict: keys must be a cell vector of strings");
82 [keys, idx] = sort (keys(:));
83 values = values (idx)(:);
88 d = class (struct ("keys", {keys}, "values", {values}), "dict");
94 %! free({"computing", "society"}) = {true};
95 %! assert (free("computing"), free("society"));