@@ -6,7 +6,7 @@ Create a `Stack` object containing elements of type `T` for Last In, First Out (
6
6
7
7
# Parameters
8
8
- `T::Type` Stack element data type.
9
- - `blksize::Integer` Unrolled linked-list bleck size (in bytes). Defualt = 1024.
9
+ - `blksize::Integer` Unrolled linked-list bleck size (in bytes). Default = 1024.
10
10
11
11
# Examples
12
12
```jldoctest
@@ -28,14 +28,43 @@ Stack{T}(blksize::Integer) where {T} = Stack(Deque{T}(blksize))
28
28
isempty(s::Stack)
29
29
30
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
+ ```
31
47
"""
32
48
Base. isempty (s:: Stack ) = isempty (s. store)
33
49
34
50
35
51
"""
36
- isempty (s::Stack)
52
+ length (s::Stack)
37
53
38
54
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
+ ```
39
68
"""
40
69
Base. length (s:: Stack ) = length (s. store)
41
70
@@ -44,49 +73,180 @@ Base.length(s::Stack) = length(s.store)
44
73
eltype(::Type{Stack{T}}) where {T}
45
74
46
75
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
+ ```
47
88
"""
48
89
Base. eltype (:: Type{Stack{T}} ) where {T} = T
49
90
91
+
50
92
"""
51
93
first(s::Stack)
52
94
53
- Get the top item from stack `s`. Also know as "peak".
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).
97
+
98
+ # Example
99
+ ```jldoctest
100
+ julia> s = Stack{Float32}()
101
+ Stack{Float32}(Deque [Float32[]])
102
+
103
+ julia> for i in range(1, 0.2, 5)
104
+ push!(s, i)
105
+ end
106
+
107
+ julia> s
108
+ Stack{Float32}(Deque [Float32[1.0, 0.8, 0.6, 0.4, 0.2]])
109
+
110
+ julia> first(s)
111
+ 0.2f0
112
+ ```
54
113
"""
55
114
Base. first (s:: Stack ) = last (s. store)
56
115
116
+ """
117
+ last(s::Stack)
118
+
119
+ Get the last element of `s`. Since `s` is a stack, the last element will be the at
120
+ bottom of the stack.
121
+
122
+ # Example
123
+ ```jldoctest
124
+ julia> s = Stack{Float32}()
125
+ Stack{Float32}(Deque [Float32[]])
126
+
127
+ julia> for i in range(1, 0.2, 5)
128
+ push!(s, i)
129
+ end
130
+
131
+ julia> s
132
+ Stack{Float32}(Deque [Float32[1.0, 0.8, 0.6, 0.4, 0.2]])
133
+
134
+ julia> first(s)
135
+ 1.0f0
136
+ ```
137
+ """
57
138
Base. last (s:: Stack ) = first (s. store)
58
139
140
+
59
141
"""
60
142
push!(s::Stack, x)
61
143
62
144
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
+ ```
63
157
"""
64
158
function Base. push! (s:: Stack , x)
65
159
push! (s. store, x)
66
160
return s
67
161
end
68
162
163
+
69
164
"""
70
- pop!(s::Stack)
165
+ pop!(s::Stack)
71
166
72
167
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
+ ```
73
190
"""
74
191
Base. pop! (s:: Stack ) = pop! (s. store)
75
192
193
+
76
194
"""
77
195
empty!(s::Stack)
78
196
79
- Remove all elements from stack `s`.
197
+ 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
+ ```
80
217
"""
81
218
Base. empty! (s:: Stack ) = (empty! (s. store); s)
82
219
83
220
Base. iterate (st:: Stack , s... ) = iterate (Iterators. reverse (st. store), s... )
84
221
85
222
Iterators. reverse (s:: Stack{T} ) where {T} = DequeIterator {T} (s. store)
86
223
224
+
87
225
"""
88
226
==(x::Stack, y::Stack)
89
227
90
- Check if stacks `x` and `y` are equal in term of their contents.
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.
231
+
232
+ # Example
233
+ ```jldoctest
234
+ julia> s1, s2 = Stack{String}(), Stack{String}()
235
+ (Stack{String}(Deque [String[]]), Stack{String}(Deque [String[]]))
236
+
237
+ julia> for string in ["foo", "bar", "42"]
238
+ push!(s1, string)
239
+ push!(s2, string)
240
+ end
241
+
242
+ julia> s1 == s2
243
+ true
244
+
245
+ julia> pop!(s1)
246
+ "42"
247
+
248
+ julia> s1 == s2
249
+ false
250
+ ```
91
251
"""
92
252
Base.:(== )(x:: Stack , y:: Stack ) = x. store == y. store
0 commit comments