Dictionary.vala 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Invercargill.Convert;
  2. namespace Invercargill {
  3. public class Dictionary<TKey, TValue> : Associative<TKey, TValue>, KeyValues<TKey, TValue> {
  4. private HashTable<TKey, TValue> hash_table;
  5. public Dictionary(HashFunc<TKey>? key_hash_func = null, EqualFunc<TKey>? key_equal_func = null) {
  6. var hash_func = key_hash_func;
  7. var equal_func = key_equal_func;
  8. var key_type = typeof(TKey);
  9. if(hash_func == null) {
  10. if(key_type == typeof(string)) {
  11. hash_func = GLib.str_hash;
  12. }
  13. else if(key_type == typeof(int64) || key_type == typeof(uint64)) {
  14. hash_func = GLib.int64_hash;
  15. }
  16. else if(key_type == typeof(int)) {
  17. hash_func = GLib.direct_hash;
  18. }
  19. else if(key_type == typeof(double)) {
  20. hash_func = GLib.double_hash;
  21. }
  22. else if(key_type.is_a(typeof(Hashable))) {
  23. hash_func = (k) => ((Hashable)k).hash_code();
  24. }
  25. }
  26. if(equal_func == null) {
  27. if(key_type == typeof(string)) {
  28. equal_func = GLib.str_equal;
  29. }
  30. else if(key_type == typeof(int64) || key_type == typeof(uint64)) {
  31. equal_func = GLib.int64_equal;
  32. }
  33. else if(key_type == typeof(int)) {
  34. equal_func = GLib.direct_equal;
  35. }
  36. else if(key_type == typeof(double)) {
  37. equal_func = GLib.double_equal;
  38. }
  39. else if(key_type.is_a(typeof(Equatable))) {
  40. equal_func = (a, b) => ((Equatable<TKey>)a).equals(b);
  41. }
  42. }
  43. hash_table = new HashTable<TKey, TValue>(hash_func, equal_func);
  44. }
  45. public override void @set (TKey key, TValue value) {
  46. lock(hash_table) {
  47. hash_table.set (key, value);
  48. }
  49. }
  50. public override void set_all (Enumerable<KeyValuePair<TKey, TValue>> key_values) {
  51. lock(hash_table) {
  52. key_values.iterate(kv => hash_table.set(kv.key, kv.value));
  53. }
  54. }
  55. public override bool try_get (TKey key, out TValue value) {
  56. lock(hash_table) {
  57. TKey orig_key;
  58. return hash_table.lookup_extended (key, out orig_key, out value);
  59. }
  60. }
  61. public override void clear (TKey key) {
  62. lock(hash_table) {
  63. hash_table.remove(key);
  64. }
  65. }
  66. public override Tracker<KeyValuePair<TKey, TValue>> get_tracker () {
  67. return new DictionaryTracker<TKey, TValue> (this);
  68. }
  69. public override bool has (TKey key) {
  70. lock(hash_table) {
  71. return hash_table.contains (key);
  72. }
  73. }
  74. private class DictionaryTracker<TKey, TValue> : Tracker<KeyValuePair<TKey, TValue>> {
  75. private Dictionary<TKey, TValue> dictionary;
  76. private (unowned TKey)[] keys;
  77. private int index;
  78. public DictionaryTracker(Dictionary<TKey, TValue> dict) {
  79. dictionary = dict;
  80. keys = dict.hash_table.get_keys_as_array ();
  81. index = 0;
  82. }
  83. public override bool has_next() {
  84. return index < keys.length;
  85. }
  86. public override KeyValuePair<TKey, TValue> get_next() {
  87. var key = keys[index];
  88. var item = dictionary.get_or_default(key);
  89. index++;
  90. return new KeyValuePair<TKey, TValue> (key, item);
  91. }
  92. }
  93. }
  94. }