Skip to content

Commit 4534b6a

Browse files
committed
peek/pop adjustment
1 parent 39ed499 commit 4534b6a

File tree

6 files changed

+73
-17
lines changed

6 files changed

+73
-17
lines changed

src/Datastructure/Lists/ArrayLists/StringBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private function arrayListToStringBuilder(ArrayList $arrayList, bool $reverse =
162162
}
163163
}
164164
while (!$stack->isEmpty()) {
165-
$stringBuilder->append($stack->peek());
165+
$stringBuilder->append($stack->pop());
166166
}
167167
} else {
168168
$queue = new Queue();

src/Datastructure/Stackqueue/Stack.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function isValid(): bool {
9090
*
9191
*/
9292
public function peek() {
93-
if (!$this->isValid()) {
93+
if (null === $this->stack) {
9494
return null;
9595
}
9696
if (0 === $this->stackSize()) {
@@ -113,9 +113,8 @@ public function stackSize(): int {
113113
/**
114114
* pop() removes an item from the top of the stack.
115115
*
116-
* @return bool
117116
*/
118-
public function pop(): bool {
117+
public function pop() {
119118
if ($this->stack === null) {
120119
return false;
121120
}
@@ -128,7 +127,7 @@ public function pop(): bool {
128127
* of the array.
129128
*/
130129
$return = array_pop($this->stack);
131-
return $return !== null;
130+
return $return;
132131
}
133132

134133
/**

src/Datastructure/Stackqueue/StackSet.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,29 @@ class StackSet {
3737
private $counter = 0;
3838
private $stackList = null;
3939

40+
/**
41+
* StackSet constructor.
42+
*
43+
* @param int $maxSize
44+
*/
4045
public function __construct(int $maxSize = 128) {
4146
$this->maxSize = $maxSize;
4247
$this->stackList = new ArrayList();
4348
}
4449

50+
/**
51+
* @param $element
52+
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
53+
*/
4554
public function push($element) {
4655
$stack = $this->getLastStack();
4756
$this->addToStack($stack, $element);
4857
}
4958

59+
/**
60+
* @return Stack
61+
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
62+
*/
5063
private function getLastStack(): Stack {
5164
$modulo = $this->counter % $this->maxSize;
5265
if (0 === $modulo) {
@@ -57,6 +70,11 @@ private function getLastStack(): Stack {
5770
return $stack;
5871
}
5972

73+
/**
74+
* @param Stack $stack
75+
* @param $element
76+
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
77+
*/
6078
private function addToStack(Stack $stack, $element) {
6179
$stack->push($element);
6280
$this->counter++;
@@ -68,11 +86,15 @@ private function addToStack(Stack $stack, $element) {
6886
}
6987
}
7088

89+
/**
90+
* @return mixed|null
91+
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
92+
*/
7193
public function pop() {
7294
$index = $this->stackList->length();
7395
/** @var Stack $stack */
7496
$stack = $this->stackList->get($index - 1);
75-
$element = $stack->peek();
97+
$element = $stack->pop();
7698
$this->counter--;
7799
if (0 === $stack->stackSize()) {
78100
$this->stackList->remove($index - 1);
@@ -82,6 +104,9 @@ public function pop() {
82104
return $element;
83105
}
84106

107+
/**
108+
* @return int
109+
*/
85110
public function stackCount(): int {
86111
return $this->stackList->length();
87112
}

tests/StackQueue/FixedStackTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function testStack() {
4444
$this->assertTrue($added === false);
4545
$this->assertTrue($stack->stackSize() === 2);
4646

47-
$class = $stack->peek();
47+
$class = $stack->pop();
4848
$this->assertTrue($class instanceof Exception);
4949
$this->assertTrue($stack->isEmpty() == false);
5050

51-
$class = $stack->peek();
51+
$class = $stack->pop();
5252
$this->assertTrue($class instanceof stdClass);
5353
$this->assertTrue($stack->isEmpty() == true);
5454
}

tests/StackQueue/StackSetTest.php

+36-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
<?php
2-
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
325

426
namespace StackQueue;
527

6-
728
use doganoo\PHPAlgorithms\Datastructure\Stackqueue\StackSet;
29+
use PHPUnit\Framework\TestCase;
830

9-
class StackSetTest extends \PHPUnit\Framework\TestCase {
31+
/**
32+
* Class StackSetTest
33+
*
34+
* @package StackQueue
35+
*/
36+
class StackSetTest extends TestCase {
37+
/**
38+
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
39+
*/
1040
public function testStackSet() {
1141
$stackSet = new StackSet(2);
1242
$stackSet->push("Hallo");
@@ -19,6 +49,9 @@ public function testStackSet() {
1949
$this->assertTrue($stackSet->stackCount() === 1);
2050
}
2151

52+
/**
53+
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
54+
*/
2255
public function testHugeStackSet() {
2356
$setSize = 1024;
2457
$factor = 4;

tests/StackQueue/StackTest.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,26 @@
2525

2626
use doganoo\PHPAlgorithms\Datastructure\Stackqueue\Queue;
2727
use doganoo\PHPAlgorithms\Datastructure\Stackqueue\Stack;
28+
use PHPUnit\Framework\TestCase;
2829

2930
/**
3031
* Class StackQueueTest class testing Stacks and Queues
3132
*/
32-
class StackTest extends \PHPUnit\Framework\TestCase {
33+
class StackTest extends TestCase {
3334
/**
3435
* Stack class test
3536
*/
3637
public function testStack() {
3738
$stack = new Stack();
3839
$stack->push(new stdClass());
3940
$stack->push(new Exception());
40-
$this->assertTrue($stack->isEmpty() == false);
41+
$this->assertTrue(false === $stack->isEmpty());
4142

42-
$class = $stack->peek();
43+
$class = $stack->pop();
4344
$this->assertTrue($class instanceof Exception);
4445
$this->assertTrue($stack->isEmpty() == false);
4546

46-
$class = $stack->peek();
47+
$class = $stack->pop();
4748
$this->assertTrue($class instanceof stdClass);
4849
$this->assertTrue($stack->isEmpty() == true);
4950
}
@@ -64,7 +65,5 @@ public function testQueue() {
6465
$class = $queue->dequeue();
6566
$this->assertTrue($class instanceof stdClass);
6667
$this->assertTrue($queue->isEmpty() == true);
67-
68-
6968
}
7069
}

0 commit comments

Comments
 (0)