Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Commit 2238b0d

Browse files
committed
Add new Collections component, with a first Set implementation
1 parent e944604 commit 2238b0d

File tree

4 files changed

+162
-1
lines changed

4 files changed

+162
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"autoload": {
2727
"psr-0": {
2828
"QafooLabs\\Refactoring": "src/main/",
29-
"QafooLabs\\Patches": "src/main/"
29+
"QafooLabs\\Patches": "src/main/",
30+
"QafooLabs\\Collections": "src/main/"
3031
}
3132
},
3233

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace QafooLabs\Collections;
4+
5+
/**
6+
* Allows to span a hash over all identifing values of an object.
7+
*
8+
* This is used by some data structures to compute the uniqueness of
9+
* an object, for example in the Set.
10+
*/
11+
interface Hashable
12+
{
13+
/**
14+
* Return a hash over identifying values of this object.
15+
*
16+
* @return string
17+
*/
18+
public function hashCode();
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace QafooLabs\Collections;
4+
5+
use Countable;
6+
use IteratorAggregate;
7+
use ArrayIterator;
8+
9+
/**
10+
* Unique Set of Elements
11+
*/
12+
class Set implements Countable, IteratorAggregate
13+
{
14+
private $items = array();
15+
16+
/**
17+
* Add a new item to the set.
18+
*
19+
* Overrides values that were previously set that have the same value
20+
* or hashCode. If you pass an object make sure it implements either
21+
* {__toString()} or the {@see Hashable} interface.
22+
*
23+
* @param mixed $item
24+
* @return void
25+
*/
26+
public function add($item)
27+
{
28+
if ($item instanceof Hashable) {
29+
$this->items[$item->hashCode()] = $item;
30+
31+
return;
32+
}
33+
34+
$this->items[(string)$item] = $item;
35+
}
36+
37+
/**
38+
* @return int
39+
*/
40+
public function count()
41+
{
42+
return count($this->items);
43+
}
44+
45+
/**
46+
* @return Iterator
47+
*/
48+
public function getIterator()
49+
{
50+
return new ArrayIterator(array_values($this->items));
51+
}
52+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace QafooLabs\Collections;
4+
5+
class SetTest extends \PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function whenAddingItemMultipleTimes_ThenOnlyAddItOnce()
11+
{
12+
$item = 'A';
13+
14+
$set = new Set();
15+
$set->add($item);
16+
$set->add($item);
17+
18+
$this->assertEquals(1, count($set));
19+
}
20+
21+
/**
22+
* @test
23+
*/
24+
public function whenAddingMultipleItems_ThenCountThemUniquely()
25+
{
26+
$item1 = 'A';
27+
$item2 = 'B';
28+
29+
$set = new Set();
30+
$set->add($item1);
31+
$set->add($item1);
32+
$set->add($item2);
33+
34+
$this->assertEquals(2, count($set));
35+
}
36+
37+
/**
38+
* @test
39+
*/
40+
public function whenAddingHashableObjectMultipleTimes_ThenOnlyAddItOnce()
41+
{
42+
$item1 = new FooObject(1);
43+
$item2 = new FooObject(2);
44+
45+
$set = new Set();
46+
$set->add($item1);
47+
$set->add($item1);
48+
$set->add($item2);
49+
$set->add($item2);
50+
51+
$this->assertEquals(2, count($set));
52+
}
53+
54+
/**
55+
* @test
56+
*/
57+
public function whenIteratingOverSet_ThenReturnAllUniqueItems()
58+
{
59+
$item1 = 'A';
60+
$item2 = 'B';
61+
62+
$set = new Set();
63+
$set->add($item1);
64+
$set->add($item2);
65+
66+
$values = array();
67+
68+
foreach ($set as $key => $value) {
69+
$values[$key] = $value;
70+
}
71+
72+
$this->assertEquals(array(0 => 'A', 1 => 'B'), $values);
73+
}
74+
}
75+
76+
class FooObject implements Hashable
77+
{
78+
private $value;
79+
80+
public function __construct($value)
81+
{
82+
$this->value = $value;
83+
}
84+
85+
public function hashCode()
86+
{
87+
return md5($this->value);
88+
}
89+
}

0 commit comments

Comments
 (0)