Explorar o código

Tidy some loose ends up

Billy Barrow hai 11 meses
pai
achega
c932899711

+ 19 - 3
src/lib/Associative/Associative.vala

@@ -3,8 +3,24 @@ namespace Invercargill {
     public abstract class Associative<TKey, TValue> : Collection<KeyValuePair<TKey, TValue>> {
 
         public abstract new void @set(TKey key, TValue value);
-        public abstract new TValue? @get(TKey key);
+        public abstract bool try_get(TKey key, out TValue value);
         public abstract void clear(TKey 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) {
@@ -21,8 +37,8 @@ namespace Invercargill {
         public virtual Enumerable<KeyValuePair<TKey, TValue>> get_all(Enumerable<TKey> keys) {
             var vec = new Vector<KeyValuePair<TKey, TValue>>();
             foreach (var key in keys) {
-                var item = @get(key);
-                if(item != null) {
+                TValue item;
+                if(try_get(key, out item)) {
                     vec.add(new KeyValuePair<TKey, TValue>(key, item));
                 }
             }

+ 4 - 3
src/lib/Associative/Dictionary.vala

@@ -62,9 +62,10 @@ namespace Invercargill {
             }
         }
 
-        public override TValue @get (TKey key) {
+        public override bool try_get (TKey key, out TValue value) {
             lock(hash_table) {
-                return hash_table.get(key);
+                TKey orig_key;
+                return hash_table.lookup_extended (key, out orig_key, out value);
             }
         }
         
@@ -95,7 +96,7 @@ namespace Invercargill {
             }
             public override KeyValuePair<TKey, TValue> get_next() {
                 var key = keys[index];
-                var item = dictionary.get (key);
+                var item = dictionary.get_or_default(key);
                 index++;
                 return new KeyValuePair<TKey, TValue> (key, item);
             }

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

@@ -101,7 +101,7 @@ namespace Invercargill {
             return new_set;
         }
 
-        public new T find(T item) {
+        public new T? find(T item) {
             lock(hash_table) {
                 return hash_table.get(item);
             }

+ 1 - 1
src/lib/Collections/Vector.vala

@@ -79,7 +79,7 @@ namespace Invercargill {
             }
         }
 
-        public override void set(int index, T value) throws IndexError {
+        public override void @set(int index, T value) throws IndexError {
             IndexError? e = null;
             lock(array) {
                 if(index < 0) {

+ 1 - 1
src/lib/Interfaces.vala

@@ -9,7 +9,7 @@ namespace Invercargill {
         public abstract uint hash_code();
     }
 
-    public interface Comparable<T> : Equatable<T> {
+    public interface Comparable<T> {
         public abstract int compare(T other);
     }
 

+ 4 - 0
src/lib/Invercargill.vala

@@ -37,4 +37,8 @@ namespace Invercargill {
         };
     }
 
+    public static Enumerable<string> directory(string path, uint flags) throws FileError {
+        return new DirEnumerable(Dir.open(path, flags));
+    }
+
 }

+ 9 - 9
src/tests/Integration/Dictionary.vala

@@ -9,15 +9,15 @@ void dictionary_tests() {
 
         dict["James"] = "James Smith";
 
-        var billy = dict["Billy"];
-        var john = dict["John"];
-        var james = dict["James"];
-        var bob = dict["Bob"];
+        var billy = dict.get_or_default("Billy");
+        var john = dict.get_or_default("John");
+        var james = dict.get_or_default("James");
+        var bob = dict.get_or_default("Bob");
 
         assert_cmpstr("Billy Barrow", CompareOperator.EQ, billy);
         assert_cmpstr("John Doe", CompareOperator.EQ, john);
         assert_cmpstr("James Smith", CompareOperator.EQ, james);
-        assert_null(bob);        
+        assert_null(bob);
     });
 
     Test.add_func("/invercargill/dictionary/int", () => {
@@ -26,10 +26,10 @@ void dictionary_tests() {
         dict[1962] = "John";
         dict[2002] = "James";
 
-        var billy = dict[1999];
-        var john = dict[1962];
-        var james = dict[2002];
-        var bob = dict[1840];
+        var billy = dict.get_or_default(1999);
+        var john = dict.get_or_default(1962);
+        var james = dict.get_or_default(2002);
+        var bob = dict.get_or_default(1840);
 
         assert_cmpstr("Billy", CompareOperator.EQ, billy);
         assert_cmpstr("John", CompareOperator.EQ, john);

+ 1 - 0
src/tests/TestRunner.vala

@@ -27,5 +27,6 @@ public static int main(string[] args) {
     dictionary_speed_test();
     fifo_speed_test();
 
+
     return 0;
 }