Il suffit de manipuler des structures de pointeurs sur fonctions !

voila un exemple, avec une petite classe implémentant une table de hashage :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function h = myhashtable()
 
this = struct('keys', {{}}, 'values', {{}});
 
h = struct('get',@get_, ...
    'getKey',@getKey, ...
    'add',@add , ...
    'remove',@remove, ...
    'isKey',@iskey, ...
    'isValue',@isvalue, ...
    'isEmpty',@isempty_);
 
 
    function idx = add(key, value)
        if isempty(this.keys)
            this.keys{end+1}   = key;
            this.values{end+1} = value;
            idx = length(this.keys);
            return
        end
        [v, idx] = get_(key);
        if isempty(idx)
            this.keys{end+1}   = key;
            this.values{end+1} = value;
            idx = length(this.keys);
        else
            this.values{idx} = value;
        end
    end
 
    function [value, idx] = get_(key)
        if nargin == 0
            value = {this.keys,this.values};
            return
        end
        value = [];
        idx = strmatch(key,this.keys);
        if ~isempty(idx)
            value = this.values(idx);
        end
    end
 
    function [key, idx] = getKey(value)
        if nargin == 0
            idx = this.values;
            key = this. keys;
            return
        end
        key = [];
        idx = strmatch(value,this.values);
        if ~isempty(idx)
            key = this.keys(idx);
        end
    end
 
    function idx = remove(key)
        [v,idx] = get_(key);
        if ~isempty(idx)
            this.keys{idx} = [];
            this.values{idx} = [];
        end
    end
 
    function out = iskey(key)
        if nargin == 0
            out = 0; 
            return;
        end
        out = strmatch(key,this.keys);
    end
 
    function out = isvalue(val)
        if nargin == 0
            out = 0; return;
        end
        out = strmatch(val,this.values);
    end
 
    function out = isempty_()
        if isempty(h.values) && isempty(h.keys)
            out = 1;
        else
            out = 0;
        end
    end
 
 
end
Exemple d'utilisation :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
myhash = myhashtable
myhash.add('a',1);
myhash.add('b',2);
[v,k] = myhash.get()