2015-12-31 22:43:55 +03:00
|
|
|
classdef type_utils
|
2015-02-08 10:13:41 +03:00
|
|
|
properties (Constant = true)
|
|
|
|
nil = types.Nil();
|
|
|
|
end
|
2015-02-08 22:18:08 +03:00
|
|
|
|
|
|
|
methods(Static)
|
|
|
|
function ret = equal(a,b)
|
|
|
|
ret = false;
|
|
|
|
ota = class(a); otb = class(b);
|
2015-02-10 08:20:23 +03:00
|
|
|
if ~(strcmp(ota,otb) || ...
|
2015-12-31 22:43:55 +03:00
|
|
|
(type_utils.sequential_Q(a) && type_utils.sequential_Q(b)))
|
2015-02-08 22:18:08 +03:00
|
|
|
return;
|
|
|
|
end
|
|
|
|
switch (ota)
|
2015-02-10 08:20:23 +03:00
|
|
|
case {'types.List', 'types.Vector'}
|
2015-02-08 22:18:08 +03:00
|
|
|
if ~(length(a) == length(b))
|
2015-02-10 08:20:23 +03:00
|
|
|
return;
|
2015-02-08 22:18:08 +03:00
|
|
|
end
|
|
|
|
for i=1:length(a)
|
2015-12-31 22:43:55 +03:00
|
|
|
if ~(type_utils.equal(a.get(i), b.get(i)))
|
2015-02-10 08:20:23 +03:00
|
|
|
return;
|
2015-02-08 22:18:08 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
ret = true;
|
2015-11-30 23:09:38 +03:00
|
|
|
case 'types.HashMap'
|
|
|
|
if ~(length(a) == length(b))
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
ks1 = a.keys();
|
|
|
|
for i=1:length(ks1)
|
|
|
|
k = ks1{i};
|
2015-12-31 22:43:55 +03:00
|
|
|
if ~(b.data.isKey(k))
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
if ~(type_utils.equal(a.data(k), b.data(k)))
|
2015-11-30 23:09:38 +03:00
|
|
|
return;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ret = true;
|
2015-02-08 22:18:08 +03:00
|
|
|
case 'char'
|
|
|
|
ret = strcmp(a,b);
|
|
|
|
otherwise
|
|
|
|
ret = a == b;
|
|
|
|
end
|
|
|
|
end
|
2015-02-10 08:20:23 +03:00
|
|
|
|
|
|
|
function ret = sequential_Q(obj)
|
|
|
|
ret = strcmp(class(obj), 'types.List') || ...
|
|
|
|
strcmp(class(obj), 'types.Vector');
|
|
|
|
end
|
|
|
|
|
|
|
|
function ret = list_Q(obj)
|
|
|
|
ret = strcmp(class(obj), 'types.List');
|
|
|
|
end
|
|
|
|
function ret = vector_Q(obj)
|
|
|
|
ret = strcmp(class(obj), 'types.Vector');
|
|
|
|
end
|
|
|
|
function ret = hash_map_Q(obj)
|
|
|
|
ret = strcmp(class(obj), 'types.HashMap');
|
|
|
|
end
|
|
|
|
|
|
|
|
function ret = keyword(str)
|
2015-12-31 22:43:55 +03:00
|
|
|
if type_utils.keyword_Q(str)
|
2015-11-30 23:09:38 +03:00
|
|
|
ret = str;
|
|
|
|
else
|
2015-12-31 22:43:55 +03:00
|
|
|
ret = sprintf('%c%s', 0xff, str);
|
2015-11-30 23:09:38 +03:00
|
|
|
end
|
2015-02-10 08:20:23 +03:00
|
|
|
end
|
|
|
|
function ret = keyword_Q(obj)
|
2015-12-31 22:43:55 +03:00
|
|
|
ret = length(obj) > 1 && strcmp(obj(1), sprintf('%c', 0xff));
|
|
|
|
end
|
|
|
|
|
|
|
|
function print_stack(err)
|
|
|
|
for i=1:numel(err.stack)
|
|
|
|
stack = err.stack(i);
|
|
|
|
fprintf(' %s at line %d column %d (%s)\n', ...
|
|
|
|
stack.name, stack.line, stack.column, stack.file);
|
|
|
|
end
|
2015-02-10 08:20:23 +03:00
|
|
|
end
|
2015-02-08 22:18:08 +03:00
|
|
|
end
|
2015-02-08 10:13:41 +03:00
|
|
|
end
|
|
|
|
|