changeset 415:16907bf31e67 feature/better_map

Overload indexing operations.
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 02 Feb 2017 16:22:47 +0100
parents 50fd7e88aa74
children e97550b5c1e7
files Map.m
diffstat 1 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Map.m	Thu Feb 02 15:57:43 2017 +0100
+++ b/Map.m	Thu Feb 02 16:22:47 2017 +0100
@@ -54,5 +54,33 @@
         function c = values(obj)
             c = obj.map.values;
         end
+
+        function v = subsref(obj, S)
+            switch S.type
+                case '()'
+                    k = S.subs;
+                    try
+                        v = obj.get(k);
+                    catch ME
+                        if strcmp(ME.identifier,'MATLAB:Containers:Map:NoKey')
+                            error('Reference to non-existent entry %s',toString(S.subs));
+                        else
+                            throw(ME);
+                        end
+                    end
+                otherwise
+                    v = builtin('subsref', obj, S);
+            end
+        end
+
+        function obj = subsasgn(obj, S, v);
+            switch S.type
+                case '()'
+                    k = S.subs;
+                    obj.set(k, v);
+                otherwise
+                    error('Unsupported indexing operator: %s',S.type);
+            end
+        end
     end
 end