Skip to content

Commit

Permalink
Add Set implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyShepherd committed Feb 18, 2017
1 parent 703b67f commit 2ceb32b
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/Model/Collection/AbstractSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* This file is part of the Composite Utils package.
*
* (c) Emily Shepherd <[email protected]>
*
* For the full copyright and license information, please view the
* LICENSE.md file that was distributed with this source code.
*
* @package spaark/composite-utils
* @author Emily Shepherd <[email protected]>
* @license MIT
*/

namespace Spaark\CompositeUtils\Model\Collection;

/**
* Represents an abstract collection which acts as a list of items
*/
abstract class AbstractSet
extends AbstractCollection
implements SetInterface
{
/**
* {@inheritDoc}
*/
public function offsetSet($offset, $value)
{
if ($offset === null)
{
$this->push($value);
}
else
{
throw new \Exception();
}
}

/**
* {@inheritDoc}
*/
public function offsetUnset($index)
{
throw new \Exception();
}

/**
* {@inheritDoc}
*/
public function offsetExists($index)
{
throw new \Exception();
}

/**
* {@inheritDoc}
*/
public function offsetGet($index)
{
throw new \Exception();
}
}

73 changes: 73 additions & 0 deletions src/Model/Collection/HashSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* This file is part of the Composite Utils package.
*
* (c) Emily Shepherd <[email protected]>
*
* For the full copyright and license information, please view the
* LICENSE.md file that was distributed with this source code.
*
* @package spaark/composite-utils
* @author Emily Shepherd <[email protected]>
* @license MIT
*/

namespace Spaark\CompositeUtils\Model\Collection;

use ArrayIterator;
use Spaark\CompositeUtils\Service\HashProducer;

/**
* Represents an List stored in a PHP array
*/
class HashSet extends AbstractSet
{
/**
* @var ValueType[]
*/
protected $data = [];

/**
* {@inheritDoc}
*/
public function add($item)
{
$this->data[HashProducer::getHash($item)] = $item;
}

/**
* Checks if an element exists
*
* @param ValueType The item to search for
* @return boolean If the item exists
*/
public function contains($item) : bool
{
return isset($this->data[HashProducer::getHash($item)]);
}

/**
* {@inheritDoc}
*/
public function remove($item)
{
unset($this->data[HashProducer::getHash($item)]);
}

/**
* {@inheritDoc}
*/
public function getIterator()
{
return new ArrayIterator($this->data);
}

/**
* {@inheritDoc}
*/
public function size() : int
{
return count($this->data);
}
}

35 changes: 35 additions & 0 deletions src/Model/Collection/SetInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* This file is part of the Composite Utils package.
*
* (c) Emily Shepherd <[email protected]>
*
* For the full copyright and license information, please view the
* LICENSE.md file that was distributed with this source code.
*
* @package spaark/composite-utils
* @author Emily Shepherd <[email protected]>
* @license MIT
*/

namespace Spaark\CompositeUtils\Model\Collection;

/**
* Represents an abstract collection which acts as a list of items
*/
interface SetInterface extends CollectionInterface
{
/**
* Adds a new item to the end of the list
*
* @param ValueType $item The item to add
*/
public function add($item) : bool;

/**
* Removes an item from the list, specified by its index
*
* @parami ValueType $item The item to remove
*/
public function remove($item);
}

0 comments on commit 2ceb32b

Please sign in to comment.