changeset 40:54d3ab296ba0

Added Dictionary class. Added string conversions for a bunch of types. Deprecated some replaced functions.
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 12 Oct 2015 10:38:53 +0200
parents 7249f105e67b
children 910a05dcdfdf
files +noname/calculateSolution.m Dictionary.m printExpr.m printSize.m print_issparse.m struct2string.m struct2syntax.m toString.m
diffstat 8 files changed, 189 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/+noname/calculateSolution.m	Fri Oct 09 10:52:42 2015 +0200
+++ b/+noname/calculateSolution.m	Mon Oct 12 10:38:53 2015 +0200
@@ -5,7 +5,7 @@
 %    order     -- order of accuracy of the approximtion
 %    T         -- time to calculate solution for
 %    input paramters m, t, order may all be vectors.
-function [] = calculateSolution(filename, discrHand, method, m, T, order, force_flag)
+function [] = calculateSolution(filename, discrHand, method, m, T_in, order, force_flag)
     default_arg('force_flag',false);
 
     if exist(filename,'file') && ~force_flag
@@ -19,16 +19,16 @@
 
     sf = SolutionFile(filename);
 
-    % Make sure times are sorted
-    T = sort(T);
 
 
     orderWidth = findFieldWidth('%d',order);
     mWidth = findFieldWidth('%d',m);
-    TWidth = findFieldWidth('%d',T);
+    TWidth = findFieldWidth('%d',T_in);
 
     for i = 1:length(order)
         for j = 1:length(m)
+            T = sort(T_in); % Make sure times are sorted
+
             discr = discrHand(m(j),order(i));
             k_max = discr.getTimestep(method);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Dictionary.m	Mon Oct 12 10:38:53 2015 +0200
@@ -0,0 +1,110 @@
+% TODO
+%   Documentation
+%   Should the storage be done in some other way?
+%   Cell array as keys?
+%   Some possibility to load and save to a matfile?
+%       May be load and save from outside is enough.
+%   Get and set functions called by subsref and subsasgn?
+%   Unit tests.
+
+
+classdef Dictionary
+    properties
+        store
+    end
+
+    methods
+        function obj = Dictionary()
+            obj.store = struct();
+        end
+
+        function s = getStore(obj)
+            s = obj.store;
+        end
+
+        function display(obj)
+            if length(fieldnames(obj.store)) == 0
+                fprintf('%s is an empty Dictionary\n',inputname(1));
+                return
+            end
+
+            lineformat = [inputname(1) '(%s) = %s\n'];
+
+            display_impl(obj.store,'');
+
+            function display_impl(s, path)
+                if ~isstruct(s)
+                    fprintf(lineformat,path(3:end), value2str(s));
+                    % fprintf(') = %s\n', value2str(s));
+                    % fprintf('%s(', objName);
+                    return
+                end
+
+                fn = fieldnames(s);
+
+                for i = 1:length(fn)
+                    display_impl(s.(fn{i}), [path ', ' fn{i}(2:end)]);
+                end
+
+            end
+
+            function str = value2str(val)
+                if isnumeric(val) || ischar(val)
+                    str = mat2str(val);
+                else
+                    str = class(val);
+                end
+            end
+        end
+
+        % B = obj(i)
+        function B = subsref(obj,S)
+            switch S.type
+                case '()'
+                    Sf = obj.subs2dotSubs(S);
+                    try
+                        B = subsref(obj.store,Sf);
+                    catch ME
+                        if strcmp(ME.identifier,'MATLAB:nonExistentField')
+                            error('Reference to non-existent entry %s',toString(S.subs));
+                        else
+                            throw(ME);
+                        end
+                    end
+                otherwise
+                    error('Unsupported indexing operator: %s',S.type);
+            end
+        end
+
+         % A(i) = B
+        function obj = subsasgn(obj,S,B);
+            switch S.type
+                case '()'
+                    Sf = obj.subs2dotSubs(S);
+                    obj.store = subsasgn(obj.store,Sf,B);
+                otherwise
+                    error('Unsupported indexing operator: %s',S.type);
+            end
+        end
+
+        function Sf = subs2dotSubs(obj,S)
+            for i = 1:length(S.subs)
+                Sf(i).type = '.';
+                Sf(i).subs = obj.getFieldname(S.subs{i});
+            end
+        end
+
+        % Should probably use mat2str with some kind of normalization to make all variables valied fieldname
+        %  and make it possible to recover the value
+        function fName = getFieldname(obj, val)
+            if isnumeric(val)
+                valStr = num2str(val);
+            elseif ischar(val)
+                valStr = val;
+            else
+                error('Dont know what to do with val!');
+            end
+            fName = sprintf('f%s',valStr);
+        end
+    end
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/printExpr.m	Mon Oct 12 10:38:53 2015 +0200
@@ -0,0 +1,4 @@
+function printExpr(expr)
+    result = evalin('caller',expr);
+    fprintf('%s => %s\n', expr, toString(result));
+end
\ No newline at end of file
--- a/printSize.m	Fri Oct 09 10:52:42 2015 +0200
+++ b/printSize.m	Mon Oct 12 10:38:53 2015 +0200
@@ -1,4 +1,5 @@
 function printSize(A)
