[libgee] Add additional query functions to Traversable<G>



commit da95e830524ffa309eb57925320666e5085b9d66
Author: Edward Hennessy <ehennes sbcglobal net>
Date:   Sun Apr 23 11:43:23 2017 -0700

    Add additional query functions to Traversable<G>
    
    * one_match (Predicate<G>) check if exactly one element matches
    * count_match (Predicate<G>) returns the count of items that matches
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781641

 gee/traversable.vala      |   39 +++++++++++++++++++++++++++++++++++++++
 tests/testcollection.vala |   44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+), 0 deletions(-)
---
diff --git a/gee/traversable.vala b/gee/traversable.vala
index 9383041..3745f08 100644
--- a/gee/traversable.vala
+++ b/gee/traversable.vala
@@ -558,6 +558,45 @@ public interface Gee.Traversable<G> : Object {
                return result.iterator ();
        }
 
+       /**
+        * Checks if a signle element matches the given predicate.
+        *
+        * @param pred Predicate to be called to check for matches
+        * @return If a single element matches the predicate
+        * @since 0.20.1
+        */
+       [CCode (ordering = 16)]
+       public virtual bool one_match (owned Predicate<G> pred) {
+               int count = 0;
+               this.foreach ((item) => {
+                       if (pred (item)) {
+                               count++;
+                               return count <= 1;
+                       }
+                       return true;
+               });
+               return count == 1;
+       }
+
+       /**
+        * Counts the number of elements matching the given predicate.
+        *
+        * @param pred Predicate to be called to check for matches
+        * @return The number of elements matching the pre
+        * @since 0.20.1
+        */
+       [CCode (ordering = 17)]
+       public virtual int count_match (owned Predicate<G> pred) {
+               int count = 0;
+               this.foreach ((item) => {
+                       if (pred (item)) {
+                               count++;
+                       }
+                       return true;
+               });
+               return count;
+       }
+
        public enum Stream {
                YIELD,
                CONTINUE,
diff --git a/tests/testcollection.vala b/tests/testcollection.vala
index 819e130..0a2b40a 100644
--- a/tests/testcollection.vala
+++ b/tests/testcollection.vala
@@ -56,6 +56,8 @@ public abstract class CollectionTests : Gee.TestCase {
                add_test ("[Collection] all_match", test_all_match);
                add_test ("[Collection] max_min", test_max_min);
                add_test ("[Collection] order_by", test_order_by);
+               add_test ("[Collection] one_match", test_one_match);
+               add_test ("[Collection] count_match", test_count_match);
        }
 
        protected Collection<string> test_collection;
@@ -1259,5 +1261,47 @@ public abstract class CollectionTests : Gee.TestCase {
                        previous_item = item;
                }
        }
+
+       public void test_one_match () {
+               assert (!test_collection.one_match ((x) => x == "one"));
+
+               assert (test_collection.add ("one"));
+               assert (test_collection.one_match ((x) => x == "one"));
+               assert (!test_collection.one_match ((x) => x == "two"));
+
+               assert (test_collection.add ("two"));
+               assert (test_collection.one_match ((x) => x == "one"));
+               assert (test_collection.one_match ((x) => x == "two"));
+
+               if (test_collection.add ("two")) {
+                       assert (!test_collection.one_match ((x) => x == "two"));
+               } else {
+                       assert (test_collection.one_match ((x) => x == "two"));
+               }
+               assert (test_collection.one_match ((x) => x == "one"));
+
+               assert (!test_collection.one_match ((x) => x == "three"));
+       }
+
+       public void test_count_match () {
+               assert (test_collection.count_match ((x) => x == "one") == 0);
+
+               assert (test_collection.add ("one"));
+               assert (test_collection.count_match ((x) => x == "one") == 1);
+               assert (test_collection.count_match ((x) => x == "two") == 0);
+
+               assert (test_collection.add ("two"));
+               assert (test_collection.count_match ((x) => x == "one") == 1);
+               assert (test_collection.count_match ((x) => x == "two") == 1);
+
+               if (test_collection.add ("two")) {
+                       assert (test_collection.count_match ((x) => x == "two") == 2);
+               } else {
+                       assert (test_collection.count_match ((x) => x == "two") == 1);
+               }
+               assert (test_collection.count_match ((x) => x == "one") == 1);
+
+               assert (test_collection.count_match ((x) => x == "three") == 0);
+       }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]