Skip to content

Commit

Permalink
allow to add two sets (#100)
Browse files Browse the repository at this point in the history
* allow to add two sets

* faster implementaton - sets are unique
  • Loading branch information
akondas authored May 18, 2024
1 parent 3c9c97e commit 8c48b1c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Collection/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ public function add($element): self
return self::fromPointer($elements);
}

/**
* @param Set<T> $elements
*
* @return Set<T>
*/
public function addAll(Set $elements): self
{
$new = $this->elements;
foreach ($elements->elements as $current) {
if (!$this->contains($current)) {
$new[] = $current;
}
}

return self::fromPointer($new);
}

/**
* @param T $element
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Collection/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,12 @@ public function testSetDisjoint()
self::assertFalse(Set::of('a', 'b', 'c')->disjoint(Set::of('d', 'b', 'f')));
self::assertFalse(Set::of('a', 'b', 'c')->disjoint(Set::of('d', 'e', 'c')));
}

public function testSetAddAll()
{
self::assertTrue(Set::of('a', 'b', 'c')->equals(Set::of('a')->addAll(Set::of('b', 'c'))));
self::assertTrue(Set::of('a', 'b', 'c')->equals(Set::empty()->addAll(Set::of('a', 'b', 'c'))));
self::assertTrue(Set::empty()->equals(Set::empty()->addAll(Set::empty())));
self::assertTrue(Set::of('a', 'b', 'c')->equals(Set::of('a')->addAll(Set::of('a', 'b', 'c'))));
}
}

0 comments on commit 8c48b1c

Please sign in to comment.