summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Hennessy <ehennes@sbcglobal.net>2017-04-23 11:43:23 -0700
committerRico Tzschichholz <ricotz@ubuntu.com>2017-12-11 13:39:18 +0100
commitda95e830524ffa309eb57925320666e5085b9d66 (patch)
tree16d7550e7ca87d34adf8ed5af345d71ef61bb126
parent205c12ae23bc2ec43ff40a89d220a12ac8d622a5 (diff)
downloadlibgee-da95e830524ffa309eb57925320666e5085b9d66.tar.gz
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
-rw-r--r--gee/traversable.vala39
-rw-r--r--tests/testcollection.vala44
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);
+ }
}