Skip to content

Commit

Permalink
peek/pop adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Aug 20, 2018
1 parent 39ed499 commit 4534b6a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Datastructure/Lists/ArrayLists/StringBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private function arrayListToStringBuilder(ArrayList $arrayList, bool $reverse =
}
}
while (!$stack->isEmpty()) {
$stringBuilder->append($stack->peek());
$stringBuilder->append($stack->pop());
}
} else {
$queue = new Queue();
Expand Down
7 changes: 3 additions & 4 deletions src/Datastructure/Stackqueue/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function isValid(): bool {
*
*/
public function peek() {
if (!$this->isValid()) {
if (null === $this->stack) {
return null;
}
if (0 === $this->stackSize()) {
Expand All @@ -113,9 +113,8 @@ public function stackSize(): int {
/**
* pop() removes an item from the top of the stack.
*
* @return bool
*/
public function pop(): bool {
public function pop() {
if ($this->stack === null) {
return false;
}
Expand All @@ -128,7 +127,7 @@ public function pop(): bool {
* of the array.
*/
$return = array_pop($this->stack);
return $return !== null;
return $return;
}

/**
Expand Down
27 changes: 26 additions & 1 deletion src/Datastructure/Stackqueue/StackSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,29 @@ class StackSet {
private $counter = 0;
private $stackList = null;

/**
* StackSet constructor.
*
* @param int $maxSize
*/
public function __construct(int $maxSize = 128) {
$this->maxSize = $maxSize;
$this->stackList = new ArrayList();
}

/**
* @param $element
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
*/
public function push($element) {
$stack = $this->getLastStack();
$this->addToStack($stack, $element);
}

/**
* @return Stack
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
*/
private function getLastStack(): Stack {
$modulo = $this->counter % $this->maxSize;
if (0 === $modulo) {
Expand All @@ -57,6 +70,11 @@ private function getLastStack(): Stack {
return $stack;
}

/**
* @param Stack $stack
* @param $element
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
*/
private function addToStack(Stack $stack, $element) {
$stack->push($element);
$this->counter++;
Expand All @@ -68,11 +86,15 @@ private function addToStack(Stack $stack, $element) {
}
}

/**
* @return mixed|null
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
*/
public function pop() {
$index = $this->stackList->length();
/** @var Stack $stack */
$stack = $this->stackList->get($index - 1);
$element = $stack->peek();
$element = $stack->pop();
$this->counter--;
if (0 === $stack->stackSize()) {
$this->stackList->remove($index - 1);
Expand All @@ -82,6 +104,9 @@ public function pop() {
return $element;
}

/**
* @return int
*/
public function stackCount(): int {
return $this->stackList->length();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/StackQueue/FixedStackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function testStack() {
$this->assertTrue($added === false);
$this->assertTrue($stack->stackSize() === 2);

$class = $stack->peek();
$class = $stack->pop();
$this->assertTrue($class instanceof Exception);
$this->assertTrue($stack->isEmpty() == false);

$class = $stack->peek();
$class = $stack->pop();
$this->assertTrue($class instanceof stdClass);
$this->assertTrue($stack->isEmpty() == true);
}
Expand Down
39 changes: 36 additions & 3 deletions tests/StackQueue/StackSetTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
<?php

/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace StackQueue;


use doganoo\PHPAlgorithms\Datastructure\Stackqueue\StackSet;
use PHPUnit\Framework\TestCase;

class StackSetTest extends \PHPUnit\Framework\TestCase {
/**
* Class StackSetTest
*
* @package StackQueue
*/
class StackSetTest extends TestCase {
/**
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
*/
public function testStackSet() {
$stackSet = new StackSet(2);
$stackSet->push("Hallo");
Expand All @@ -19,6 +49,9 @@ public function testStackSet() {
$this->assertTrue($stackSet->stackCount() === 1);
}

/**
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
*/
public function testHugeStackSet() {
$setSize = 1024;
$factor = 4;
Expand Down
11 changes: 5 additions & 6 deletions tests/StackQueue/StackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,26 @@

use doganoo\PHPAlgorithms\Datastructure\Stackqueue\Queue;
use doganoo\PHPAlgorithms\Datastructure\Stackqueue\Stack;
use PHPUnit\Framework\TestCase;

/**
* Class StackQueueTest class testing Stacks and Queues
*/
class StackTest extends \PHPUnit\Framework\TestCase {
class StackTest extends TestCase {
/**
* Stack class test
*/
public function testStack() {
$stack = new Stack();
$stack->push(new stdClass());
$stack->push(new Exception());
$this->assertTrue($stack->isEmpty() == false);
$this->assertTrue(false === $stack->isEmpty());

$class = $stack->peek();
$class = $stack->pop();
$this->assertTrue($class instanceof Exception);
$this->assertTrue($stack->isEmpty() == false);

$class = $stack->peek();
$class = $stack->pop();
$this->assertTrue($class instanceof stdClass);
$this->assertTrue($stack->isEmpty() == true);
}
Expand All @@ -64,7 +65,5 @@ public function testQueue() {
$class = $queue->dequeue();
$this->assertTrue($class instanceof stdClass);
$this->assertTrue($queue->isEmpty() == true);


}
}

0 comments on commit 4534b6a

Please sign in to comment.