+    warning('Deprecated! Use printExpr() instead!');
     s = size(A);
     fprintf('%8s has size: [%d, %d]\n',inputname(1),s(1),s(2));
 end
\ No newline at end of file
--- a/print_issparse.m	Fri Oct 09 10:52:42 2015 +0200
+++ b/print_issparse.m	Mon Oct 12 10:38:53 2015 +0200
@@ -1,4 +1,5 @@
 function print_issparse(A)
+    warning('Deprecated! Use printExpr() instead!');
     b = issparse(A);
     if b
         s = 'true';
--- a/struct2string.m	Fri Oct 09 10:52:42 2015 +0200
+++ b/struct2string.m	Mon Oct 12 10:38:53 2015 +0200
@@ -1,4 +1,5 @@
 function str = struct2string(s)
+    warning('Deprecated! Use toString() instead!');
     fn = fieldnames(s);
 
     if length(fn) == 0
@@ -16,10 +17,12 @@
     str = [str sprintf('%s: %s}',fn{end}, valueString(value))];
 end
 
-function str  = valueString(value)
-    if ischar(value)
-        str = ['''' value ''''];
+function str  = value2string(value)
+    if isnumeric(value) || ischar(value)
+        str = mat2str(value);
+    elseif isstruct(value)
+        str = struct2string(value);
     else
-        str = mat2str(value);
+        str = 'NO_STR_REP';
     end
 end
\ No newline at end of file
--- a/struct2syntax.m	Fri Oct 09 10:52:42 2015 +0200
+++ b/struct2syntax.m	Mon Oct 12 10:38:53 2015 +0200
@@ -1,4 +1,5 @@
 function str = struct2syntax(s)
+    warning('Deprecated! Use toString() instead!');
     fn = fieldnames(s);
 
     if length(fn) == 0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/toString.m	Mon Oct 12 10:38:53 2015 +0200
@@ -0,0 +1,61 @@
+% Takes a value and returns a string representation of that value.
+% If syntaxFlag is true, a string with valid matlab syntax is returned.
+function str = toString(value, syntaxFlag)
+    default_arg('syntaxFlag',false);
+
+    if syntaxFlag
+        error('Not supported yet.')
+    end
+
+    str = value2string(value);
+end
+
+function str  = value2string(value)
+    if isnumeric(value) || ischar(value)
+        str = mat2str(value);
+    elseif isstruct(value)
+        str = struct2string(value);
+    elseif iscell(value)
+        str = cell2string(value);
+    elseif isa('function_hande')
+        str = func2str(value);
+    else
+        warning('No string representation for class ''%s''', class(value))
+        str = 'NO_STR_REP';
+    end
+end
+
+function str = cell2string(c)
+    len = length(c);
+
+    if len == 0
+        str = '{}';
+        return
+    end
+
+    str = '{';
+
+    for i =1:len-1
+        str = [str sprintf('%s, ', value2string(c{i}))];
+    end
+    str = [str sprintf('%s}', value2string(c{end}))];
+end
+
+function str = struct2string(s)
+    fn = fieldnames(s);
+
+    if length(fn) == 0
+        str = '{}';
+        return
+    end
+
+    str = '{';
+
+    for i = 1:length(fn) - 1
+        value = s.(fn{i});
+        str = [str sprintf('%s: %s, ',fn{i}, value2string(value))];
+    end
+    value = s.(fn{end});
+    str = [str sprintf('%s: %s}',fn{end}, value2string(value))];
+end
+