|
1 | 1 | // @ts-check
|
2 | 2 |
|
3 |
| - |
4 |
| -const _items = new WeakMap(); |
5 |
| -const _count = new WeakMap(); |
6 |
| - |
7 | 3 | export default class Stack {
|
8 | 4 | constructor() {
|
9 |
| - _count.set(this, 0); |
10 |
| - _items.set(this, {}); |
| 5 | + this.count = 0; |
| 6 | + this.items = {}; |
11 | 7 | }
|
12 |
| - |
13 | 8 | push(element) {
|
14 |
| - const items = _items.get(this); |
15 |
| - const count = _count.get(this); |
16 |
| - items[count] = element; |
17 |
| - _count.set(this, count + 1); |
| 9 | + this.items[this.count] = element; |
| 10 | + this.count++; |
18 | 11 | }
|
19 |
| - |
20 | 12 | pop() {
|
21 | 13 | if (this.isEmpty()) {
|
22 | 14 | return undefined;
|
23 | 15 | }
|
24 |
| - const items = _items.get(this); |
25 |
| - let count = _count.get(this); |
26 |
| - count--; |
27 |
| - _count.set(this, count); |
28 |
| - const result = items[count]; |
29 |
| - delete items[count]; |
| 16 | + this.count--; |
| 17 | + const result = this.items[this.count]; |
| 18 | + delete this.items[this.count]; |
30 | 19 | return result;
|
31 | 20 | }
|
32 |
| - |
33 | 21 | peek() {
|
34 | 22 | if (this.isEmpty()) {
|
35 | 23 | return undefined;
|
36 | 24 | }
|
37 |
| - const items = _items.get(this); |
38 |
| - const count = _count.get(this); |
39 |
| - return items[count - 1]; |
| 25 | + return this.items[this.count - 1]; |
40 | 26 | }
|
41 |
| - |
42 | 27 | isEmpty() {
|
43 |
| - return _count.get(this) === 0; |
| 28 | + return this.count === 0; |
44 | 29 | }
|
45 |
| - |
46 | 30 | size() {
|
47 |
| - return _count.get(this); |
| 31 | + return this.count; |
48 | 32 | }
|
49 |
| - |
50 | 33 | clear() {
|
51 | 34 | /* while (!this.isEmpty()) {
|
52 | 35 | this.pop();
|
53 | 36 | } */
|
54 |
| - _count.set(this, 0); |
55 |
| - _items.set(this, {}); |
| 37 | + this.items = {}; |
| 38 | + this.count = 0; |
56 | 39 | }
|
57 |
| - |
58 | 40 | toString() {
|
59 | 41 | if (this.isEmpty()) {
|
60 | 42 | return '';
|
61 | 43 | }
|
62 |
| - const items = _items.get(this); |
63 |
| - const count = _count.get(this); |
64 |
| - let objString = `${items[0]}`; |
65 |
| - for (let i = 1; i < count; i++) { |
66 |
| - objString = `${objString},${items[i]}`; |
| 44 | + let objString = `${this.items[0]}`; |
| 45 | + for (let i = 1; i < this.count; i++) { |
| 46 | + objString = `${objString},${this.items[i]}`; |
67 | 47 | }
|
68 | 48 | return objString;
|
69 | 49 | }
|
|
0 commit comments