-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructDiff.m
219 lines (211 loc) · 7.83 KB
/
StructDiff.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
%% Copyright (c) 2018-2018, Yichao Yu <[email protected]>
%
% This library is free software; you can redistribute it and/or
% modify it under the terms of the GNU Lesser General Public
% License as published by the Free Software Foundation; either
% version 3.0 of the License, or (at your option) any later version.
% This library is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% Lesser General Public License for more details.
% You should have received a copy of the GNU Lesser General Public
% License along with this library.
classdef StructDiff
properties(Hidden)
v1;
v2;
end
methods(Static, Access=private)
function res = compare(v1, v2)
try
s1 = isscalar(v1);
s2 = isscalar(v2);
if ischar(v1) && ischar(v2)
res = strcmp(v1, v2);
elseif ~s1 && ~s2
if ndims(v1) ~= ndims(v2) || ~all(size(v1) == size(v2))
res = false;
else
res = all(v1 == v2);
end
elseif s1 && s2
res = v1 == v2;
else
res = false;
end
catch
res = false;
end
end
function [d1, d2] = compute_real(v1, v2)
d1 = struct();
d2 = struct();
fns1 = fieldnames(v1);
for i = 1:length(fns1)
name = fns1{i};
f1 = v1.(name);
if ~isfield(v2, name)
d1.(name) = f1;
continue;
end
f2 = v2.(name);
if DynProps.isscalarstruct(f1) && DynProps.isscalarstruct(f2)
[df1, df2] = StructDiff.compute_real(f1, f2);
if ~isempty(fieldnames(df1))
d1.(name) = df1;
end
if ~isempty(fieldnames(df2))
d2.(name) = df2;
end
elseif ~StructDiff.compare(f1, f2)
d1.(name) = f1;
d2.(name) = f2;
end
end
fns2 = fieldnames(v2);
for i = 1:length(fns2)
name = fns2{i};
if ~isfield(v1, name)
d2.(name) = v2.(name);
end
end
end
% All printing should have a indent of at least `indent`.
% For `toplevel`, the first line already has an indent of `indent`,
% outputs without additional prefix should be outputed with an indent of
% `indent` (implicit for the first line)
% and additional indent is needed when new prefix is added/printed.
% For non `toplevel`, the first line already have prefix printed.
% Only additional prefix should be printed without new line.
% Outputs without additional prefix should be outputed after a new line
% with an indent of `indent` after.
% The output always ends with a new line.
function print(v1, v2, indent, toplevel)
spaces = [repmat(' ', 1, indent)];
if ~DynProps.isscalarstruct(v1) || ~DynProps.isscalarstruct(v2)
assert(~toplevel);
fprintf(':\n');
cprintf('red', '%s- %s\n', spaces, ...
YAML.sprint(v1, indent + 2, true));
cprintf('green', '%s+ %s\n', spaces, ...
YAML.sprint(v2, indent + 2, true));
return;
end
fns1 = fieldnames(v1);
fns2 = fieldnames(v2);
% If there's only one field difference and it's the same for both
% Print the single prefix and
% move on to print the field value under the longer prefix.
if length(fns1) == 1 && length(fns2) == 1 && strcmp(fns1{1}, fns2{1})
name = fns1{1};
if toplevel
fprintf('%s', name);
indent = indent + 2;
else
fprintf('.%s', name);
end
StructDiff.print(v1.(name), v2.(name), indent, false);
return;
end
has_output = false;
% More than one fields to print.
% First print the fields that only exist in one.
% Terminate the prefix line we are currently on with `:\n` if needed
% and print the diff with `+` or `-` prefix and additional indentation.
for i = 1:length(fns1)
name = fns1{i};
if isfield(v2, name)
continue;
end
if ~has_output
has_output = true;
if ~toplevel
fprintf(':\n%s', spaces)
end
else
fprintf('%s', spaces);
end
cprintf('red', '- %s:\n %s%s\n', name, spaces, ...
YAML.sprint(v1.(name), indent + 4, true));
end
for i = 1:length(fns2)
name = fns2{i};
if isfield(v1, name)
continue;
end
if ~has_output
has_output = true;
if ~toplevel
fprintf(':\n%s', spaces)
end
else
fprintf('%s', spaces);
end
cprintf('green', '+ %s:\n %s%s\n', name, spaces, ...
YAML.sprint(v2.(name), indent + 4, true));
end
for i = 1:length(fns1)
name = fns1{i};
if ~isfield(v2, name)
continue;
end
if ~has_output
has_output = true;
if ~toplevel
fprintf(':\n%s', spaces)
end
else
fprintf('%s', spaces);
end
fprintf('%s', name);
StructDiff.print(v1.(name), v2.(name), indent + 2, false);
end
if ~has_output
% I don't think this can actually happen....
fprintf('\n');
end
end
function v = get_struct(v)
if isa(v, 'ExpSeq')
v = v.C();
elseif isa(v, 'DynProps')
v = v();
elseif isa(v, 'SubProps') && isa(get_parent(v), 'DynProps')
v = v();
end
end
end
methods(Static)
function [d1, d2] = compute(v1, v2)
v1 = StructDiff.get_struct(v1);
v2 = StructDiff.get_struct(v2);
if ~DynProps.isscalarstruct(v1) || ~DynProps.isscalarstruct(v2)
error('Input must be scalar structures');
end
[d1, d2] = StructDiff.compute_real(v1, v2);
end
end
methods
function self = StructDiff(v1, v2)
[self.v1, self.v2] = StructDiff.compute(v1, v2);
end
function disp(self)
if isempty(fieldnames(self.v1)) && isempty(fieldnames(self.v2))
fprintf('StructDiff:\n ');
cprintf('blue', '<empty>\n');
else
fprintf('StructDiff: ');
cprintf('red', '-1 ');
cprintf('green', '+2');
fprintf('\n ');
StructDiff.print(self.v1, self.v2, 2, true);
end
end
function display(self, name)
if exist('name', 'var')
fprintf('%s = ', name);
end
disp(self);
end
end
end