123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using Invercargill.Convert;
- namespace Invercargill {
- public class Dictionary<TKey, TValue> : Associative<TKey, TValue>, KeyValues<TKey, TValue> {
- private HashTable<TKey, TValue> hash_table;
- public Dictionary(HashFunc<TKey>? key_hash_func = null, EqualFunc<TKey>? key_equal_func = null) {
- var hash_func = key_hash_func;
- var equal_func = key_equal_func;
- var key_type = typeof(TKey);
- if(hash_func == null) {
- if(key_type == typeof(string)) {
- hash_func = GLib.str_hash;
- }
- else if(key_type == typeof(int64) || key_type == typeof(uint64)) {
- hash_func = GLib.int64_hash;
- }
- else if(key_type == typeof(int)) {
- hash_func = GLib.direct_hash;
- }
- else if(key_type == typeof(double)) {
- hash_func = GLib.double_hash;
- }
- else if(key_type.is_a(typeof(Hashable))) {
- hash_func = (k) => ((Hashable)k).hash_code();
- }
- }
- if(equal_func == null) {
- if(key_type == typeof(string)) {
- equal_func = GLib.str_equal;
- }
- else if(key_type == typeof(int64) || key_type == typeof(uint64)) {
- equal_func = GLib.int64_equal;
- }
- else if(key_type == typeof(int)) {
- equal_func = GLib.direct_equal;
- }
- else if(key_type == typeof(double)) {
- equal_func = GLib.double_equal;
- }
- else if(key_type.is_a(typeof(Equatable))) {
- equal_func = (a, b) => ((Equatable<TKey>)a).equals(b);
- }
- }
- hash_table = new HashTable<TKey, TValue>(hash_func, equal_func);
- }
- public override void @set (TKey key, TValue value) {
- lock(hash_table) {
- hash_table.set (key, value);
- }
- }
- public override void set_all (Enumerable<KeyValuePair<TKey, TValue>> key_values) {
- lock(hash_table) {
- key_values.iterate(kv => hash_table.set(kv.key, kv.value));
- }
- }
- public override bool try_get (TKey key, out TValue value) {
- lock(hash_table) {
- TKey orig_key;
- return hash_table.lookup_extended (key, out orig_key, out value);
- }
- }
-
- public override void clear (TKey key) {
- lock(hash_table) {
- hash_table.remove(key);
- }
- }
- public override Tracker<KeyValuePair<TKey, TValue>> get_tracker () {
- return new DictionaryTracker<TKey, TValue> (this);
- }
- public override bool has (TKey key) {
- lock(hash_table) {
- return hash_table.contains (key);
- }
- }
- private class DictionaryTracker<TKey, TValue> : Tracker<KeyValuePair<TKey, TValue>> {
- private Dictionary<TKey, TValue> dictionary;
- private (unowned TKey)[] keys;
- private int index;
- public DictionaryTracker(Dictionary<TKey, TValue> dict) {
- dictionary = dict;
- keys = dict.hash_table.get_keys_as_array ();
- index = 0;
- }
- public override bool has_next() {
- return index < keys.length;
- }
- public override KeyValuePair<TKey, TValue> get_next() {
- var key = keys[index];
- var item = dictionary.get_or_default(key);
- index++;
- return new KeyValuePair<TKey, TValue> (key, item);
- }
- }
- }
- }
|