mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-08 21:04:20 +00:00
Merge pull request #1308 from flyx/tset_additions
Logical operations for TSet
This commit is contained in:
@@ -157,37 +157,37 @@ proc `$`*[A](s: TSet[A]): string =
|
||||
|
||||
proc union*[A](s1, s2: TSet[A]): TSet[A] =
|
||||
## returns a new set of all items that are contained in at
|
||||
## least one of `l` and `r`
|
||||
## least one of `s1` and `s2`
|
||||
result = s1
|
||||
incl(result, s2)
|
||||
|
||||
proc intersection*[A](s1, s2: TSet[A]): TSet[A] =
|
||||
## returns a new set of all items that are contained in both `l` and `r`
|
||||
## returns a new set of all items that are contained in both `s1` and `s2`
|
||||
result = initSet[A](min(s1.data.len, s2.data.len))
|
||||
for item in s1:
|
||||
if item in s2: incl(result, item)
|
||||
|
||||
proc symmetricDifference*[A](s1, s2: TSet[A]): TSet[A] =
|
||||
## returns a new set of all items that are contained in either
|
||||
## `l` or `r`, but not both
|
||||
## `s1` or `s2`, but not both
|
||||
result = s1
|
||||
for item in s2:
|
||||
if containsOrIncl(result, item): excl(result, item)
|
||||
|
||||
proc `or`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} =
|
||||
proc `+`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} =
|
||||
## alias for `union`
|
||||
result = union(s1, s2)
|
||||
|
||||
proc `and`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} =
|
||||
proc `*`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} =
|
||||
## alias for `intersection`
|
||||
result = intersection(s1, s2)
|
||||
|
||||
proc `xor`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} =
|
||||
proc `-+-`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} =
|
||||
## alias for `symmetricDifference`
|
||||
result = symmetricDifference(s1, s2)
|
||||
|
||||
proc disjoint*[A](s1, s2: TSet[A]): bool =
|
||||
## returns true iff `l` and `r` have no items in common
|
||||
## returns true iff `s1` and `s2` have no items in common
|
||||
for item in s1:
|
||||
if item in s2: return false
|
||||
return true
|
||||
|
||||
@@ -8,8 +8,8 @@ let
|
||||
block union:
|
||||
let
|
||||
s1_s2 = union(s1, s2)
|
||||
s1_s3 = s1 or s3
|
||||
s2_s3 = s2 or s3
|
||||
s1_s3 = s1 + s3
|
||||
s2_s3 = s2 + s3
|
||||
|
||||
assert s1_s2.len == 7
|
||||
assert s1_s3.len == 8
|
||||
@@ -25,14 +25,14 @@ block union:
|
||||
assert i in s1_s3
|
||||
assert i in s2_s3
|
||||
|
||||
assert((s1 or s1) == s1)
|
||||
assert((s2 or s1) == s1_s2)
|
||||
assert((s1 + s1) == s1)
|
||||
assert((s2 + s1) == s1_s2)
|
||||
|
||||
block intersection:
|
||||
let
|
||||
s1_s2 = intersection(s1, s2)
|
||||
s1_s3 = intersection(s1, s3)
|
||||
s2_s3 = s2 and s3
|
||||
s2_s3 = s2 * s3
|
||||
|
||||
assert s1_s2.len == 3
|
||||
assert s1_s3.len == 0
|
||||
@@ -48,14 +48,14 @@ block intersection:
|
||||
assert i in s2
|
||||
assert i in s3
|
||||
|
||||
assert((s2 and s2) == s2)
|
||||
assert((s3 and s2) == s2_s3)
|
||||
assert((s2 * s2) == s2)
|
||||
assert((s3 * s2) == s2_s3)
|
||||
|
||||
block symmetricDifference:
|
||||
let
|
||||
s1_s2 = symmetricDifference(s1, s2)
|
||||
s1_s3 = s1 xor s3
|
||||
s2_s3 = s2 xor s3
|
||||
s1_s3 = s1 -+- s3
|
||||
s2_s3 = s2 -+- s3
|
||||
|
||||
assert s1_s2.len == 4
|
||||
assert s1_s3.len == 8
|
||||
@@ -71,8 +71,8 @@ block symmetricDifference:
|
||||
assert i in s1_s3 xor i in s1
|
||||
assert i in s2_s3 xor i in s2
|
||||
|
||||
assert((s3 xor s3) == initSet[int]())
|
||||
assert((s3 xor s1) == s1_s3)
|
||||
assert((s3 -+- s3) == initSet[int]())
|
||||
assert((s3 -+- s1) == s1_s3)
|
||||
|
||||
block disjoint:
|
||||
assert(not disjoint(s1, s2))
|
||||
|
||||
Reference in New Issue
Block a user