From: Alexis Laferrière Date: Mon, 3 Feb 2014 03:43:26 +0000 (-0500) Subject: lib: add Set::union and Set::intersection from pep8analysis X-Git-Tag: v0.6.6~129^2~15 X-Git-Url: http://nitlanguage.org lib: add Set::union and Set::intersection from pep8analysis Signed-off-by: Alexis Laferrière --- diff --git a/contrib/pep8analysis/Makefile b/contrib/pep8analysis/Makefile index 7d05a20..d7e081e 100644 --- a/contrib/pep8analysis/Makefile +++ b/contrib/pep8analysis/Makefile @@ -1,9 +1,9 @@ bin/pep8analysis: mkdir -p bin - ../../bin/nitg --global -o bin/pep8analysis -I lib src/pep8analysis.nit + ../../bin/nitg --global -o bin/pep8analysis src/pep8analysis.nit doc/index.html: - ../../bin/nitdoc -I lib src/pep8analysis.nit + ../../bin/nitdoc src/pep8analysis.nit tests: bin/pep8analysis bin/pep8analysis --cfg-long tests/privat/*.pep tests/laf/*.pep tests/terrasa/*.pep diff --git a/contrib/pep8analysis/src/flow_analysis/framework.nit b/contrib/pep8analysis/src/flow_analysis/framework.nit index 57444e4..a2e9e65 100644 --- a/contrib/pep8analysis/src/flow_analysis/framework.nit +++ b/contrib/pep8analysis/src/flow_analysis/framework.nit @@ -1,5 +1,4 @@ import cfg -import advanced_collections class FlowAnalysis[S] super Visitor diff --git a/lib/standard/collection/abstract_collection.nit b/lib/standard/collection/abstract_collection.nit index ab04618..3241920 100644 --- a/lib/standard/collection/abstract_collection.nit +++ b/lib/standard/collection/abstract_collection.nit @@ -272,6 +272,25 @@ interface Set[E: Object] for e in self do res += res.hash return res end + + # Returns the union of this set with the `other` set + fun union(other: Set[E]): Set[E] + do + var nhs = new_set + nhs.add_all self + nhs.add_all other + return nhs + end + + # Returns the intersection of this set with the `other` set + fun intersection(other: Set[E]): Set[E] + do + var nhs = new_set + for v in self do if other.has(v) then nhs.add(v) + return nhs + end + + protected fun new_set: Set[E] is abstract end # MapRead are abstract associative collections: `key` -> `item`. diff --git a/lib/standard/collection/array.nit b/lib/standard/collection/array.nit index 5de71cd..8c3fe46 100644 --- a/lib/standard/collection/array.nit +++ b/lib/standard/collection/array.nit @@ -424,6 +424,8 @@ class ArraySet[E: Object] # Create an empty set with a given capacity. init with_capacity(i: Int) do _array = new Array[E].with_capacity(i) + + redef fun new_set do return new ArraySet[E] end # Iterators on sets implemented with arrays. diff --git a/lib/standard/collection/hash_collection.nit b/lib/standard/collection/hash_collection.nit index 7f73c3b..f4a1120 100644 --- a/lib/standard/collection/hash_collection.nit +++ b/lib/standard/collection/hash_collection.nit @@ -440,6 +440,8 @@ class HashSet[E: Object] init add_all(coll) end + + redef fun new_set do return new HashSet[E] end private class HashSetNode[E: Object]