aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/util/weak_map.vala
blob: a7f4bc44e624c7ce60168560ce169fafe6ae0826 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Gee;

public class WeakMap<K, V> : Gee.AbstractMap<K, V> {

    private HashMap<K, weak V> hash_map;
    private HashMap<K, WeakNotifyWrapper> notify_map;

    public HashDataFunc<K>? key_hash_func = null;
    public EqualDataFunc<K>? key_equal_func = null;
    public EqualDataFunc<V>? value_equal_func = null;

    public WeakMap(owned HashDataFunc<K>? key_hash_func = null, owned EqualDataFunc<K>? key_equal_func = null, owned EqualDataFunc<V>? value_equal_func = null) {
        if (!typeof(V).is_object()) {
            error("WeakMap only takes values that are Objects");
        }

        this.key_hash_func = (owned)key_hash_func;
        this.key_equal_func = (owned)key_equal_func;
        this.value_equal_func = (owned)value_equal_func;

        if (this.key_equal_func == null || this.key_equal_func == null || this.value_equal_func == null) {
            hash_map = new HashMap<K, weak V>();
            notify_map = new HashMap<K, WeakNotifyWrapper>();
        } else {
            hash_map = new HashMap<K, weak V>((v) => { return this.key_hash_func != null ? this.key_hash_func(v) : 0; },
                    (a, b) => { return this.key_equal_func != null ? this.key_equal_func(a, b) : a == b; },
                    (a, b) => { return this.value_equal_func != null ? this.value_equal_func(a, b) : a == b; });
            notify_map = new HashMap<K, WeakNotifyWrapper>((v) => { return this.key_hash_func != null ? this.key_hash_func(v) : 0; },
                    (a, b) => { return this.key_equal_func != null ? this.key_equal_func(a, b) : a == b; });
        }
    }

    public override void clear() {
        foreach (K key in notify_map.keys) {
            Object o = (Object) hash_map[key];
            o.weak_unref(notify_map[key].func);
        }
        hash_map.clear();
        notify_map.clear();
    }

    public override V @get(K key) {
        if (!hash_map.has_key(key)) return null;

        var v = hash_map[key];

        return (owned) v;
    }

    public override bool has(K key, V value) {
        return has_key(key) && (this.value_equal_func != null ? this.value_equal_func(hash_map[key], value) : hash_map[key] == value);
    }

    public override bool has_key(K key) {
        return hash_map.has_key(key);
    }

    public override Gee.MapIterator<K,V> map_iterator() {
        assert_not_reached();
    }

    public override void @set(K key, V value) {
        assert(value != null);

        unset(key);

        Object v_obj = (Object) value;
        var notify_wrap = new WeakNotifyWrapper((obj) => {
            hash_map.unset(key);
            notify_map.unset(key);
        });
        notify_map[key] = notify_wrap;
        v_obj.weak_ref(notify_wrap.func);

        hash_map[key] = value;
    }

    public override bool unset(K key, out V value = null) {
        if (!hash_map.has_key(key)) {
            value = null;
            return false;
        }

        Object v_obj = (Object) hash_map[key];
        v_obj.weak_unref(notify_map[key].func);
        notify_map.unset(key);
        return hash_map.unset(key, out value);
    }
    public override Gee.Set<Gee.Map.Entry<K,V>> entries { owned get; }

    public override Gee.Set<K> keys {
        owned get { return hash_map.keys; }
    }

    public override bool read_only { get { assert_not_reached(); } }

    public override int size { get { return hash_map.size; } }

    public override Gee.Collection<V> values {
        owned get {
            assert_not_reached();
        }
    }

    public override void dispose() {
        foreach (K key in notify_map.keys) {
            Object o = (Object) hash_map[key];
            o.weak_unref(notify_map[key].func);
        }
    }
}

internal class WeakNotifyWrapper {
    public WeakNotify func;

    public WeakNotifyWrapper(owned WeakNotify func) {
        this.func = (owned) func;
    }
}