Mercurial > repos > public > sbplib
comparison Map.m @ 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 | effd75b113ba |
children | a6c5e73ff44e |
comparison
equal
deleted
inserted
replaced
419:1adfc42bbc1a | 421:b6a5dc423990 |
---|---|
1 classdef Map < handle | |
2 properties | |
3 map | |
4 end | |
5 | |
6 % can we support multi map using varargin? | |
7 % probably a bad idea. For example it complicates keys(); | |
8 | |
9 methods | |
10 function obj = Map() | |
11 obj.map = containers.Map(); | |
12 end | |
13 | |
14 function set(obj, k, v) | |
15 keyByteStream = getByteStreamFromArray(k); | |
16 | |
17 obj.map(char(keyByteStream)) = v; | |
18 end | |
19 | |
20 function v = get(obj, k) | |
21 keyByteStream = getByteStreamFromArray(k); | |
22 | |
23 v = obj.map(char(keyByteStream)); | |
24 end | |
25 | |
26 function b = isKey(obj, k) | |
27 keyByteStream = getByteStreamFromArray(k); | |
28 b = obj.map.isKey(char(keyByteStream)); | |
29 end | |
30 | |
31 function c = keys(obj) | |
32 keyByteStreams = obj.map.keys; | |
33 | |
34 n = length(keyByteStreams); | |
35 | |
36 c = cell(1, n); | |
37 for i = 1:n | |
38 c{i} = getArrayFromByteStream(uint8(keyByteStreams{i})); | |
39 end | |
40 end | |
41 | |
42 function l = length(obj) | |
43 l = obj.map.length; | |
44 end | |
45 | |
46 function remove(obj, k) | |
47 keyByteStream = getByteStreamFromArray(k); | |
48 obj.map.remove(char(keyByteStream)); | |
49 end | |
50 | |
51 function s = size(obj) | |
52 s = obj.map.size; | |
53 end | |
54 | |
55 function c = values(obj) | |
56 c = obj.map.values; | |
57 end | |
58 | |
59 function v = subsref(obj, S) | |
60 switch S(1).type | |
61 case '()' | |
62 k = S.subs{1}; | |
63 try | |
64 v = get(obj, k); | |
65 catch ME | |
66 if strcmp(ME.identifier,'MATLAB:Containers:Map:NoKey') | |
67 error('Reference to non-existent entry %s',toString(S.subs)); | |
68 else | |
69 throw(ME); | |
70 end | |
71 end | |
72 otherwise | |
73 try | |
74 v = builtin('subsref', obj, S); | |
75 catch e | |
76 error('You can''t use dot notation for this because Matlab(TM). What is this piece of shit software anyway?') | |
77 end | |
78 end | |
79 end | |
80 | |
81 function obj = subsasgn(obj, S, v); | |
82 switch S(1).type | |
83 case '()' | |
84 k = S.subs{1}; | |
85 set(obj, k, v); | |
86 otherwise | |
87 error('You can''t use dot notation because Matlab(TM). What is this piece of shit software anyway?') | |
88 end | |
89 end | |
90 end | |
91 end |