HashSet.vala 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. namespace Invercargill.DataStructures {
  2. public class HashSet<T> : Enumerable<T>, Lot<T>, ReadOnlyCollection<T>, ReadOnlySet<T>, Set<T> {
  3. private const uint BUCKET_TOMBSTONE = uint.MAX;
  4. private const uint BUCKET_EMPTY = 0;
  5. private uint[] buckets;
  6. private T[] items;
  7. private int n_items = 0;
  8. private int n_buckets = 16;
  9. private int n_collissions = 0;
  10. private int next_item_index = 0;
  11. private SafeReadFunc<T>? safe_read;
  12. private SafeWriteFunc<T>? safe_write;
  13. private HashDelegate<T> hash_func;
  14. private EqualityDelegate<T> equal_func;
  15. construct {
  16. setup();
  17. }
  18. public HashSet(HashDelegate<T>? value_hash_func = null, EqualityDelegate<T>? value_equal_func = null) {
  19. setup(value_hash_func, value_equal_func);
  20. }
  21. private void setup(HashDelegate<T>? value_hash_func = null, EqualityDelegate<T>? value_equal_func = null) {
  22. hash_func = value_hash_func ?? Operators.hash<T>();
  23. equal_func = value_equal_func ?? Operators.equality<T>();
  24. safe_read = get_safe_read_function_for<T>();
  25. safe_write = get_safe_write_function_for<T>();
  26. buckets = new uint[n_buckets];
  27. items = new T[n_buckets];
  28. }
  29. private T read_item(uint index) {
  30. if(safe_read != null){
  31. return safe_read(items, index);
  32. }
  33. return items[index];
  34. }
  35. private void write_item(uint index, T item) {
  36. if(safe_write != null) {
  37. safe_write(items, index, item);
  38. return;
  39. }
  40. items[index] = item;
  41. }
  42. private void ensure_room_for_items(int count) {
  43. if(items.length <= n_items + count) {
  44. items.resize(items.length * 2);
  45. }
  46. }
  47. private void ensure_room_for_bucket(uint index) {
  48. var target = buckets.length;
  49. while(target <= index) {
  50. target = buckets.length * 2;
  51. }
  52. if(target != buckets.length) {
  53. buckets.resize(target);
  54. }
  55. }
  56. private void double_buckets() {
  57. n_buckets *= 2;
  58. buckets = new uint[n_buckets];
  59. n_collissions = 0;
  60. for(var i = 0; i < n_items; i++) {
  61. var item = read_item(i);
  62. var bucket_index = bucket_for(item);
  63. while(buckets[bucket_index] != BUCKET_EMPTY) {
  64. bucket_index++;
  65. n_collissions++;
  66. ensure_room_for_bucket(bucket_index);
  67. }
  68. buckets[bucket_index] = i+1;
  69. }
  70. }
  71. private T get_item(uint index) {
  72. if(index == BUCKET_EMPTY || index == BUCKET_TOMBSTONE) {
  73. assert_not_reached();
  74. }
  75. return read_item(index-1);
  76. }
  77. private uint add_item(T item) {
  78. ensure_room_for_items(1);
  79. write_item(next_item_index, item);
  80. n_items++;
  81. next_item_index++;
  82. return n_items;
  83. }
  84. private uint bucket_for(T item) {
  85. var hash = hash_func(item);
  86. return hash % n_buckets;
  87. }
  88. public override int? peek_count() {
  89. return n_items;
  90. }
  91. public override EnumerableInfo get_info() {
  92. return new EnumerableInfo.infer_ultimate (this, EnumerableCategory.IN_MEMORY);
  93. }
  94. public override Tracker<T> get_tracker () {
  95. return range(0, buckets.length)
  96. .where(i => buckets[i] != BUCKET_EMPTY && buckets[i] != BUCKET_TOMBSTONE)
  97. .select<T>(i => get_item(buckets[i]))
  98. .get_tracker();
  99. }
  100. public bool add (T item) {
  101. return add_internal(item, false);
  102. }
  103. public new void @set(T item) {
  104. add_internal(item, true);
  105. }
  106. private bool add_internal (T item, bool overwrite) {
  107. if(n_collissions > n_buckets / 4) {
  108. double_buckets();
  109. }
  110. var bucket_index = bucket_for(item);
  111. while(buckets[bucket_index] != BUCKET_EMPTY && buckets[bucket_index] != BUCKET_TOMBSTONE) {
  112. if(equal_func(get_item(buckets[bucket_index]), item)) {
  113. if(overwrite)
  114. write_item(buckets[bucket_index] - 1, item);
  115. return overwrite;
  116. }
  117. bucket_index++;
  118. n_collissions++;
  119. ensure_room_for_bucket(bucket_index);
  120. }
  121. buckets[bucket_index] = add_item(item);
  122. return true;
  123. }
  124. public bool try_find(T search, out T item) {
  125. var bucket_index = bucket_for(search);
  126. while(bucket_index < buckets.length && buckets[bucket_index] != BUCKET_EMPTY) {
  127. if(buckets[bucket_index] != BUCKET_TOMBSTONE) {
  128. var ours = get_item(buckets[bucket_index]);
  129. if(equal_func(ours, search)) {
  130. item = ours;
  131. return true;
  132. }
  133. }
  134. bucket_index++;
  135. }
  136. item = null;
  137. return false;
  138. }
  139. public void remove_first_where (Invercargill.PredicateDelegate<T> predicate) {
  140. remove(first_or_default(predicate));
  141. }
  142. public void remove_all_where (Invercargill.PredicateDelegate<T> predicate) {
  143. except_with(where(predicate));
  144. }
  145. public bool has(T item) {
  146. T? _;
  147. return try_find(item, out _);
  148. }
  149. public void clear() {
  150. n_items = 0;
  151. n_buckets = 16;
  152. n_collissions = 0;
  153. buckets = new uint[n_buckets];
  154. items = new T[n_buckets];
  155. }
  156. public T? remove(T item) {
  157. var bucket_index = bucket_for(item);
  158. while(bucket_index < buckets.length && buckets[bucket_index] != BUCKET_EMPTY) {
  159. if(buckets[bucket_index] != BUCKET_TOMBSTONE) {
  160. var ours = get_item(buckets[bucket_index]);
  161. if(equal_func(ours, item)) {
  162. write_item(buckets[bucket_index] - 1, null);
  163. buckets[bucket_index] = BUCKET_TOMBSTONE;
  164. n_items--;
  165. return ours;
  166. }
  167. else {
  168. }
  169. }
  170. bucket_index++;
  171. }
  172. return null;
  173. }
  174. public bool equals(Enumerable<T> other) {
  175. if(other == this) {
  176. return true;
  177. }
  178. return other.non_common(this, hash_func, equal_func).count() == 0;
  179. }
  180. public bool is_subset_of(Enumerable<T> other) {
  181. return this.exclude(other, hash_func, equal_func).count() == 0;
  182. }
  183. }
  184. }