Set.php 755 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace Pingpp\Util;
  3. use IteratorAggregate;
  4. use ArrayIterator;
  5. class Set implements IteratorAggregate
  6. {
  7. private $_elts;
  8. public function __construct($members = array())
  9. {
  10. $this->_elts = array();
  11. foreach ($members as $item) {
  12. $this->_elts[$item] = true;
  13. }
  14. }
  15. public function includes($elt)
  16. {
  17. return isset($this->_elts[$elt]);
  18. }
  19. public function add($elt)
  20. {
  21. $this->_elts[$elt] = true;
  22. }
  23. public function discard($elt)
  24. {
  25. unset($this->_elts[$elt]);
  26. }
  27. public function toArray()
  28. {
  29. return array_keys($this->_elts);
  30. }
  31. public function getIterator()
  32. {
  33. return new ArrayIterator($this->toArray());
  34. }
  35. }