annotate scripts/+containers/Map.m @ 30564:796f54d4ddbf stable

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2021. In all .txi and .texi files except gpl.txi and gpl.texi in the doc/liboctave and doc/interpreter directories, change the copyright to "Octave Project Developers", the same as used for other source files. Update copyright notices for 2022 (not done since 2019). For gpl.txi and gpl.texi, change the copyright notice to be "Free Software Foundation, Inc." and leave the date at 2007 only because this file only contains the text of the GPL, not anything created by the Octave Project Developers. Add Paul Thomas to contributors.in.
author John W. Eaton <jwe@octave.org>
date Tue, 28 Dec 2021 18:22:40 -0500
parents 8afdeac24ba4
children 83f9f8bda883
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 30383
diff changeset
3 ## Copyright (C) 2017-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27431
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
7 ##
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
8 ## This file is part of Octave.
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23888
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23888
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
13 ## (at your option) any later version.
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
14 ##
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
18 ## GNU General Public License for more details.
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
19 ##
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23888
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
25
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
26 classdef Map < handle
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
27
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
28 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
29 ## @deftypefn {} {@var{m} =} containers.Map ()
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
30 ## @deftypefnx {} {@var{m} =} containers.Map (@var{keys}, @var{vals})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
31 ## @deftypefnx {} {@var{m} =} containers.Map (@var{keys}, @var{vals}, @qcode{"UniformValues"}, @var{is_uniform})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
32 ## @deftypefnx {} {@var{m} =} containers.Map (@qcode{"KeyType"}, @var{kt}, @qcode{"ValueType"}, @var{vt})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
33 ##
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
34 ## Create an object of the containers.Map class that stores a list of
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
35 ## key/value pairs.
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
36 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
37 ## @var{keys} is an array of @emph{unique} keys for the map. The keys can be
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
38 ## numeric scalars or strings. The type for numeric keys may be one of
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
39 ## @qcode{"double"}, @qcode{"single"}, @qcode{"int32"}, @qcode{"uint32"},
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
40 ## @qcode{"int64"}, or @qcode{"uint64"}. Other numeric or logical keys will
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
41 ## be converted to @qcode{"double"}. A single string key may be entered as
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
42 ## is. Multiple string keys are entered as a cell array of strings.
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
43 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
44 ## @var{vals} is an array of values for the map with the @emph{same} number
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
45 ## of elements as @var{keys}.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
46 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
47 ## When called with no input arguments a default map is created with strings
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
48 ## as the key type and @qcode{"any"} as the value type.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
49 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
50 ## The @qcode{"UniformValues"} option specifies whether the values of
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
51 ## the map must be strictly of the same type. If @var{is_uniform} is true,
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
52 ## any values which would be added to the map are first validated to ensure
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
53 ## they are of the correct type.
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
54 ##
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
55 ## When called with @qcode{"KeyType"} and @qcode{"ValueType"} arguments,
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
56 ## create an empty map with the specified types. The inputs @var{kt} and
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
57 ## @var{vt} are the types for the keys and values of the map respectively.
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
58 ## Allowed values for @var{kt} are @qcode{"char"}, @qcode{"double"},
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
59 ## @qcode{"single"}, @qcode{"int32"}, @qcode{"uint32"}, @qcode{"int64"},
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
60 ## @qcode{"uint64"}. Allowed values for @var{vt} are @qcode{"any"},
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
61 ## @qcode{"char"}, @qcode{"double"}, @qcode{"single"}, @qcode{"int32"},
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
62 ## @qcode{"uint32"}, @qcode{"int64"}, @qcode{"uint64"}, @qcode{"logical"}.
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
63 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
64 ## The return value @var{m} is an object of the containers.Map class.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
65 ## @seealso{struct}
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
66 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
67
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
68 properties (GetAccess = public, SetAccess = private)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
69
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
70 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
71 ## @deftypefn {} {@var{type} =} Map.KeyType ()
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
72 ## Return the key type.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
73 ##
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
74 ## Possible values are listed above when describing input variable
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
75 ## @var{kt}.
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
76 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
77
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
78 KeyType = "char";
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
79
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
80 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
81 ## @deftypefn {} {@var{type} =} Map.ValueType ()
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
82 ## Return the value type.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
83 ##
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
84 ## Possible values are listed above when describing input variable
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
85 ## @var{vt}.
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
86 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
87
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
88 ValueType = "any";
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
89
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
90 endproperties
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
91
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
92 properties (Dependent, SetAccess = protected)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
93
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
94 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
95 ## @deftypefn {} {@var{n} =} Map.Count ()
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
96 ## Return the number of key/value pairs in the map, as a uint64.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
97 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
98
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
99 Count = 0;
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
100
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
101 endproperties
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
102
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
103 properties (private)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
104 map = struct ();
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
105
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
106 numeric_keys = false;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
107 endproperties
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
108
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
109 methods (Access = public)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
110
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
111 function this = Map (varargin)
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
112
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
113 if (nargin == 0)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
114 ## Empty object with "char" key type and "any" value type.
25949
c2bf210ac94f style fixes
John W. Eaton <jwe@octave.org>
parents: 25455
diff changeset
115 elseif (nargin == 2 || (nargin == 4
c2bf210ac94f style fixes
John W. Eaton <jwe@octave.org>
parents: 25455
diff changeset
116 && strcmpi (varargin{3}, "UniformValues")))
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
117 ## Get Map keys
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
118 keys = varargin{1};
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
119 if (isempty (keys))
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
120 error ("containers.Map: empty keys are not allowed");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
121 endif
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
122 if (! iscell (keys))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
123 if (isnumeric (keys) || islogical (keys))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
124 keys = num2cell (keys);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
125 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
126 keys = { keys };
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
127 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
128 endif
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
129 keys = keys(:); # Use Nx1 column vector to simplify calls to all()
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
130
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
131 ## Get Map values
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
132 vals = varargin{2};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
133 if (! iscell (vals))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
134 if ((isnumeric (vals) || islogical (vals)) && ! isscalar (keys))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
135 vals = num2cell (vals);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
136 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
137 vals = { vals };
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
138 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
139 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
140 vals = vals(:);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
141 if (numel (keys) != numel (vals))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
142 error ("containers.Map: the number of keys and values must match");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
143 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
144
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
145 ## Determine KeyType
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
146 kt = unique (cellfun (@class, keys, "UniformOutput", false));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
147 if (numel (kt) == 1)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
148 ## Single key type--most common case
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
149 if (strcmp (kt{1}, "char"))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
150 ## String is most common key type
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
151 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
152 if (! all (cellfun ("isreal", keys)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
153 & (cellfun ("numel", keys) == 1)))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
154 error ("containers.Map: keys must be real scalar numeric values or char vectors");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
155 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
156 if (any (strcmp (kt{1},
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
157 {"logical", "int8", "uint8", "int16", "uint16"})))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
158 kt = { "double" };
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
159 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
160 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
161 this.KeyType = char (kt);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
162 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
163 ## Multiple key types
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
164 if (! all (ismember (kt, {"double", "single", "int8", "uint8", ...
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
165 "int16", "uint16", "int32", "uint32", ...
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
166 "int64", "uint64", "logical"})))
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
167 error ("containers.Map: when using multiple key types, all types must be numeric");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
168 else
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
169 warning ("containers.Map: all keys will be converted to double");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
170 this.KeyType = "double";
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
171 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
172 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
173
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
174 ## Determine ValueType
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
175 vt = unique (cellfun (@class, vals, "UniformOutput", false));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
176 if (numel (vt) == 1
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
177 && (ischar (vals{1})
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
178 || ((isnumeric (vals{1}) || islogical (vals{1}))
23368
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
179 && all (cellfun (@numel, vals) == 1))))
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
180 this.ValueType = vt{1};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
181 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
182 this.ValueType = "any";
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
183 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
184
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
185 ## Process UniformValues option
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
186 if (nargin == 4)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
187 UniformValues = varargin{4};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
188 if (! isscalar (UniformValues)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
189 || ! (islogical (UniformValues) || isnumeric (UniformValues)))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
190 error ("containers.Map: 'UniformValues' must be a logical scalar");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
191 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
192
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
193 if (UniformValues)
23368
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
194 if (numel (vt) > 1
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
195 || (strcmp (this.ValueType, "any") && isreal (vals{1})))
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
196 error ("containers.Map: when 'UniformValues' is true, all values must be scalars of the same data type");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
197 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
198 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
199 this.ValueType = "any";
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
200 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
201 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
202
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
203 ## Check type of keys and values, and define numeric_keys
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
204 check_types (this);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
205
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
206 ## Sort keys (faster than call to sort_keys once encoded)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
207 if (this.numeric_keys)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
208 [~, I] = sort (cell2mat (keys));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
209 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
210 [~, I] = sort (keys);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
211 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
212 keys = keys(I);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
213 vals = vals(I);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
214 ## Convert keys to char vectors
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
215 keys = encode_keys (this, keys);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
216 ## Fill in the Map
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
217 this.map = cell2struct (vals, keys);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
218 elseif (nargin == 4)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
219 for i = [1, 3]
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
220 switch (tolower (varargin{i}))
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
221 case "keytype"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
222 this.KeyType = varargin{i+1};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
223 case "valuetype"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
224 this.ValueType = varargin{i+1};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
225 otherwise
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
226 error ("containers.Map: missing parameter name 'KeyType' or 'ValueType'");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
227 endswitch
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
228 endfor
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
229 check_types (this);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
230 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
231 error ("containers.Map: incorrect number of inputs specified");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
232 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
233 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
234
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
235 function keySet = keys (this)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
236
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
237 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
238 ## @deftypefn {} {@var{keys} =} Map.keys ()
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
239 ## Return the sorted list of all keys of the map as a cell vector.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
240 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
241
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
242 keySet = fieldnames (this.map).'; # compatibility requires row vector
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
243 keySet = decode_keys (this, keySet);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
244 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
245
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
246 function valueSet = values (this, keySet)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
247
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
248 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
249 ## @deftypefn {} {@var{val} =} Map.values ()
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
250 ## @deftypefnx {} {@var{val} =} Map.values (@var{keySet})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
251 ## Return the list of all the values stored in the map as a cell vector.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
252 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
253 ## If @var{keySet}, a cell array of keys is provided, the corresponding
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
254 ## values will be returned.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
255 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
256
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
257 if (nargin == 1)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
258 valueSet = struct2cell (this.map).';
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
259 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
260 if (! iscell (keySet))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
261 error ("containers.Map: input argument 'keySet' must be a cell");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
262 endif
23368
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
263 enckeySet = encode_keys (this, keySet);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
264 valueSet = cell (size (keySet));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
265 for i = 1:numel (valueSet)
23368
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
266 try
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
267 valueSet{i} = this.map.(enckeySet{i});
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
268 catch
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
269 error ("containers.Map: key <%s> does not exist",
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
270 strtrim (disp (keySet{i})));
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
271 end_try_catch
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
272 endfor
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
273 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
274 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
275
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
276 function tf = isKey (this, keySet)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
277
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
278 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
279 ## @deftypefn {} {@var{mask} =} Map.isKey (@var{keySet})
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
280 ## Return a logical array which is true where the elements of
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
281 ## @var{keySet} are keys of the map and false otherwise.
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
282 ##
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
283 ## @var{keySet} is a cell array of keys. If a single key is being
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
284 ## checked, it can be entered directly as a scalar value or a char
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
285 ## vector.
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
286 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
287
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
288 if (! iscell (keySet))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
289 if (isnumeric (keySet) || islogical (keySet))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
290 keySet = num2cell (keySet);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
291 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
292 keySet = { keySet };
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
293 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
294 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
295 tf = false (size (keySet));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
296 in = cellfun ("isnumeric", keySet) | cellfun ("islogical", keySet);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
297 if (! this.numeric_keys)
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
298 in = ! in;
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
299 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
300 keySet = encode_keys (this, keySet(in));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
301 tf(in) = isfield (this.map, keySet);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
302 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
303
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
304 function this = remove (this, keySet)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
305
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
306 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
307 ## @deftypefn {} {} Map.remove (@var{keySet})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
308 ## Remove the list of key/value pairs specified by a cell array of keys
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
309 ## @var{keySet} from the map.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
310 ##
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
311 ## @var{keySet}) can also be a scalar value or a string when specifying a
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
312 ## single key.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
313 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
314
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
315 if (! iscell (keySet))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
316 if (isnumeric (keySet) || islogical (keySet))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
317 keySet = num2cell (keySet);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
318 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
319 keySet = { keySet };
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
320 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
321 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
322 in = cellfun ("isnumeric", keySet) | cellfun ("islogical", keySet);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
323 if (! this.numeric_keys)
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
324 in = ! in;
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
325 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
326 keySet = encode_keys (this, keySet(in));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
327 in = isfield (this.map, keySet);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
328 this.map = rmfield (this.map, keySet(in));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
329 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
330
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
331 function varargout = size (this, n)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
332
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
333 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
334 ## @deftypefn {} {@var{l} =} Map.size (@var{n})
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
335 ## @deftypefnx {} {@var{sz} =} Map.size ()
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
336 ## @deftypefnx {} {@var{dim_1}, @dots{}, @var{dim_n} =} Map.size ()
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
337 ## If @var{n} is 1, return the number of key/value pairs in the map,
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
338 ## otherwise return 1.
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
339 ##
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
340 ## Without input arguments, return vector @code{[@var{l}, 1]} where
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
341 ## @var{l} is the number of key/value pairs in the map.
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30236
diff changeset
342 ##
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
343 ## With multiple outputs, return @code{[@var{l}, @dots{}, 1]}.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
344 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
345
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
346 c = length (this);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
347 if (nargin == 1)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
348 if (nargout <= 1)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
349 varargout = { [c 1] };
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
350 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
351 varargout{1} = c;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
352 [varargout{2:nargout}] = deal (1);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
353 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
354 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
355 if (n == 1)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
356 varargout = { c };
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
357 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
358 varargout = { 1 };
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
359 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
360 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
361 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
362
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
363 function len = length (this)
28378
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
364
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
365 ## -*- texinfo -*-
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
366 ## @deftypefn {} {@var{n} =} Map.length ()
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
367 ## Return the number of key/value pairs in the map.
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
368 ## @end deftypefn
8dbe4eec1741 move classdef docstring comments inside classdef definitions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
369
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
370 len = double (this.Count);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
371 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
372
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
373 function tf = isempty (this)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
374 tf = (this.Count == 0);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
375 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
376
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
377 function count = get.Count (this)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
378 count = uint64 (numfields (this.map));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
379 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
380
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
381 function sref = subsref (this, s)
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
382
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
383 switch (s(1).type)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
384 case "."
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
385 switch (s(1).subs)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
386 case "keys"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
387 sref = keys (this);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
388 case "values"
29137
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
389 if (numel (s) > 1
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
390 && strcmp (s(2).type, "()") && ! isempty (s(2).subs))
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
391 sref = values (this, s(2).subs{1});
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
392 s(1) = [];
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
393 else
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
394 sref = values (this);
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
395 endif
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
396 case "size"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
397 sref = size (this);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
398 case "length"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
399 sref = length (this);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
400 case "isempty"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
401 sref = isempty (this);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
402 case "Count"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
403 sref = this.Count;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
404 case "KeyType"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
405 sref = this.KeyType;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
406 case "ValueType"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
407 sref = this.ValueType;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
408 case {"isKey", "remove"}
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
409 if (numel (s) < 2 || numel (s(2).subs) != 1)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
410 error ("containers.Map: input argument 'KeySet' is missing");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
411 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
412 sref = feval (s(1).subs, this, s(2).subs{1});
29137
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
413 s(1) = [];
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
414 otherwise
25455
4c4a49bffdc5 Fix typo in error statement from containers.Map (bug #54103).
Rik <rik@octave.org>
parents: 25054
diff changeset
415 error ("containers.Map: unknown property '%s'", s(1).subs);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
416 endswitch
27067
9cf1a1732201 Map.m: Silence unnecessary warning about "Octave:empty-index" (bug #56203).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 26716
diff changeset
417 if (numel (s) > 1 && strcmp (s(2).type, "()") && isempty (s(2).subs))
9cf1a1732201 Map.m: Silence unnecessary warning about "Octave:empty-index" (bug #56203).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 26716
diff changeset
418 s(1) = [];
9cf1a1732201 Map.m: Silence unnecessary warning about "Octave:empty-index" (bug #56203).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 26716
diff changeset
419 endif
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
420 case "()"
23888
f9a33c3925a2 Map.m: Issue a more informative error message for empty indexing.
Rik <rik@octave.org>
parents: 23565
diff changeset
421 if (isempty (s(1).subs))
f9a33c3925a2 Map.m: Issue a more informative error message for empty indexing.
Rik <rik@octave.org>
parents: 23565
diff changeset
422 error ("containers.Map: no key specified");
f9a33c3925a2 Map.m: Issue a more informative error message for empty indexing.
Rik <rik@octave.org>
parents: 23565
diff changeset
423 endif
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
424 key = s(1).subs{1};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
425 if ((! this.numeric_keys && ! ischar (key))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
426 || (this.numeric_keys && (! (isnumeric (key) || islogical (key))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
427 || ! isscalar (key))))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
428 error ("containers.Map: specified key type does not match the type of this container");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
429 endif
23368
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
430 enckey = encode_keys (this, key);
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
431 if (! isfield (this.map, enckey))
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
432 error ("containers.Map: specified key <%s> does not exist",
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
433 strtrim (disp (key)));
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
434 endif
23368
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
435 sref = this.map.(enckey);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
436 otherwise
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
437 error ("containers.Map: only '()' indexing is supported");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
438 endswitch
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
439 if (numel (s) > 1)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
440 sref = subsref (sref, s(2:end));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
441 endif
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
442
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
443 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
444
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
445 function this = subsasgn (this, s, val)
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
446
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
447 if (numel (s) > 1)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
448 error ("containers.Map: only one level of indexing is supported");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
449 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
450 switch (s(1).type)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
451 case "."
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
452 error ("containers.Map: properties are read-only");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
453 case "()"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
454 key = s(1).subs{1};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
455 if ((! this.numeric_keys && ! ischar (key))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
456 || (this.numeric_keys && (! (isnumeric (key) || islogical (key))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
457 || ! isscalar (key))))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
458 error ("containers.Map: specified key type does not match the type of this container");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
459 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
460 if (! strcmp (this.ValueType, "any"))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
461 if ((strcmp (this.ValueType, "char") && ! ischar (val))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
462 || (! strcmp (this.ValueType, "char")
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
463 && (! (isnumeric (val) || islogical (val))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
464 || ! isscalar (val))))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
465 error ("containers.Map: specified value type does not match the type of this container");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
466 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
467 val = feval (this.ValueType, val);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
468 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
469 key = encode_keys (this, key);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
470 if (isfield (this.map, key))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
471 this.map.(key) = val;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
472 else
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
473 this.map.(key) = val;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
474 this = sort_keys (this);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
475 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
476 case "{}"
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
477 error ("containers.Map: only '()' indexing is supported for assigning values");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
478 endswitch
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
479
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
480 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
481
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
482 function newobj = horzcat (varargin)
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
483 warning ("Octave:language-extension",
23396
945b53af4655 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 23368
diff changeset
484 "containers.Map: horizontal concatenation is an Octave-only feature");
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
485 newobj = vertcat (varargin{:});
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
486 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
487
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
488 function newobj = vertcat (varargin)
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
489
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
490 ## When concatenating maps, the data type of all values must be
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
491 ## consistent with the ValueType of the leftmost map.
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
492 keySet = cell (1, 0);
27431
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
493 keyTypes = cell (1, 0);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
494 for i = 1:numel (varargin)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
495 keySet = [keySet, keys(varargin{i})];
27431
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
496 keyTypes = [keyTypes, varargin{i}.KeyType];
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
497 endfor
27431
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
498 if (numel (unique (keyTypes)) != 1)
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
499 if (any (strcmp (keyTypes, "char")))
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
500 error ("containers.Map: cannot concatenate maps with numeric and string keys");
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
501 endif
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
502 keySet = cellfun (@(x) feval (keyTypes{1}, x), keySet, "UniformOutput", false);
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
503 endif
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
504 valueSet = cell (1, 0);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
505 for i = 1:numel (varargin)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
506 valueSet = [valueSet, values(varargin{i})];
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
507 endfor
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
508 newobj = containers.Map (keySet, valueSet);
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
509
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
510 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
511
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
512 function disp (this)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
513 printf (" containers.Map object with properties:\n\n");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
514 printf ([" Count : %d\n" ...
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
515 " KeyType : %s\n" ...
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
516 " ValueType : %s\n\n"],
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
517 this.Count, this.KeyType, this.ValueType);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
518 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
519
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
520 endmethods
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
521
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
522 methods (Access = private)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
523
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
524 ## All keys are encoded as strings.
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
525 ## For numeric keys, this requires conversion.
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
526 function keys = encode_keys (this, keys)
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
527
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
528 if (iscellstr (keys) || ischar (keys))
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
529 return;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
530 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
531 cell_input = iscell (keys);
23353
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
532 if (cell_input)
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
533 orig_sz = size (keys);
23353
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
534 if (! all (cellfun ("isclass", keys, this.KeyType)))
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
535 ## Convert input set to KeyType. This is rarely necessary.
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
536 keys = cellfun (@(x) feval (this.KeyType, x), keys);
23353
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
537 else
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
538 keys = cell2mat (keys);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
539 endif
27431
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
540 elseif (! isa (keys, this.KeyType))
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
541 keys = feval (this.KeyType, keys);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
542 endif
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
543 keys = num2hex (keys); # Force to char matrix with single logical column
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
544 if (cell_input)
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
545 keys = reshape (cellstr (keys), orig_sz);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
546 endif
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
547
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
548 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
549
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
550 function keys = decode_keys (this, keys)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
551 if (this.numeric_keys)
23358
42bd10feedfa use new hex2num and num2hex features to simplify +containers.Map key encoding
John W. Eaton <jwe@octave.org>
parents: 23357
diff changeset
552 keys = hex2num (keys, this.KeyType);
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
553 ## This always returns a 1xN list of keys ignoring input shape
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
554 keys = mat2cell (keys(:).', 1, ones (1, numel (keys)));
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
555 endif
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
556 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
557
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
558 function this = sort_keys (this)
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
559
23357
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
560 keySet = keys (this);
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
561 if (this.numeric_keys)
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
562 [~, p] = sort (cell2mat (keySet));
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
563 else
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
564 [~, p] = sort (keySet);
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
565 endif
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
566 this.map = orderfields (this.map, p);
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
567
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
568 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
569
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
570 function check_types (this)
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
571
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
572 switch (this.KeyType)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
573 case {"char"}
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
574 this.numeric_keys = false;
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
575 case {"double", "single", "int32", "uint32", "int64", "uint64"}
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
576 this.numeric_keys = true;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
577 otherwise
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
578 error ("containers.Map: unsupported KeyType");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
579 endswitch
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
580
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
581 if (! any (strcmp (this.ValueType, {"any"; "char"; "logical";
23396
945b53af4655 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 23368
diff changeset
582 "double"; "single";
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
583 "int8"; "uint8"; "int16"; "uint16";
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
584 "int32"; "uint32";
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
585 "int64"; "uint64"})))
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
586 error ("containers.Map: unsupported ValueType");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
587 endif
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
588
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
589 endfunction
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
590
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
591 endmethods
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
592
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
593 endclassdef
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
594
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
595
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
596 ## Test empty Map
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
597 %!test
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
598 %! m = containers.Map ();
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
599 %! assert (m.Count, uint64 (0));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
600 %! assert (length (m), 0);
30236
628f26e122d9 maint: use rows() or columns() instead of size(__, 1 | 2) for clarity.
Rik <rik@octave.org>
parents: 29434
diff changeset
601 %! assert (rows (m), 0);
628f26e122d9 maint: use rows() or columns() instead of size(__, 1 | 2) for clarity.
Rik <rik@octave.org>
parents: 29434
diff changeset
602 %! assert (columns (m), 1);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
603 %! assert (isempty (m));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
604 %! assert (isempty (keys (m)));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
605 %! assert (isempty (values (m)));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
606 %! assert (isKey (m, "Octave"), false);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
607 %! assert (isKey (m, 42), false);
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
608 %! assert (isKey (m, {"Octave", 42}), [false, false]);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
609 %! assert (isKey (m, {"Octave"; 42}), [false; false]);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
610
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
611 ## Test string keys
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
612 %!test
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
613 %! key = {"One", "Two", "Three", "Four"};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
614 %! val = [1, 2, 3, 4];
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
615 %! m = containers.Map (key, val);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
616 %! assert (m.KeyType, "char");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
617 %! assert (m.ValueType, "double");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
618 %! assert (m.Count, uint64 (4));
23357
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
619 %! assert (iscell (keys (m)));
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
620 %! assert (iscell (values (m)));
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
621 %! assert (size (keys (m)), [1, 4]);
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
622 %! assert (size (values (m)), [1, 4]);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
623 %! assert (m("Two"), 2);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
624 %! m("Five") = 5;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
625 %! key2 = {"Six", "Seven", "Eight"};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
626 %! val2 = [6, 7, 8];
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
627 %! m2 = containers.Map (key2, val2);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
628 %! m = [m; m2];
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
629 %! assert (m.Count, uint64 (8));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
630 %! k = keys (m);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
631 %! assert (isempty (setdiff (k, [key, "Five", key2])));
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
632 %! v = values (m, {"Three", "Four"; "Five", "Six"});
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
633 %! assert (v, {3, 4; 5, 6});
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
634 %! remove (m, {"Three", "Four"});
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
635 %! k = keys (m);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
636 %! assert (numel (k), 6);
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
637 %! assert (m.isKey({"One", "Four"; "Ten", "Five"}), [true,false; false,true]);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
638
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
639 ## Test numeric keys
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
640 %!test
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
641 %! key = [1, 2, 3, 4];
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
642 %! val = {"One", "Two", "Three", "Four"};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
643 %! m = containers.Map (key, val);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
644 %! assert (m.KeyType, "double");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
645 %! assert (m.ValueType, "char");
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
646 %! assert (iscell (keys (m)));
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
647 %! assert (iscell (values (m)));
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
648 %! assert (size (keys (m)), [1, 4]);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
649 %! assert (size (values (m)), [1, 4]);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
650 %! assert (m(2), "Two");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
651 %! m(5) = "Five";
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
652 %! key2 = [6, 7, 8];
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
653 %! val2 = {"Six", "Seven", "Eight"};
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
654 %! m2 = containers.Map (key2, val2);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
655 %! m = [m; m2];
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
656 %! assert (m.Count, uint64 (8));
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
657 %! k = keys (m);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
658 %! assert (isempty (setdiff (cell2mat (k), [key, 5, key2])));
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
659 %! v = values (m, {3, 4; 5, 6});
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
660 %! assert (v, {"Three", "Four"; "Five", "Six"});
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
661 %! remove (m, {3, 4});
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
662 %! k = keys (m);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
663 %! assert (numel (k), 6);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
664 %! assert (m.isKey({1, 4; 10, 5}), [true,false; false,true]);
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
665
23368
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
666 ## Test that non-scalar objects force ValueType to "any"
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
667 %!test
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
668 %! key = [2, 3, 4];
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
669 %! val = {eye(2), eye(3), eye(4)};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
670 %! m = containers.Map (key, val);
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
671 %! assert (m(3), eye (3));
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
672 %! assert (m(2)(2,2), 1);
23357
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
673 %! assert (m.KeyType, "double");
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
674 %! assert (m.ValueType, "any");
23368
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
675 %! key = [2, 3, 4];
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
676 %! val = {2, 3, [4 5]};
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
677 %! m = containers.Map (key, val);
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
678 %! assert (m.KeyType, "double");
b04466113212 containers.Map: UniformValue requires values be scalars, not just equal size (bug #49559).
Rik <rik@octave.org>
parents: 23366
diff changeset
679 %! assert (m.ValueType, "any");
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
680
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
681 ## Test that mixed object types force ValueType to "any"
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
682 %!test
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
683 %! key = [2, 3, 4];
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
684 %! val = {double(1), single(2), uint8(3)};
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
685 %! m = containers.Map (key, val);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
686 %! assert (m.KeyType, "double");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
687 %! assert (m.ValueType, "any");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
688 %! assert (class (m(4)), "uint8");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
689 %! assert (class (m(3)), "single");
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
690
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
691 ## Test that non-numeric, non-char object types force ValueType to "any"
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
692 %!test
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
693 %! key = {"a", "b"};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
694 %! val = {struct(), struct()};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
695 %! m = containers.Map (key, val);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
696 %! assert (m.ValueType, "any");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
697 %! m = containers.Map (key, val, "UniformValues", true);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
698 %! assert (m.ValueType, "any");
23357
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
699 %! m = containers.Map (key, {1, 2i});
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
700 %! assert (m.ValueType, "double");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
701
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
702 ## Test "UniformValues" input
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
703 %!test
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
704 %! key = {"one", "two", "three"};
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
705 %! val = {1, 2, 3};
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
706 %! m = containers.Map (key, val, "UniformValues",false);
23357
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
707 %! assert (m.ValueType, "any");
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
708 %! m("four") = "GNU";
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
709 %! assert (values (m), {"GNU", 1, 3, 2});
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
710
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
711 ## Test 4-input form of Map
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
712 %!test
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
713 %! m = containers.Map ("KeyType","char", "ValueType","int32");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
714 %! assert (m.KeyType, "char");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
715 %! assert (m.ValueType, "int32");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
716 %! assert (m.Count, uint64 (0));
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
717 %! assert (isempty (m));
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
718
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
719 ## Test all allowable key types
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
720 %!test
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
721 %! key = [2, 3, 4];
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
722 %! val = {2, 3, 4};
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
723 %! types = {"double", "single", "int32", "uint32", "int64", "uint64"};
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
724 %! for type = types
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
725 %! type = type{1};
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
726 %! k = feval (type, key);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
727 %! m = containers.Map (k, val);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
728 %! assert (m.KeyType, type);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
729 %! assert (isa (keys (m){1}, type));
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
730 %! endfor
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
731 %! assert (all (isKey (m, keys (m))));
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
732
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
733 ## Test that other numeric key types are converted to double
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
734 %!test
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
735 %! key = [0, 1];
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
736 %! val = {1, 2};
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
737 %! types = {"logical", "int8", "uint8", "int16", "uint16"};
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
738 %! for type = types
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
739 %! type = type{1};
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
740 %! k = feval (type, key);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
741 %! m = containers.Map (k, val);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
742 %! assert (m.KeyType, "double");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
743 %! assert (isa (keys (m){1}, "double"));
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
744 %! endfor
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
745 %! assert (all (isKey (m, keys (m))));
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
746
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
747 ## Test removal of keys
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
748 %!test
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
749 %! m = containers.Map ({"a","b","c"}, {1,2,3});
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
750 %! assert (m.isKey ("a"), true);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
751 %! assert (m.isKey ({"a","d"}), [true, false]);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
752 %! m.remove ("a");
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
753 %! m.remove ({"b","c"});
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
754 %! assert (isempty (m));
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
755
23353
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
756 ## Ensure that exact key values are preserved.
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
757 %!test
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
758 %! keytypes = {"int32", "int64", "uint32", "uint64"};
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
759 %! for keytype = keytypes
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
760 %! keytype = keytype{1};
23353
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
761 %! key = intmax (keytype);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
762 %! m = containers.Map (key, pi);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
763 %! assert (m.isKey (key));
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
764 %! assert (m.keys (), {key});
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
765 %! key = intmin (keytype);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
766 %! m = containers.Map (key, pi);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
767 %! assert (m.isKey (key));
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
768 %! assert (m.keys (), {key});
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
769 %! endfor
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
770 %! keytypes = {"double", "single"};
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
771 %! for i = 1:numel (keytypes)
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
772 %! keytype = keytypes{i};
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
773 %! key = realmax (keytype);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
774 %! m = containers.Map (key, pi);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
775 %! assert (m.isKey (key));
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
776 %! assert (m.keys (), {key});
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
777 %! key = realmin (keytype);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
778 %! m = containers.Map (key, pi);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
779 %! assert (m.isKey (key));
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
780 %! assert (m.keys (), {key});
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
781 %! key = -realmax (keytype);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
782 %! m = containers.Map (key, pi);
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
783 %! assert (m.isKey (key));
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
784 %! assert (m.keys (), {key});
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
785 %! endfor
95744d6d7d3b use hex values for numeric containers.Map keys (bug #49559)
John W. Eaton <jwe@octave.org>
parents: 23348
diff changeset
786
27431
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
787 ## Test using mixed numerical keys (subsref)
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
788 %!test <*56594>
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
789 %! key = [1, 2, 3];
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
790 %! val = {"One", "Two", "Three"};
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
791 %! types = {"double", "single", "int32", "uint32", "int64", "uint64", ...
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
792 %! "int8", "uint8", "int16", "uint16"};
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
793 %! for type1 = types
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
794 %! type = type1{1};
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
795 %! k = feval (type, key);
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
796 %! m = containers.Map (k, val);
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
797 %! for type2 = [types, "logical"]
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
798 %! type = type2{1};
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
799 %! k = feval (type, key(1));
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
800 %! assert (m(k), "One");
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
801 %! endfor
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
802 %! endfor
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
803
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
804 ## Test using mixed numerical keys (subsasgn)
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
805 %!test <*56594>
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
806 %! key = [1, 2, 3];
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
807 %! val = {"One", "Two", "Three"};
30383
8afdeac24ba4 maint: Use space after function name and before opening parenthesis.
Rik <rik@octave.org>
parents: 30379
diff changeset
808 %! m = containers.Map (key, val);
27431
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
809 %! m (uint32 (1)) = "Four";
30383
8afdeac24ba4 maint: Use space after function name and before opening parenthesis.
Rik <rik@octave.org>
parents: 30379
diff changeset
810 %! assert (m.Count, uint64 (3));
27431
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
811 %! assert (keys (m), {1, 2, 3});
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
812 %! assert (m(1), "Four");
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
813 %! assert (m(uint16 (1)), "Four");
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
814
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
815 ## Test sort order of keys and values
23357
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
816 %!test
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
817 %! key = {"d","a","b"};
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
818 %! m = containers.Map (key, 1:numel (key));
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
819 %! assert (keys (m), sort (key));
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
820 %! assert (values (m), {2, 3, 1});
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
821 %! m("c") = 4;
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
822 %! assert (keys (m), sort ([key, "c"]));
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
823 %! assert (values (m), {2, 3, 4, 1});
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
824 %! key = [Inf, 2, 0, -Inf, -1];
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
825 %! m = containers.Map (key, 1:numel (key));
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
826 %! assert (keys (m), num2cell (sort (key)));
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
827 %! assert (values (m), {4, 5, 3, 2, 1});
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
828 %! m(-2) = 6;
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
829 %! assert (keys (m), num2cell (sort ([key, -2])));
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
830 %! assert (values (m), {4, 6, 5, 3, 2, 1});
426b593b4b6b fix key sorting for +containers.Map
Guillaume Flandin
parents: 23353
diff changeset
831
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
832 ## Test horizontal concatenation
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
833 %!test
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
834 %! m1 = containers.Map ("b", 2);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
835 %! m2 = containers.Map ("a", 1);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
836 %! m3 = [m1, m2];
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
837 %! k = keys (m3);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
838 %! assert (numel (k), 2);
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
839 %! assert (k, {"a", "b"});
27431
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
840 %! m1 = containers.Map (1, 1);
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
841 %! m2 = containers.Map (single ([2, 3]), {2, 3});
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
842 %! m3 = [m1, m2];
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
843 %! assert (m3.KeyType, "double");
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
844 %! assert (keys (m3), {1, 2, 3});
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
845 %! m3 = [m2, m1];
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
846 %! assert (m3.KeyType, "single");
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
847
29137
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
848 ## Test subsref calls
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
849 %!test <*59607>
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
850 %! months = {'Jan', 'Feb', 'Mar', 'Apr'};
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
851 %! vals = [10, 11, 12, 13];
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
852 %! M = containers.Map (months, vals);
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
853 %! keys = {'Jan', 'Feb'};
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
854 %! assert (M.values, values (M));
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
855 %! assert (M.values (), values (M));
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
856 %! assert (M.values (keys), {10, 11});
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
857 %! assert (M.values (keys)(2), {11});
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
858 %! assert (M.values (keys){2}, 11);
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
859 %!test
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
860 %! months = {'Jan', 'Feb', 'Mar', 'Apr'};
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
861 %! vals = [10, 11, 12, 13];
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
862 %! M = containers.Map (months, vals);
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
863 %! keys = {'Jan', 'FooBar', 'Feb'};
29434
91f32bf0d497 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 29359
diff changeset
864 %! assert (M.isKey (keys)(2:end), logical ([0, 1]));
29137
fd8e536d166e +containers.Map.m: Fix subsref errors (bug #59607).
Rik <rik@octave.org>
parents: 28378
diff changeset
865
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
866 ## Test input validation
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
867 %!error containers.Map (1,2,3)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
868 %!error containers.Map (1,2,3,4,5)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
869 %!error <empty keys are not allowed> containers.Map ([], 1)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
870 %!error <number of keys and values must match> containers.Map (1, {2, 3})
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
871 %!error <keys must be real .* values> containers.Map ({{1}}, 2)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
872 %!error <keys must be .* scalar .* values> containers.Map ({magic(3)}, 2)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
873 %!warning <keys .* converted to double>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
874 %! containers.Map ({1,int8(2)}, {3,4});
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
875 %!error <when using multiple key types, all types must be numeric>
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
876 %! containers.Map ({1, {2}}, {3,4})
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
877 %!error <'UniformValues' must be a logical scalar>
23366
56c59b3f9172 containers.Map: Fix values fcn, Add BIST tests (bug #49559).
Rik <rik@octave.org>
parents: 23358
diff changeset
878 %! containers.Map (1,2, 'UniformValues', ones (2,2))
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
879 %!error <'UniformValues' must be a logical scalar>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
880 %! containers.Map (1,2, 'UniformValues', {true})
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
881 %!error <all values must be scalars of the same data type>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
882 %! containers.Map ({1,2}, {3, uint32(4)}, "UniformValues", true)
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
883 %!error <missing parameter name 'KeyType'>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
884 %! containers.Map ("keytype", "char", "vtype", "any")
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
885 %!error <'keySet' must be a cell>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
886 %! m = containers.Map ();
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
887 %! values (m, 1);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
888 %#!error <key .foobar. does not exist>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
889 %! m = containers.Map ();
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
890 %! values (m, "foobar");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
891 %!error <input argument 'KeySet' is missing>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
892 %! m = containers.Map ();
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
893 %! m.isKey (1,2);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
894 %!error <unknown property 'foobar'>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
895 %! m = containers.Map ();
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
896 %! m.foobar;
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
897 %!error <key type does not match the type of this container>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
898 %! m = containers.Map ("a", 1);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
899 %! m(1);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
900 %!error <specified key .b. does not exist>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
901 %! m = containers.Map ("a", 1);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
902 %! m("b");
26716
daa281e03d9a Harden BIST tests against changes in format().
Rik <rik@octave.org>
parents: 26376
diff changeset
903 %!test
daa281e03d9a Harden BIST tests against changes in format().
Rik <rik@octave.org>
parents: 26376
diff changeset
904 %! [old_fmt, old_spacing] = format ();
daa281e03d9a Harden BIST tests against changes in format().
Rik <rik@octave.org>
parents: 26376
diff changeset
905 %! unwind_protect
daa281e03d9a Harden BIST tests against changes in format().
Rik <rik@octave.org>
parents: 26376
diff changeset
906 %! format short;
daa281e03d9a Harden BIST tests against changes in format().
Rik <rik@octave.org>
parents: 26376
diff changeset
907 %! m = containers.Map (1, 1);
daa281e03d9a Harden BIST tests against changes in format().
Rik <rik@octave.org>
parents: 26376
diff changeset
908 %! fail ("m(2)", "specified key <2> does not exist");
daa281e03d9a Harden BIST tests against changes in format().
Rik <rik@octave.org>
parents: 26376
diff changeset
909 %! unwind_protect_cleanup
daa281e03d9a Harden BIST tests against changes in format().
Rik <rik@octave.org>
parents: 26376
diff changeset
910 %! format (old_fmt);
daa281e03d9a Harden BIST tests against changes in format().
Rik <rik@octave.org>
parents: 26376
diff changeset
911 %! format (old_spacing);
daa281e03d9a Harden BIST tests against changes in format().
Rik <rik@octave.org>
parents: 26376
diff changeset
912 %! end_unwind_protect
23348
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
913 %!error <only '\(\)' indexing is supported>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
914 %! m = containers.Map ("a", 1);
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
915 %! m{1};
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
916 %!error <unsupported KeyType>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
917 %! m1 = containers.Map ("KeyType", "cell", "ValueType", "any");
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
918 %!error <unsupported ValueType>
5ab7192f91d8 containers.Map: New key/value storage container (bug #49559).
Rik <rik@octave.org>
parents:
diff changeset
919 %! m1 = containers.Map ("KeyType", "char", "ValueType", "cell");
27431
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
920 %!error
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
921 %! m1 = containers.Map (1, 1);
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
922 %! m2 = containers.Map ("a", 2);
170d05ce158f Map.m: Allow numeric keys of one type to be used with a Map of a differing numeric keytype (bug #56594).
Guillaume Flandin
parents: 27067
diff changeset
923 %! m3 = [m1, m2];