1
+ <?php
2
+ /**
3
+ * MIT License
4
+ *
5
+ * Copyright (c) 2018 Dogan Ucar
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
+ */
25
+
26
+ namespace doganoo \PHPAlgorithms \Datastructure \Stackqueue ;
27
+
28
+ use doganoo \PHPAlgorithms \Datastructure \Lists \ArrayLists \ArrayList ;
29
+
30
+ /**
31
+ * Class StackSet
32
+ *
33
+ * @package doganoo\PHPAlgorithms\Datastructure\Stackqueue
34
+ */
35
+ class StackSet {
36
+ private $ maxSize = 0 ;
37
+ private $ counter = 0 ;
38
+ private $ stackList = null ;
39
+
40
+ public function __construct (int $ maxSize = 128 ) {
41
+ $ this ->maxSize = $ maxSize ;
42
+ $ this ->stackList = new ArrayList ();
43
+ }
44
+
45
+ public function push ($ element ) {
46
+ $ stack = $ this ->getLastStack ();
47
+ $ this ->addToStack ($ stack , $ element );
48
+ }
49
+
50
+ private function getLastStack (): Stack {
51
+ $ modulo = $ this ->counter % $ this ->maxSize ;
52
+ if (0 === $ modulo ) {
53
+ return new Stack ();
54
+ }
55
+ $ index = $ this ->stackList ->length ();
56
+ $ stack = $ this ->stackList ->get ($ index - 1 );
57
+ return $ stack ;
58
+ }
59
+
60
+ private function addToStack (Stack $ stack , $ element ) {
61
+ $ stack ->push ($ element );
62
+ $ this ->counter ++;
63
+ if (1 === $ stack ->stackSize ()) {
64
+ $ this ->stackList ->add ($ stack );
65
+ } else {
66
+ $ index = $ this ->stackList ->length ();
67
+ $ this ->stackList ->set ($ index - 1 , $ stack );
68
+ }
69
+ }
70
+
71
+ public function pop () {
72
+ $ index = $ this ->stackList ->length ();
73
+ /** @var Stack $stack */
74
+ $ stack = $ this ->stackList ->get ($ index - 1 );
75
+ $ element = $ stack ->peek ();
76
+ $ this ->counter --;
77
+ if (0 === $ stack ->stackSize ()) {
78
+ $ this ->stackList ->remove ($ index - 1 );
79
+ } else {
80
+ $ this ->stackList ->set ($ index - 1 , $ stack );
81
+ }
82
+ return $ element ;
83
+ }
84
+
85
+ public function stackCount (): int {
86
+ return $ this ->stackList ->length ();
87
+ }
88
+
89
+ }
0 commit comments