Skip to content

Commit 99cb3cf

Browse files
committed
get keys
1 parent b735972 commit 99cb3cf

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/PriorityList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function has($name)
4040
return isset($this->list[$name]);
4141
}
4242

43+
public function getKeys()
44+
{
45+
return array_keys($this->list);
46+
}
47+
4348
public function count()
4449
{
4550
return count($this->list);

src/WeightList.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
namespace Sokil\DataType;
44

55
/**
6-
* Class used to get some identifier according to
6+
* Class used to get some identifier according to
77
* probability of this key.
8-
*
8+
*
99
*/
1010
class WeightList
1111
{
1212
private $list;
13-
13+
1414
public function __construct(array $list = array())
1515
{
1616
$this->list = $list;
1717
}
18-
18+
1919
public function set($value, $weight)
2020
{
2121
$this->list[$value] = $weight;
2222
return $this;
2323
}
24-
24+
2525
private function getValueByPosition($position)
2626
{
2727
$accumulator = 0;
@@ -34,10 +34,15 @@ private function getValueByPosition($position)
3434

3535
return $value;
3636
}
37-
37+
3838
public function getRandomValue()
3939
{
4040
$position = mt_rand(0, array_sum($this->list));
4141
return $this->getValueByPosition($position);
4242
}
43+
44+
public function getValues()
45+
{
46+
return array_keys($this->list);
47+
}
4348
}

tests/PriorityListTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ public function testGet()
8181
$this->assertEquals('v5', $list->get('k5'));
8282
}
8383

84+
public function testGetKeys()
85+
{
86+
$list = new PriorityList();
87+
88+
$list->set('k1', 'v1', 2);
89+
$list->set('k2', 'v2', 8);
90+
$list->set('k3', 'v3', 16);
91+
$list->set('k4', 'v4', 1);
92+
$list->set('k5', 'v5', 4);
93+
94+
$this->assertEquals(
95+
array('k1', 'k2', 'k3', 'k4', 'k5'),
96+
$list->getKeys()
97+
);
98+
}
99+
84100
public function testHas()
85101
{
86102
$list = new PriorityList();

0 commit comments

Comments
 (0)