Skip to content

Commit 609d419

Browse files
committed
doct: added examples to the stack revamped docs + small corrections
1 parent 751d283 commit 609d419

File tree

2 files changed

+169
-9
lines changed

2 files changed

+169
-9
lines changed

docs/src/stack.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ DocTestSetup = :(using DataStructures)
66

77
The Stack data structure corresponds to a *Last In, First Out* (LIFO) queue in
88
which elements are added to and removed from only one of the ends of the queue.
9-
In DataStructures.jl the `Stack` type is a light-weight wrapper around the
10-
[`Deque`](./deque.md) type.
9+
In DataStructures.jl the [`Stack`](./stack.md) type is a light-weight wrapper around
10+
the [`Deque`](./deque.md) type.
1111

12-
!!! note "Note: Iterator interface"
12+
!!! note "Notes on the Iterator interface implemented by the Stack"
1313
The `Stack` type implements the Julia Iterator interface; iterating
1414
over `Stack` returns items in *First In, Last Out* (FILO) order, i.e. "from top
1515
to bottom" of the stack. There is also a `Iterators.reverse` function which

src/stack.jl

Lines changed: 166 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Create a `Stack` object containing elements of type `T` for Last In, First Out (
66
77
# Parameters
88
- `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.
1010
1111
# Examples
1212
```jldoctest
@@ -28,14 +28,43 @@ Stack{T}(blksize::Integer) where {T} = Stack(Deque{T}(blksize))
2828
isempty(s::Stack)
2929
3030
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+
```
3147
"""
3248
Base.isempty(s::Stack) = isempty(s.store)
3349

3450

3551
"""
36-
isempty(s::Stack)
52+
length(s::Stack)
3753
3854
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+
```
3968
"""
4069
Base.length(s::Stack) = length(s.store)
4170

@@ -44,49 +73,180 @@ Base.length(s::Stack) = length(s.store)
4473
eltype(::Type{Stack{T}}) where {T}
4574
4675
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+
```
4788
"""
4889
Base.eltype(::Type{Stack{T}}) where {T} = T
4990

91+
5092
"""
5193
first(s::Stack)
5294
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+
```
54113
"""
55114
Base.first(s::Stack) = last(s.store)
56115

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+
"""
57138
Base.last(s::Stack) = first(s.store)
58139

140+
59141
"""
60142
push!(s::Stack, x)
61143
62144
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+
```
63157
"""
64158
function Base.push!(s::Stack, x)
65159
push!(s.store, x)
66160
return s
67161
end
68162

163+
69164
"""
70-
pop!(s::Stack)
165+
pop!(s::Stack)
71166
72167
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+
```
73190
"""
74191
Base.pop!(s::Stack) = pop!(s.store)
75192

193+
76194
"""
77195
empty!(s::Stack)
78196
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+
```
80217
"""
81218
Base.empty!(s::Stack) = (empty!(s.store); s)
82219

83220
Base.iterate(st::Stack, s...) = iterate(Iterators.reverse(st.store), s...)
84221

85222
Iterators.reverse(s::Stack{T}) where {T} = DequeIterator{T}(s.store)
86223

224+
87225
"""
88226
==(x::Stack, y::Stack)
89227
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+
```
91251
"""
92252
Base.:(==)(x::Stack, y::Stack) = x.store == y.store

0 commit comments

Comments
 (0)