11/*
2- * Created by Trevor Sears <[email protected] >. 3- * 9:12 PM -- July 29th, 2019.
4- * Project: @jsdsl/stack
2+ * Created by Trevor Sears <[email protected] >. 3+ * 9:12 PM -- July 29th, 2019.
4+ * Project: @jsdsl/stack
5+ *
6+ * @jsdsl /stack - A stack (LIFO) implementation written in TypeScript.
7+ * Copyright (C) 2021 Trevor Sears
8+ *
9+ * This program is free software: you can redistribute it and/or modify
10+ * it under the terms of the GNU General Public License as published by
11+ * the Free Software Foundation, either version 3 of the License, or
12+ * (at your option) any later version.
13+ *
14+ * This program is distributed in the hope that it will be useful,
15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+ * GNU General Public License for more details.
18+ *
19+ * You should have received a copy of the GNU General Public License
20+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
521 */
622
7- import { AbstractList } from "@jsdsl/abstract-list" ;
8- import { IIterator , ArrayIterator } from "iter-over" ;
23+ import JSDSL from "@jsdsl/iterator" ;
924
1025/**
1126 * A stack (LIFO) implementation written in JavaScript/TypeScript.
1227 *
1328 * @author Trevor Sears <[email protected] > 14- * @version v0.1 .0
29+ * @version v0.2 .0
1530 * @since v0.1.0
1631 */
17- export class Stack < E > extends AbstractList < E > {
32+ export class Stack < E > implements JSDSL . Iterable < E > {
1833
34+ /**
35+ * The internal raw array used to maintain this stack data structure.
36+ */
1937 private internalStack : E [ ] ;
2038
39+ /**
40+ * Initializes a new stack with an optional collection of elements. The elements should be ordered in bottom-to-top
41+ * of stack order.
42+ *
43+ * @param {E } elements An optional list of elements to include in this newly initialized stack.
44+ */
2145 public constructor ( ...elements : E [ ] ) {
2246
23- super ( ) ;
24-
2547 this . internalStack = [ ] ;
26- this . addAll ( elements ) ;
2748
2849 }
2950
51+ /**
52+ * Pushes the specified element onto the top of this stack.
53+ *
54+ * @param {E } element The element to push onto this stack.
55+ */
3056 public push ( element : E ) : void {
3157
3258 this . internalStack . push ( element ) ;
3359
3460 }
3561
62+ /**
63+ * Removes and returns the element at the top of this stack.
64+ *
65+ * @returns {E | undefined } The element at the top of this stack.
66+ */
3667 public pop ( ) : E | undefined {
3768
3869 return this . internalStack . pop ( ) ;
3970
4071 }
4172
42- public peek ( ) : E {
73+ /**
74+ * Returns the element at the top of this stack without removing it.
75+ *
76+ * @returns {E } The element at the top of this stack without removing it.
77+ */
78+ public peek ( ) : E | undefined {
4379
4480 return this . internalStack [ this . internalStack . length - 1 ] ;
4581
4682 }
4783
48- public add ( element : E ) : void {
49-
50- this . push ( element ) ;
51-
52- }
53-
84+ /**
85+ * Clears out this stack, rendering it empty of elements.
86+ */
5487 public clear ( ) : void {
5588
5689 this . internalStack = [ ] ;
5790
5891 }
5992
60- public contains ( element : E ) : boolean {
61-
62- return this . internalStack . includes ( element ) ;
63-
64- }
65-
66- public get ( index : number ) : E {
67-
68- return this . internalStack [ index ] ;
69-
70- }
71-
93+ /**
94+ * Returns true if this stack contains no elements.
95+ *
96+ * @returns {boolean } true if this stack contains no elements.
97+ */
7298 public isEmpty ( ) : boolean {
7399
74100 return ( this . internalStack . length === 0 ) ;
75101
76102 }
77103
78- public iterator ( ) : IIterator < E > {
104+ /**
105+ * Returns an iterator over the elements of this stack, in LIFO (last in, first out) order.
106+ *
107+ * @returns {IIterator<E> } An iterator over the elements of this stack, in LIFO (last in, first out) order.
108+ */
109+ public iterator ( ) : JSDSL . Iterator < E > {
79110
80- return new ArrayIterator < E > ( this . internalStack ) ;
111+ return new class extends JSDSL . AbstractIterator < E > {
112+
113+ protected elements : E [ ] ;
114+
115+ protected index : number ;
116+
117+ public constructor ( ...elements : E [ ] ) {
118+
119+ super ( ) ;
120+
121+ this . elements = elements ;
122+ this . index = this . elements . length - 1 ;
123+
124+ }
125+
126+ public hasNext ( ) : boolean {
127+
128+ return this . index >= 0 ;
129+
130+ }
131+
132+ public next ( ) : E | undefined {
133+
134+ return this . elements [ this . index -- ] ;
135+
136+ }
137+
138+ } ( ...this . internalStack ) ;
81139
82140 }
83141
84- public remove ( element : E ) : void {
85-
86- let index : number ;
142+ /**
143+ * Returns an {@link IterableIterator} object that allows this class to be used with the baked-in `for...of`
144+ * construct in JavaScript.
145+ *
146+ * @returns {IterableIterator<E> } An {@link IterableIterator} object that allows this class to be used with the
147+ * baked-in `for...of` construct in JavaScript.
148+ */
149+ public [ Symbol . iterator ] ( ) : IterableIterator < E > {
87150
88- do {
89-
90- index = this . internalStack . indexOf ( element ) ;
91-
92- if ( index !== - 1 ) this . internalStack . splice ( index , 1 ) ;
93-
94- } while ( index !== - 1 )
151+ return this . iterator ( ) [ Symbol . iterator ] ( ) ;
95152
96153 }
97154
155+ // TODO [9/9/2021 @ 4:37 PM] Add `stream` method.
156+
157+ /**
158+ * Returns an integer representative of the number of elements contained in this stack.
159+ *
160+ * @returns {number } An integer representative of the number of elements contained in this stack.
161+ */
98162 public size ( ) : number {
99163
100164 return this . internalStack . length ;
101165
102166 }
103167
168+ /**
169+ * Returns an array representative of this stack, ordered from bottom to top of stack.
170+ *
171+ * @returns {E[] } An array representative of this stack.
172+ */
104173 public toArray ( ) : E [ ] {
105174
106175 return this . internalStack ;
107176
108177 }
109178
110- }
179+ }
0 commit comments