Billy Barrow 1 місяць тому
батько
коміт
1bb19b99e7

+ 0 - 71
src/lib/Associative/Associative.vala

@@ -1,71 +0,0 @@
-namespace Invercargill {
-
-    public abstract class Associative<TKey, TValue> : Collection<KeyValuePair<TKey, TValue>>, KeyValues<TKey, TValue> {
-
-        public abstract new void @set(TKey key, TValue value);
-        public abstract bool try_get(TKey key, out TValue value);
-        public abstract void clear(TKey key); 
-        public abstract bool has(TKey key);
-        
-        public virtual Enumerable<TKey> get_keys() {
-            return select<TKey>(kv => kv.key);
-        }
-
-        public virtual Enumerable<TKey> get_values() {
-            return select<TKey>(kv => kv.key);
-        }
-        
-        public virtual TValue? get_or_default(TKey key) {
-            TValue val;
-            if(try_get(key, out val)) {
-                return val;
-            }
-            return null;
-        }
-
-        public virtual new TValue @get(TKey key) throws IndexError {
-            TValue val;
-            if(try_get(key, out val)) {
-                return val;
-            }
-            throw new IndexError.KEY_NOT_FOUND("The specified key was not found in the associative collection");
-        }
-
-        public virtual void set_all(Enumerable<KeyValuePair<TKey, TValue>> key_values) {
-            foreach (var pair in key_values) {
-                @set(pair.key, pair.value);
-            }
-        }
-
-        public virtual void clear_all(Enumerable<TKey> keys) {
-            foreach (var key in keys) {
-                clear(key);
-            }
-        }
-
-        public virtual Enumerable<KeyValuePair<TKey, TValue>> get_all(Enumerable<TKey> keys) {
-            var vec = new Vector<KeyValuePair<TKey, TValue>>();
-            foreach (var key in keys) {
-                TValue item;
-                if(try_get(key, out item)) {
-                    vec.add(new KeyValuePair<TKey, TValue>(key, item));
-                }
-            }
-            return vec.seal();
-        }
-
-        public override void add (KeyValuePair<TKey, TValue> item) {
-            @set(item.key, item.value);
-        }
-        public override void remove_first_where (Invercargill.PredicateDelegate<Invercargill.KeyValuePair<TKey,TValue>> predicate) {
-            var key = first_or_default(predicate);
-            if(key != null) {
-                clear(key);
-            }
-        }
-        public override void remove_where (Invercargill.PredicateDelegate<Invercargill.KeyValuePair<TKey,TValue>> predicate) {
-            clear_all(where(predicate).select<TKey>(i => i.key));
-        }
-    }
-
-}

+ 19 - 1
src/lib/Collections/Set.vala

