1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 18:18:51 +03:00
mal/matlab/Dict.m
Joel Martin 4769962955 matlab: support Octave 4.0.0
- Workarounds for Octave 4.0.0:
    - no containers.Map yet so use new Dict
      structure when running under Octave.
    - weird error when +FOO/ directory and FOO.m file both exist so
      rename types.m to type_utils.m
    - no getReport so implement custom stack printer
    - workaround weird issue that happens when a class initializer is
      called with the first argument of the same class as per
      Env(outer, ...). The class is not properly initialized. So for
      now, wrap the outer in a cell as Env({outer}, ...)
    - missing MException object types so when in Octave, use a global
      variable to store the error object.
    - missing native2unicode so just use 0xff character for keyword
      prefix.
    - workaround some function calling/passing differences. For
      example, the "@" anonymous function symbol does not seem to work
      for conveying an existing function but only for defininign a new
      one. E.g. this works:
          @(a,b) a+b
      but this doesn't
          @my_adder_fn.
      so just do this:
          @(a,b) my_adder_fn(a,b)

- Add Dockerfile with Octave

- Active Travis for matlab implementation using Octave
2015-12-31 13:55:18 -06:00

62 lines
2.0 KiB
Matlab

% Implement containers.Map like structure
% This only applies to GNU Octave and will break in Matlab when
% arbitrary string keys are used.
classdef Dict < handle
properties
data
end
methods
function dict = Dict(keys, values)
dict.data = struct();
if nargin > 0
for i=1:length(keys)
dict.data.(keys{i}) = values{i};
end
end
end
function ret = subsasgn(dict, ind, val)
dict.data.(ind(1).subs{1}) = val;
ret = dict;
end
function ret = subsref(dict, ind)
if strcmp('.', ind(1).type)
% Function call
switch ind(1).subs
case 'isKey'
if numel(ind) > 1
ret = isfield(dict.data, ind(2).subs{1});
else
error('Dict:invalidArgs', ...
sprintf('''%s'' called with no arguments', ind(1).subs));
end
case 'keys'
ret = fieldnames(dict.data);
case 'values'
ret = {};
keys = fieldnames(dict.data);
for i=1:length(keys)
ret{end+1} = dict.data.(keys{i});
end
case 'remove'
if numel(ind) > 1
if numel(ind(2).subs) > 0
dict.data = rmfield(dict.data, ind(2).subs{1});
end
else
error('Dict:invalidArgs', ...
sprintf('''%s'' called with no arguments', ind(1).subs));
end
otherwise
error('Dict:notfound', ...
sprintf('''%s'' not found', ind(1).subs));
end
else
% Key lookup
ret = dict.data.(ind(1).subs{1});
end
end
end
end