changeset 421:b6a5dc423990

Merged in feature/better_map (pull request #8) Feature/better map
author Jonatan Werpers <jonatan.werpers@it.uu.se>
date Tue, 07 Feb 2017 14:38:51 +0000
parents 1adfc42bbc1a (current diff) 8bd2e36f1f8b (diff)
children 38173ea263ed a2cb0d4f4a02 5f4540e13f9b 0bc37a25ed88
files
diffstat 2 files changed, 188 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Map.m	Tue Feb 07 14:38:51 2017 +0000
@@ -0,0 +1,91 @@
+classdef Map < handle
+    properties
+        map
+    end
+
+    % can we support multi map using varargin?
+    % probably a bad idea. For example it complicates keys();
+
+    methods
+        function obj = Map()
+            obj.map = containers.Map();
+        end
+
+        function set(obj, k, v)
+            keyByteStream = getByteStreamFromArray(k);
+
+            obj.map(char(keyByteStream)) = v;
+        end
+
+        function v = get(obj, k)
+            keyByteStream = getByteStreamFromArray(k);
+
+            v = obj.map(char(keyByteStream));
+        end
+
+        function b = isKey(obj, k)
+            keyByteStream = getByteStreamFromArray(k);
+            b = obj.map.isKey(char(keyByteStream));
+        end
+
+        function c = keys(obj)
+            keyByteStreams = obj.map.keys;
+
+            n = length(keyByteStreams);
+
+            c = cell(1, n);
+            for i = 1:n
+                c{i} = getArrayFromByteStream(uint8(keyByteStreams{i}));
+            end
+        end
+
+        function l = length(obj)
+            l = obj.map.length;
+        end
+
+        function remove(obj, k)
+            keyByteStream = getByteStreamFromArray(k);
+            obj.map.remove(char(keyByteStream));
+        end
+
+        function s = size(obj)
+            s = obj.map.size;
+        end
+
+        function c = values(obj)
+            c = obj.map.values;
+        end
+
+        function v = subsref(obj, S)
+            switch S(1).type
+                case '()'
+                    k = S.subs{1};
+                    try
+                        v = get(obj, 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
+                    try
+                        v = builtin('subsref', obj, S);
+                    catch e
+                        error('You can''t use dot notation for this because Matlab(TM). What is this piece of shit software anyway?')
+                    end
+            end
+        end
+
+        function obj = subsasgn(obj, S, v);
+            switch S(1).type
+                case '()'
+                    k = S.subs{1};
+                    set(obj, k, v);
+                otherwise
+                    error('You can''t use dot notation because Matlab(TM). What is this piece of shit software anyway?')
+            end
+        end
+    end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MapTest.m	Tue Feb 07 14:38:51 2017 +0000
@@ -0,0 +1,97 @@
+function tests = MatTest()
+    tests = functiontests(localfunctions);
+end
+
+function kvp = getKeyValuePairs()
+    kvp = {
+        {1,3},1;
+        struct(), [1; 3; 4];
+        [1,2; 4 3], struct();
+        'Hej', struct('lol', 6);
+        0, 'Nej';
+    };
+end
+
+function testSetAndGet(testCase)
+    keyValuePairs = getKeyValuePairs();
+
+    map = Map();
+
+    % Insert
+    for i = 1:length(keyValuePairs)
+        map(keyValuePairs{i,1}) = keyValuePairs{i,2};
+    end
+
+    % Validate output
+    for i = 1:length(keyValuePairs)
+        v = map(keyValuePairs{i,1});
+        testCase.verifyEqual(v, keyValuePairs{i,2});
+    end
+end
+
+function map = exampleMap()
+    keyValuePairs = getKeyValuePairs();
+
+    map = Map();
+
+    % Insert
+    for i = 1:length(keyValuePairs)
+        map(keyValuePairs{i,1}) = keyValuePairs{i,2};
+    end
+end
+
+function testLength(testCase)
+    map = Map();
+    testCase.verifyEqual(map.length, 0);
+
+    map = exampleMap();
+    testCase.verifyEqual(map.length, 5)
+end
+
+
+function testIsKey(testCase)
+    map = exampleMap();
+
+    keyValuePairs = getKeyValuePairs();
+    keys = keyValuePairs(:,1);
+
+    for i = 1:length(keys)
+        testCase.verifyTrue(map.isKey(keys{i}));
+    end
+
+    notKeys = {
+        'hej',
+        [],
+        1,
+        {2,5},
+    };
+
+    for i = 1:length(notKeys)
+        testCase.verifyFalse(map.isKey(notKeys{i}));
+    end
+end
+
+
+function testRemove(testCase)
+    map = exampleMap();
+
+    remove(map, struct());
+
+    testCase.verifyFalse(map.isKey(struct()));
+end
+
+% function testValues(testCase)
+%     keyValuePairs = getKeyValuePairs();
+
+%     map = exampleMap();
+
+%     testCase.verifyEqual(values(map), keyValuePairs(:,2)');
+% end
+
+% function testKeys(testCase)
+%     keyValuePairs = getKeyValuePairs();
+
+%     map = exampleMap();
+
+%     testCase.verifyEqual(keys(map), keyValuePairs(:,1)');
+% end