]> Creatis software - CreaPhase.git/blob - octave_packages/general-1.3.1/@dict/subsasgn.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / general-1.3.1 / @dict / subsasgn.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 =} subsasgn (d, s, val)
18 ## Overloaded subsasgn for dictionaries.
19 ## @end deftypefn
20
21 ## Author: Jaroslav Hajek <highegg@gmail.com>
22
23 function d = subsasgn (d, s, val)
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         ## Scalar assignment case. Search whether the key is present.
38         i = lookup (d.keys, ind, "m");
39         if (i)
40           ## The key is present; handle the rest of chain if needed,
41           ## then assign.
42           if (numel (s) > 1)
43             val = subsasgn (d.values{i}, s(2:end), val);
44           endif
45           d.values{i} = val;
46         else
47           ## The key is missing; handle the rest of chain if needed.
48           if (numel (s) > 1)
49             val = subsasgn ([], s(2:end), val);
50           endif
51           ## Look up the proper place to insert the new key.
52           i = lookup (d.keys, ind);
53           d.keys = [d.keys(1:i,1); {ind}; d.keys(i+1:end,1)];
54           ## Insert value.
55           d.values = [d.values(1:i,1); {val}; d.values(i+1:end,1)];
56         endif
57       elseif (iscellstr (ind))
58         ## Multiple assignment case. Perform checks.
59         if (numel (s) > 1)
60           error ("chained subscripts not allowed for multiple fields");
61         endif
62         if (isnull (val))
63           ## Deleting elements.
64           i = lookup (d.keys, ind, "m");
65           i = i(i != 0);
66           d.keys(i) = [];
67           d.values(i) = [];
68         elseif (iscell (val))
69           if (numel (val) == 1)
70             val = repmat (val, size (ind));
71           elseif (numel (ind) != numel (val))
72             error ("numbers of elements of index and rhs must match");
73           endif
74           ## Choose from two paths.
75           if (numel (ind) < numel (d.keys))
76             ## Scarce assignment. There's a good chance that all keys will be present.
77             i = lookup (d.keys, ind, "m");
78             mask = i != 0;
79             if (all (mask))
80               d.values(i) = val;
81             else
82               d.values(i(mask)) = val(mask);
83               mask = !mask;
84               [d.keys, i] = sort ([d.keys; ind(mask)(:)]);
85               d.values = [d.values; val(mask)(:)](i);
86             endif
87           else
88             ## Mass assignment. Probably most of the keys are new ones, so simply
89             ## melt all together.
90             [d.keys, i] = unique ([d.keys; ind(:)]);
91             d.values = [d.values; val(:)](i);
92           endif
93         else
94           error ("expected cell rhs for cell index");
95         endif
96       else
97         error ("invalid index");
98       endif
99     otherwise
100       error ("invalid subscript type for assignment");
101   endswitch
102 endfunction
103