-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapNested.m
331 lines (284 loc) · 11.9 KB
/
MapNested.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
classdef MapNested < containers.Map & handle
% A nested map container
%
% A MapNested object implements nested maps (map of maps).
%
% MapN is a handle class.
% Since MapNested is a subclass from containers.Map, all functions from
% containers.Map will be inherited (eg. keys, values, ...)
%
% Description - basic outline
% ---------------------------
%
% A MapNested object is constructed like this:
%
% M = MapNested();
%
% Values are stored using M(key1, key2, ...) = value, for example:
%
% M(1, 'a') = 'a string value';
% M(1, 'b') = 287.2;
% M(2) = [1 2 3; 4 5 6];
%
% Attention: you can not assign cell-arrays in this implementation:
% M(2, 'x', pi) = {'a' 'cell' 'array'}; %throws error
%
% another possibility is to define the keys as cell-array like:
% M({key1, key2, key3}) = value;
%
%
% Values are retrieved using M(key1, key2, ...), for example
%
% v = M(1, 'b');
% u = M(2);
%
% or with using cell-arrays for the keys
% v = M({key1, key2, key3});
%
% Set and get - methods
% -----------------------------
%
% for setting and retrieving values there are also two methods
% implemented, for setting a value:
%
% MapObj = setValueNested(MapObj, {key1, key2, key3, ...}, value);
%
% here the second input parameter has to be a cell-array with the
% keys.
%
% For retrieving values one can use:
% value = getValueNested(MapObj, {key1, key2, key3,...});
%
% here the second input parameter has to be a cell-array with the
% keys.
%
%
% Updating and removing entries
% -----------------------------
%
% The value for a given key list is updated using the usual assignment;
% the previous value is overwritten.
%
% M(pi, 'x') = 1; % 1 is current value
% M(pi, 'x') = 2; % 2 replaces 1 as the value for this key list
%
% NMapobj('Key') = []; %removes the key
% remove(NMapobj, 'Key'); %also removes the key
%
% check if is a specific key in the map
% -------------------------------------
% M.isKey(keyList) %keyList is a cell array of the nested keys
% M.isKey(Key1, Key2, Key3,..) %Key1, Key2, Key3 are the nested keys
% isKey(M, keyList) %keyList is a cell array of the nested keys
% isKey(M, Key1, Key2, Key3,..) %Key1, Key2, Key3 are the nested keys
%
%
% Methods and properties
% ----------------------
%
% MapN methods:
% MapNested - constructor for MapNested objects
% subsref - implements value = Mobj(keylist)
% subsasgn - implements M(keylist) = value
% setValueNested - implements Mobj = setValueNested(Mobj, keyList,
% value)
% getValueNested - implements value = getValueNested(Mobj, keyList);
% isKey - implements Mobj.iskey(keyList);
%
% See also: containers.Map
% (c) 2017, Roland Ritt
methods
function obj = MapNested(varargin)
% constructor, calls Superclass-constructor with varargin;
obj = [email protected](varargin{:});
end
function obj = setValueNested(obj, keyList, value)
% method which recursively constructs a map of maps and add
% a a value at the specified keys
%
% Implements the syntax
%
% MapNestedObj = setValueNested(MapNestedObj, keyList,
% value);
% (keyList is of type 'CellArray')
%
% See also: MapNested, MapNested/setValueNested
if ~iscell(keyList)
%check if the keyList is a cell array
error('second input (keylist) is no cellArray');
end
KeyType = class(keyList{1});
%find the class-type of first key
if length(keyList)==1
% check if the object is defined
if isempty(obj)
%if the object does not exist...
obj = MapNested('KeyType', KeyType,'ValueType', 'any');
% generate a new MapNested-object with the specified
% key-Type, and default ValueType set to 'any'
end
if isempty(value)
remove(obj, keyList{1});
else
obj = [obj;MapNested(keyList{1},value)];
end
return
else
% if more than one key
if obj.isKey(keyList{1})
% check if the key is in the map
temp = values(obj, keyList(1));
temp = temp{1};
% retrieve the MapNested-object from the map
if ~isa(temp ,'containers.Map')
% if the value is not a MapObject, generate a new
% one
temp = MapNested('KeyType', KeyType,'ValueType', 'any');
end
else
% if the key does not exist, generate a new
% MapNested-object
temp = MapNested('KeyType', KeyType,'ValueType', 'any');
end
temp = setValueNested(temp, keyList(2:end), value);
% set the value in the MapNested-object (recursive call)
if isempty(obj)
%if obj is empty, generate a new MapNested-object
obj = MapNested(keyList{1}, temp);
else
%if obj exists, concatenate the new entry
obj = [obj;MapNested(keyList{1}, temp)];
end
end
end
function value = getValueNested(obj, keyList)
% method for retrieving values from a MapNested object by
% recursively calls to this method
%
% Implements the syntax
%
% value = getValueNested(MapNestedObj, keyList, value);
% (keyList is of type 'CellArray')
%
% See also: MapNested, MapNested/setValueNested
if ~iscell(keyList)
%check if the keyList is a cellArray
error('second input is no cellArray');
end
if ~obj.isKey(keyList{1})
%check if the first key is in the list, if not, throw an
%error (maybe should be changed to empty object)
error(['key ''', keyList{1}, ''' is not a key'] );
end
if length(keyList)==1
% if the keyList is only of length 1, return the value of
% this list
value = values(obj, {keyList{1}});
% call the method from the superclass
value=value{1}; %retrieve the value
return
else
% if there are more than one keys Left in the keyList
temp = values(obj, {keyList{1}});
% retrieve the MapNested object (key1)
temp = temp{1};
if ~isa(temp ,'containers.Map')
% if the retrieved value is not a Map, the
% MapNested object was misused --> throw error message
error(['key ''', keyList{2}, ''' is not a key'] );
end
value = getValueNested(temp, keyList(2:end));
% recursively call the getValueNested method.
end
end
function v = subsref(M, S)
% returns value associated with key list
%
% Implements the syntax
%
% value = MapNobj(key1, key2, ...)
%
% See also: MapNested, MapNested/subsasgn
switch S(1).type
case '.'
v = builtin('subsref',M,S);
case '()'
% if ~isscalar(S) || ~strcmp(S.type, '()') || length(S)<1
% error('MapNested:Subsref:LimitedIndexing', ...
% 'Only ''()'' indexing is supported by a MapNested');
% end
try
if iscell(S(1).subs{1})
temp = S(1).subs{1};
else
temp = S(1).subs;
end
v = getValueNested(M, temp);
catch me
% default is handled in subsrefError for efficiency
error('MapNested:Subsref:IndexingError', ...
['Something went wrong in indexing: ',me(1).message] );
end
if length(S)>1
v = subsref(v, S(2:end));
end
case '{}'
error('MapNested:Subsasgn:LimitedIndexing', ...
'''{}'' indexing is not supported by a MapNested');
otherwise
error('Not a valid indexing expression')
end
end
function M = subsasgn(M, S, v)
% sets value associated with key list
%
% Implements the syntax
%
% MapNobj(key1, key2, ...) = value
%
% See also: MapNested, MapNested/subsasgn
if ~isscalar(S) || ~strcmp(S.type, '()') || length(S)<1
error('MapNested:Subsasgn:LimitedIndexing', ...
'Only ''()'' indexing is supported by a MapNested');
end
try
if iscell(S.subs{1})
temp = S.subs{1};
else
temp = S.subs;
end
M = setValueNested(M, temp, v);
catch me
% default is handled in subsrefError for efficiency
error('MapNested:Subsasgn:IndexingError', ...
'Something went wrong in indexing');
end
end
function bisKey = isKey(M, varargin)
%% test
if length(varargin)==1
keyList = varargin{1};
else
keyList = varargin;
end
if iscell(keyList)
if length(keyList)== 1
bisKey = [email protected](M,keyList{1});
return
else
KeyTemp = keyList(1);
if M.isKey(KeyTemp)
Mtemp = M.getValueNested(KeyTemp);
bisKey = isKey(Mtemp, keyList(2:end));
else
bisKey = false;
end
return;
end
else
bisKey = [email protected](M,varargin{1});
return;
end
end
end
end