@@ -7,7 +7,7 @@ namespace Invercargill {
         private uint[] buckets;
         private T[] items;
         private int n_items = 0;
-        private int n_buckets = 10000000;
+        private int n_buckets = 16;
         private int n_collissions = 0;
         private SafeReadFunc<T>? safe_read;
         private SafeWriteFunc<T>? safe_write;
@@ -49,6 +49,20 @@ namespace Invercargill {
         }
 
         private void double_buckets() {
+            n_buckets *= 2;
+            buckets = new uint[n_buckets];
+            n_collissions = 0;
+
+            for(var i = 0; i < n_items; i++) {
+                var item = safe_read(items, i);
+                var bucket_index = bucket_for(item);
+                while(buckets[bucket_index] != 0) {
+                    bucket_index++;
+                    n_collissions++;
+                    ensure_room_for_bucket(bucket_index);
+                }
+                buckets[bucket_index] = i+1;
+            }
             
         }
 
@@ -76,6 +90,10 @@ namespace Invercargill {
         }
 
         public override void add (T item) {
+            if(n_collissions > n_buckets / 4) {
+                double_buckets();
+            }
+            
             var bucket_index = bucket_for(item);
             while(buckets[bucket_index] != 0) {
                 if(equal_func(get_item(buckets[bucket_index]), item)) {

+ 2 - 1
src/lib/Errors.vala

@@ -13,7 +13,8 @@ namespace Invercargill {
     public errordomain IndexError {
         INDEX_EXCEEDS_UPPER_BOUNDS,
         INDEX_EXCEEDS_LOWER_BOUNDS,
-        KEY_NOT_FOUND
+        KEY_NOT_FOUND,
+        KEY_EXISTS
     }
 
     public errordomain PromotionError {

+ 9 - 0
src/lib/Interfaces/Addressable.vala

@@ -0,0 +1,9 @@
+namespace Invercargill {
+
+    public interface Addressable<T> : ReadOnlyAddressable<T>, Enumerable<T> {
+
+        public abstract void @set(int index, T item) throws IndexError;
+
+    }
+
+}

+ 11 - 0
src/lib/Interfaces/Associative.vala

@@ -0,0 +1,11 @@
+namespace Invercargill {
+
+    public interface Associative<TKey, TValue> : ReadOnlyAssociative<TKey, TValue>, Collection<KeyValuePair<TKey, TValue>>, Enumerable<KeyValuePair<TKey, TValue>> {
+
+        public abstract void @set(TKey key, TValue value);
+        public abstract new bool add(TKey key, TValue value);
+        public abstract new bool remove(TKey key);
+
+    }
+
+}

+ 21 - 0
src/lib/Interfaces/Collection.vala

@@ -0,0 +1,21 @@
+namespace Invercargill {
+
+    public interface Collection<T> : ReadOnlyCollection<T>, Enumerable<T> {
+
+        public abstract void add(T item);
+        public abstract void remove_first_where(PredicateDelegate<T> predicate);
+        public abstract void remove_all_where(PredicateDelegate<T> predicate);
+        public abstract void remove(T item, EqualityDelegate<T>? equator = null);
+        public abstract void clear();
+        
+        public virtual void add_all(Enumerable<T> items) {
+            items.iterate(i => add(i));
+        }
+
+        public virtual void remove_all(Enumerable<T> items, EqualityDelegate<T>? equator = null) {
+            items.iterate(i => remove(i, equator));
+        }
+
+    }
+    
+}

+ 11 - 0
src/lib/Interfaces/ReadOnlyAddressable.vala

@@ -0,0 +1,11 @@
+namespace Invercargill {
+
+    public interface ReadOnlyAddressable<T> : ReadOnlyCollection<T>, Enumerable<T> {
+
+        public abstract T @get(uint index) throws IndexError;
+        public abstract uint? first_index_of(PredicateDelegate<T> predicate);
+        public abstract uint? index_of(T item, EqualityDelegate<T>? equator = null);
+
+    }
+
+}

+ 14 - 0
src/lib/Interfaces/ReadOnlyAssociative.vala

@@ -0,0 +1,14 @@
+namespace Invercargill {
+
+    public interface ReadOnlyAssociative<TKey, TValue> : ReadOnlyCollection<KeyValuePair<TKey, TValue>>, KeyValues<TKey, TValue>, Enumerable<KeyValuePair<TKey,TValue>> {
+
+        public abstract TValue @get(TKey key) throws IndexError;
+        public abstract bool try_get(TKey key, out TValue value);
+        public abstract TValue? get_or_default(TKey key);
+        public abstract bool has(TKey key);
+        public abstract Enumerable<TKey> keys { owned get; }
+        public abstract Enumerable<TKey> values { owned get; }
+        
+
+    }
+}

+ 10 - 0
src/lib/Interfaces/ReadOnlyCollection.vala

@@ -0,0 +1,10 @@
+namespace Invercargill {
+
+    public interface ReadOnlyCollection<T> : Enumerable<T> {
+        
+        public abstract int size { get; }
+        public abstract bool contains(T item);
+
+    }
+
+}

+ 7 - 2
src/lib/meson.build

@@ -64,14 +64,19 @@ sources += files('Promotions/EquatableEnumerable.vala')
 sources += files('Promotions/AttemptEnumerable.vala')
 sources += files('Promotions/PropertyGroupEnumerable.vala')
 
-sources += files('Collections/Collection.vala')
+sources += files('Interfaces/ReadOnlyCollection.vala')
+sources += files('Interfaces/ReadOnlyAssociative.vala')
+sources += files('Interfaces/ReadOnlyAddressable.vala')
+sources += files('Interfaces/Collection.vala')
+sources += files('Interfaces/Associative.vala')
+sources += files('Interfaces/Addressable.vala')
+
 sources += files('Collections/Series.vala')
 sources += files('Collections/Fifo.vala')
 sources += files('Collections/BinaryData.vala')
 sources += files('Collections/Vector.vala')
 sources += files('Collections/Set.vala')
 
-sources += files('Associative/Associative.vala')
 sources += files('Associative/Dictionary.vala')
 sources += files('Associative/Index.vala')
 sources += files('Associative/KeyValues.vala')