From 158cff3098e9370696f9250a199c78e4f2d18ce0 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sat, 30 May 2026 16:10:36 +0200 Subject: Add JID interning --- libdino/src/util/weak_map.vala | 119 ----------------------------------------- 1 file changed, 119 deletions(-) delete mode 100644 libdino/src/util/weak_map.vala (limited to 'libdino/src/util') diff --git a/libdino/src/util/weak_map.vala b/libdino/src/util/weak_map.vala deleted file mode 100644 index a7f4bc44..00000000 --- a/libdino/src/util/weak_map.vala +++ /dev/null @@ -1,119 +0,0 @@ -using Gee; - -public class WeakMap : Gee.AbstractMap { - - private HashMap hash_map; - private HashMap notify_map; - - public HashDataFunc? key_hash_func = null; - public EqualDataFunc? key_equal_func = null; - public EqualDataFunc? value_equal_func = null; - - public WeakMap(owned HashDataFunc? key_hash_func = null, owned EqualDataFunc? key_equal_func = null, owned EqualDataFunc? 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(); - notify_map = new HashMap(); - } else { - hash_map = new HashMap((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((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 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> entries { owned get; } - - public override Gee.Set 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 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; - } -} \ No newline at end of file -- cgit v1.3