2
2
Stack{T}() where {T}
3
3
Stack{T}(blksize::Integer) where {T}
4
4
5
- Create a `Stack` object containing elements of type `T` for Last In, First Out (LIFO) access.
5
+ Create a `Stack` object containing elements of type `T` for **Last In, First Out**
6
+ (LIFO) access.
6
7
7
8
# Parameters
8
9
- `T::Type` Stack element data type.
9
- - `blksize::Integer` Unrolled linked-list bleck size (in bytes). Default = 1024.
10
+ - `blksize::Integer` Unrolled linked-list block size (in bytes) used in the
11
+ underlying representation of the stack. Default = 1024.
10
12
11
13
# Examples
12
14
```jldoctest
13
- julia> s_int = Stack{Int64}() # create a stack with int elements
15
+ julia> s_int = Stack{Int64}() # create a stack with Int64 elements
14
16
Stack{Int64}(Deque [Int64[]])
15
17
16
- julia> s_float = Stack{Float64}() # create a stack with float elements
18
+ julia> s_float = Stack{Float64}() # create a stack with Float64 elements
17
19
Stack{Float64}(Deque [Float64[]])
18
20
```
19
21
"""
@@ -27,23 +29,7 @@ Stack{T}(blksize::Integer) where {T} = Stack(Deque{T}(blksize))
27
29
"""
28
30
isempty(s::Stack)
29
31
30
- Check if stack `s` is empty.
31
-
32
- # Example
33
- ```jldoctest
34
- julia> s = Stack{Char}()
35
- Stack{Char}(Deque [Char[]])
36
-
37
- julia> isempty(s)
38
- true
39
-
40
- julia> for char in "racecar"
41
- push!(s, char)
42
- end
43
-
44
- julia> isempty(s)
45
- false
46
- ```
32
+ Returns `true` if stack `s` is empty - i.e. has no elements - or `false` otherwise.
47
33
"""
48
34
Base. isempty (s:: Stack ) = isempty (s. store)
49
35
@@ -52,19 +38,6 @@ Base.isempty(s::Stack) = isempty(s.store)
52
38
length(s::Stack)
53
39
54
40
Return the number of elements in stack `s`.
55
-
56
- # Example
57
- ```jldoctest
58
- julia> s = Stack{Char}()
59
- Stack{Char}(Deque [Char[]])
60
-
61
- julia> for char in "racecar"
62
- push!(s, char)
63
- end
64
-
65
- julia> length(s)
66
- 7
67
- ```
68
41
"""
69
42
Base. length (s:: Stack ) = length (s. store)
70
43
@@ -73,27 +46,16 @@ Base.length(s::Stack) = length(s.store)
73
46
eltype(::Type{Stack{T}}) where {T}
74
47
75
48
Return the type of the elements in the stack.
76
-
77
- # Example
78
- ```jldoctest
79
- julia> s = Stack{Float32}()
80
- Stack{Float32}(Deque [Float32[]])
81
-
82
- julia> eltype(s)
83
- Float32
84
-
85
- julia> eltype(s) <: Number
86
- true
87
- ```
88
49
"""
89
50
Base. eltype (:: Type{Stack{T}} ) where {T} = T
90
51
91
52
92
53
"""
93
54
first(s::Stack)
94
55
95
- Get the first element of `s`. Since `s` is a stack, the first element will be the
96
- element at the top of `s` (also known as "peek" of the stack).
56
+ Get the first element of `s` in *Last In, First Out* order. Since `s` is a stack,
57
+ the first element will be the element at the top of `s` (also known as "peek" of
58
+ the stack).
97
59
98
60
# Example
99
61
```jldoctest
@@ -116,8 +78,8 @@ Base.first(s::Stack) = last(s.store)
116
78
"""
117
79
last(s::Stack)
118
80
119
- Get the last element of `s`. Since `s` is a stack, the last element will be the at
120
- bottom of the stack.
81
+ Get the last element of `s` in *Last In, First Out* . Since `s` is a stack, the last
82
+ element will be the at bottom of the stack.
121
83
122
84
# Example
123
85
```jldoctest
@@ -142,18 +104,6 @@ Base.last(s::Stack) = first(s.store)
142
104
push!(s::Stack, x)
143
105
144
106
Insert new element `x` in top of stack `s`.
145
-
146
- # Example
147
- ```jldoctest
148
- julia> s = Stack{Int}()
149
- Stack{Int64}(Deque [Int64[]])
150
-
151
- julia> push!(s, 42)
152
- Stack{Int64}(Deque [[42]])
153
-
154
- julia> push!(s, 314)
155
- Stack{Int64}(Deque [[42, 314]])
156
- ```
157
107
"""
158
108
function Base. push! (s:: Stack , x)
159
109
push! (s. store, x)
165
115
pop!(s::Stack)
166
116
167
117
Remove and return the top element from stack `s`.
168
-
169
- # Example
170
- ```jldoctest
171
- julia> s = Stack{Int}()
172
- Stack{Int64}(Deque [Int64[]])
173
-
174
- julia> for i in 1:10
175
- push!(s, i)
176
- end
177
-
178
- julia> s
179
- Stack{Int64}(Deque [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
180
-
181
- julia> popped = pop!(s)
182
- 10
183
-
184
- julia> s
185
- Stack{Int64}(Deque [[1, 2, 3, 4, 5, 6, 7, 8, 9]])
186
-
187
- julia> popped
188
- 10
189
- ```
190
118
"""
191
119
Base. pop! (s:: Stack ) = pop! (s. store)
192
120
@@ -195,25 +123,6 @@ Base.pop!(s::Stack) = pop!(s.store)
195
123
empty!(s::Stack)
196
124
197
125
Make `s` empty by inplace-removing all its elements.
198
-
199
- # Example
200
- ```jldoctest
201
- julia> s = Stack{Int}()
202
- Stack{Int64}(Deque [Int64[]])
203
-
204
- julia> for i in 1:4
205
- push!(s, i)
206
- end
207
-
208
- julia> isempty(s)
209
- false
210
-
211
- julia> empty!(s)
212
- Stack{Int64}(Deque [Int64[]])
213
-
214
- julia> isempty(s)
215
- true
216
- ```
217
126
"""
218
127
Base. empty! (s:: Stack ) = (empty! (s. store); s)
219
128
@@ -225,9 +134,9 @@ Iterators.reverse(s::Stack{T}) where {T} = DequeIterator{T}(s.store)
225
134
"""
226
135
==(x::Stack, y::Stack)
227
136
228
- Check if stacks `x` and `y` are equal in term of their contents. Internally calls `==()`
229
- for each of the pairs formed by the elements of `x` and `y` in the order they appear
230
- in the stack.
137
+ Check if stacks `x` and `y` are equal in terms of their contents and the order in
138
+ which they are present in the stack. Internally calls `==()` for each of the pairs
139
+ formed by the elements of `x` and `y` in the order they appear in the stack.
231
140
232
141
# Example
233
142
```jldoctest
@@ -248,5 +157,16 @@ julia> pop!(s1)
248
157
julia> s1 == s2
249
158
false
250
159
```
160
+ ```jldoctest
161
+ julia> a, b = Stack{Int}(), Stack{Int}()
162
+ (Stack{Int64}(Deque [Int64[]]), Stack{Int64}(Deque [Int64[]]))
163
+
164
+ julia> for num in [1, 2, 3, 4] push!(a, num) end
165
+
166
+ julia> for num in [1, 2, 4, 3] push!(b, num) end
167
+
168
+ julia> a == b # same elements but in different order
169
+ false
170
+ ```
251
171
"""
252
172
Base.:(== )(x:: Stack , y:: Stack ) = x. store == y. store
0 commit comments