|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ptypes; |
|
4
|
|
|
|
|
5
|
|
|
use Ptypes\Exceptions\UnexpectedType; |
|
6
|
|
|
use Ptypes\Exceptions\InvalidArgument; |
|
7
|
|
|
use Ptypes\Exceptions\StackOverflow; |
|
8
|
|
|
use Ptypes\Exceptions\StackUnderflow; |
|
9
|
|
|
use Countable; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Stack is a last in first out (lifo) data type. |
|
13
|
|
|
* |
|
14
|
|
|
* functions: push, pop, is_empty, size, peek |
|
15
|
|
|
*/ |
|
16
|
|
|
class Stack implements Countable |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
private $maxsize; |
|
20
|
|
|
private $items; |
|
21
|
|
|
private $top; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Stack is a last in first out (lifo) data type. |
|
25
|
|
|
* |
|
26
|
|
|
* @param int $maxsize |
|
27
|
|
|
* |
|
28
|
|
|
* @return Stack |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($maxsize) |
|
31
|
|
|
{ |
|
32
|
|
|
if(gettype($maxsize) != "integer"){throw new UnexpectedType("Expected an integer, got: ".gettype($maxsize));} |
|
33
|
|
|
if($maxsize < 2) {throw new InvalidArgument("Stack has a minimum size of 2!");} |
|
34
|
|
|
|
|
35
|
|
|
$this->maxsize = $maxsize; |
|
36
|
|
|
$this->items = array(); |
|
37
|
|
|
$this->top = 0; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Push (add) the item to the Stack. |
|
42
|
|
|
* |
|
43
|
|
|
* @param object $item |
|
44
|
|
|
*/ |
|
45
|
|
|
public function push($item) |
|
46
|
|
|
{ |
|
47
|
|
|
if($this->top == $this->maxsize) {throw new StackOverflow("Pushing this item would overflow the stack (exceed the max stack size)!");} |
|
48
|
|
|
$this->items[$this->top++] = $item; |
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Pop (remove) the top item from the Stack. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function pop() |
|
56
|
|
|
{ |
|
57
|
|
|
if($this->top == 0) {throw new StackUnderflow("Cannot pop an empty stack!");} |
|
58
|
|
|
unset($this->items[$this->top--]); |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* is_empty tests whether the stack is empty. |
|
64
|
|
|
* |
|
65
|
|
|
* @return boolean |
|
66
|
|
|
*/ |
|
67
|
|
|
public function is_empty() |
|
68
|
|
|
{ |
|
69
|
|
|
return ($this->top == 0) ? true : false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Size, returns the size of the stack. |
|
74
|
|
|
* |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
|
|
public function size() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->top; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Peek returns the top element of the stack, returns null if the stack is empty. |
|
84
|
|
|
* |
|
85
|
|
|
* @return object |
|
86
|
|
|
*/ |
|
87
|
|
|
public function peek() |
|
88
|
|
|
{ |
|
89
|
|
|
if($this->top == 0) {return null;} |
|
90
|
|
|
return $this->items[$this->top-1]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
//Overriding functions & magic methods below |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Count, same functionality as Size. |
|
97
|
|
|
* Overrides the default count function call on this class. |
|
98
|
|
|
* |
|
99
|
|
|
* @return int |
|
100
|
|
|
*/ |
|
101
|
|
|
public function count() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->size(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Overrides the default toString call on this class. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function __toString() |
|
112
|
|
|
{ |
|
113
|
|
|
$str = "Stack:\n"; |
|
114
|
|
|
$str .= ((string)$this->items[$this->top-1])." <-- top\n"; |
|
115
|
|
|
for($i = $this->top-2; $i >= 0; $i--) |
|
116
|
|
|
{ |
|
117
|
|
|
$str .= ((string)$this->items[$i])."\n"; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $str; |
|
121
|
|
|
} |
|
122
|
|
|
} |