Skip to content

Commit 9f376a4

Browse files
author
Fernando Alvarruiz
committed
containers.Map: Support char N-D arrays as keys (bug #67283).
* +containers/Map.m (Map, values, isKey, remove, subsref, subsasgn): Flatten input char keys. * +containers/Map.m: Add test for multi-row char arrays as keys.
1 parent 3669837 commit 9f376a4

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

scripts/+containers/Map.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@
202202
## Check type of keys and values, and define numeric_keys
203203
check_types (this);
204204

205+
## Flatten char keys
206+
if (! this.numeric_keys)
207+
keys = cellfun (@(x) x(:).', keys, 'UniformOutput', false);
208+
endif
209+
205210
## Sort keys (faster than call to sort_keys once encoded)
206211
if (this.numeric_keys)
207212
[~, I] = sort (cell2mat (keys));
@@ -261,6 +266,10 @@
261266
if (! iscell (keySet))
262267
error ("containers.Map: input argument 'keySet' must be a cell");
263268
endif
269+
if (! this.numeric_keys)
270+
## Flatten char keys
271+
keySet = cellfun (@(x) x(:).', keySet, 'UniformOutput', false);
272+
endif
264273
enckeySet = encode_keys (this, keySet);
265274
valueSet = cell (size (keySet));
266275
for i = 1:numel (valueSet)
@@ -298,6 +307,8 @@
298307
in = cellfun ("isnumeric", keySet) | cellfun ("islogical", keySet);
299308
if (! this.numeric_keys)
300309
in = ! in;
310+
## Flatten char keys
311+
keySet = cellfun (@(x) x(:).', keySet, 'UniformOutput', false);
301312
endif
302313
keySet = encode_keys (this, keySet(in));
303314
tf(in) = isfield (this.map, keySet);
@@ -326,6 +337,8 @@
326337
in = cellfun ("isnumeric", keySet) | cellfun ("islogical", keySet);
327338
if (! this.numeric_keys)
328339
in = ! in;
340+
## Flatten char keys
341+
keySet = cellfun (@(x) x(:).', keySet, 'UniformOutput', false);
329342
endif
330343
keySet = encode_keys (this, keySet(in));
331344
in = isfield (this.map, keySet);
@@ -434,6 +447,9 @@
434447
|| ! isscalar (key))))
435448
error ("containers.Map: specified key type does not match the type of this container");
436449
endif
450+
if (ischar (key))
451+
key = key(:).';
452+
endif
437453
enckey = encode_keys (this, key);
438454
if (! isfield (this.map, enckey))
439455
error ("containers.Map: specified key <%s> does not exist",
@@ -473,6 +489,9 @@
473489
endif
474490
val = feval (this.ValueType, val);
475491
endif
492+
if (ischar (key))
493+
key = key(:).';
494+
endif
476495
key = encode_keys (this, key);
477496
if (isfield (this.map, key))
478497
this.map.(key) = val;
@@ -652,6 +671,19 @@ function check_types (this)
652671
%! m = containers.Map ('', 3);
653672
%! assert (m(''), 3);
654673
674+
## Test multi-row char array keys
675+
%!test <*67283>
676+
%! k1 = char ("key", "one");
677+
%! k2 = char ("key", "two");
678+
%! k3 = char ("key", "three");
679+
%! m = containers.Map ({k1, k2}, {1, 2});
680+
%! m(k3) = 3;
681+
%! assert (m(k1), 1);
682+
%! assert (m.values ({k1, k3}), {1, 3});
683+
%! m.remove ({k1, k3});
684+
%! assert (m(k2), 2);
685+
%! assert (m.isKey ({k1, k2, k3}), [false, true, false]);
686+
655687
## Test numeric keys
656688
%!test
657689
%! key = [1, 2, 3, 4];

0 commit comments

Comments
 (0)