From 5bfea58ffb9fd731131bbbd3f3d077cb69c5f419 Mon Sep 17 00:00:00 2001 From: def Date: Mon, 14 Jul 2014 15:03:09 +0200 Subject: [PATCH 1/3] Add missing difference and to sets module --- lib/pure/collections/sets.nim | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index 4ba67cb2e4..2f0c4d3d2d 100644 --- a/lib/pure/collections/sets.nim +++ b/lib/pure/collections/sets.nim @@ -167,6 +167,12 @@ proc intersection*[A](s1, s2: TSet[A]): TSet[A] = for item in s1: if item in s2: incl(result, item) +proc difference*[A](s1, s2: TSet[A]): TSet[A] = + ## returns a new set of all items that are contained in `s1`, but not in `s2` + result = s1 + for item in s2: + if contains(result, item): excl(result, item) + proc symmetricDifference*[A](s1, s2: TSet[A]): TSet[A] = ## returns a new set of all items that are contained in either ## `s1` or `s2`, but not both @@ -182,6 +188,10 @@ proc `*`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} = ## alias for `intersection` result = intersection(s1, s2) +proc `-`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} = + ## alias for `difference` + result = difference(s1, s2) + proc `-+-`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} = ## alias for `symmetricDifference` result = symmetricDifference(s1, s2) From 68c3c8ebfafbcd169bceaf2bebed8f7af8dbf124 Mon Sep 17 00:00:00 2001 From: def Date: Mon, 14 Jul 2014 17:56:19 +0200 Subject: [PATCH 2/3] More effificent TSet difference --- lib/pure/collections/sets.nim | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index 2f0c4d3d2d..f1eed00042 100644 --- a/lib/pure/collections/sets.nim +++ b/lib/pure/collections/sets.nim @@ -169,9 +169,10 @@ proc intersection*[A](s1, s2: TSet[A]): TSet[A] = proc difference*[A](s1, s2: TSet[A]): TSet[A] = ## returns a new set of all items that are contained in `s1`, but not in `s2` - result = s1 - for item in s2: - if contains(result, item): excl(result, item) + result = initSet[A]() + for item in s1: + if not contains(s2, item): + incl(result, item) proc symmetricDifference*[A](s1, s2: TSet[A]): TSet[A] = ## returns a new set of all items that are contained in either From 54d61d2f665d47e361cad6c6c1075a9aca2de4d3 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 15 Jul 2014 02:16:12 +0200 Subject: [PATCH 3/3] Add TSet difference tests --- tests/sets/tsets3.nim | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/sets/tsets3.nim b/tests/sets/tsets3.nim index d2b15d72d1..f599f8e7d6 100644 --- a/tests/sets/tsets3.nim +++ b/tests/sets/tsets3.nim @@ -74,8 +74,27 @@ block symmetricDifference: assert((s3 -+- s3) == initSet[int]()) assert((s3 -+- s1) == s1_s3) +block difference: + let + s1_s2 = difference(s1, s2) + s1_s3 = difference(s1, s3) + s2_s3 = s2 - s3 + + assert s1_s2.len == 2 + assert s1_s3.len == 5 + assert s2_s3.len == 3 + + for i in s1: + assert i in s1_s2 xor i in s2 + assert i in s1_s3 xor i in s3 + for i in s2: + assert i in s2_s3 xor i in s3 + + assert((s2 - s2) == initSet[int]()) + assert((s1 - s3 - s1) == s1 -+- s3) + block disjoint: assert(not disjoint(s1, s2)) assert disjoint(s1, s3) assert(not disjoint(s2, s3)) - assert(not disjoint(s2, s2)) \ No newline at end of file + assert(not disjoint(s2, s2))