-
Notifications
You must be signed in to change notification settings - Fork 2
/
DPmap.m
77 lines (74 loc) · 2.83 KB
/
DPmap.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
% subclass containers.Map to return -Inf as a default value
% also 2-dimensional map: dpmap(w,v) maps correctly
% note: uses 32-bit integers instead of 64
% future note: consider specializing to double values only
classdef DPmap < handle
properties (Constant)
default_val = -Inf;
display_default_val = false;
end
properties (SetAccess=private, GetAccess=private)
mapw;
end
methods
function this = DPmap()
% constructor
this.mapw = containers.Map('KeyType','int32','ValueType','any');
end
function val = subsref(this,S)
if ~strcmp(S.type,'()')
error('only () indexing supported');
end
if ~all(size(S.subs) == [1 2])
error('please index with two indices like so: dpmap(w,v)');
end
w = S.subs{1}; v = S.subs{2};
% ':' cases
if w == ':' && v == ':'
for keyw = cell2mat(keys(this.mapw))
mapv = this.mapw(keyw);
for keyv = cell2mat(keys(mapv))
fprintf('(%d,%d) = %d\n',keyw,keyv,mapv(keyv));%['(' keyw ',' keyv ') = ' mapv(keyv)]);
end
end
elseif v == ':'
mapv = this.mapw(w);
for key = cell2mat(keys(mapv))
fprintf('(%d,%d) = %d\n',w,key,mapv(key));
end
else
if ~this.isKey(w,v)
% NAH: return -Inf for negative entries, or if no value currently assigned
% any(S.subs < 0) ||
val = this.default_val;
else
% first subsref returns map of v->f, second returns f
mapv = this.mapw(w);
val = mapv(v);
end
end % if
end % function subsref
function this = subsasgn(this, S, B)
if ~strcmp(S.type,'()')
error('only () indexing supported');
end
if ~all(size(S.subs) == [1 2])
error('please index with two indices like so: dpmap(w,v)');
end
if B == this.default_val && ~this.display_default_val
% sparse representation; don't assign default value
return
end
w = S.subs{1}; v = S.subs{2};
if ~this.mapw.isKey(w)
this.mapw(w) = containers.Map('KeyType','int32','ValueType','any');
end
mapv = this.mapw(w);
mapv(v) = B; %#ok<NASGU> this achieves a desired side effect
end
function res = isKey(this,w,v)
% Returns 1 if (w,v) was previously assigned; otherwise 0
res = this.mapw.isKey(w) && this.mapw(w).isKey(v);
end
end
end