Skip to content

Commit 2d6cf0f

Browse files
authored
Merge pull request JuliaCollections#809 from lfenzo/docs-revamp-stack-and-queue
Improved Queue and Stack Documentation + docstrings
2 parents 47aae05 + 8009478 commit 2d6cf0f

File tree

6 files changed

+324
-68
lines changed

6 files changed

+324
-68
lines changed

docs/make.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ makedocs(
1010
"deque.md",
1111
"circ_buffer.md",
1212
"circ_deque.md",
13-
"stack_and_queue.md",
13+
"stack.md",
14+
"queue.md",
1415
"priority-queue.md",
1516
"fenwick.md",
1617
"accumulators.md",

docs/src/queue.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
```@meta
2+
DocTestSetup = :(using DataStructures)
3+
```
4+
5+
# Queue
6+
7+
The Queue data structure, also known as *First In, First Out* (FIFO) queue, allows
8+
addition and deletion of items in opposite ends of the data structure. Insertion is
9+
performed in the back of the queue while deletion is performed in the front of the
10+
queue. In DataStructures.jl, the `Queue` type is a light-weight wrapper around the
11+
[Deque](./deque.md) type.
12+
13+
Queues are often used as a base for many different data structures, some of its
14+
variations implemented by DataStructures.jl include:
15+
16+
- [Double Ended Queues](./deque.md)
17+
- [Circular Double Ended Queues](./circ_deque.md)
18+
- [Priority Queues](./priority-queue.md)
19+
20+
## Constructors
21+
22+
```@autodocs
23+
Modules = [DataStructures]
24+
Pages = ["src/queue.jl"]
25+
Order = [:type]
26+
```
27+
28+
## Usage
29+
30+
The `Queue` type implements the following methods:
31+
32+
- [`eltype(::Type{Queue{T}}) where {T}`](@ref)
33+
- [`first(q::Queue)`](@ref)
34+
- [`isempty(q::Queue)`](@ref)
35+
- [`length(q::Queue)`](@ref)
36+
- [`last(q::Queue)`](@ref)
37+
- [`push!(q::Queue, x)`](@ref)
38+
- [`popfirst!(q::Queue)`](@ref)
39+
40+
-----------
41+
42+
```@autodocs
43+
Modules = [DataStructures]
44+
Pages = ["src/queue.jl"]
45+
Order = [:function]
46+
```
47+
48+
```@meta
49+
DocTestSetup = nothing
50+
```

docs/src/stack.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```@meta
2+
DocTestSetup = :(using DataStructures)
3+
```
4+
5+
# Stack
6+
7+
The Stack data structure corresponds to a *Last In, First Out* (LIFO) queue in
8+
which elements are added to and removed from only one of the ends of the queue.
9+
In DataStructures.jl the [`Stack`](./stack.md) type is a light-weight wrapper around
10+
the [`Deque`](./deque.md) type.
11+
12+
!!! note "Notes on the Iterator interface implemented by the Stack"
13+
The `Stack` type implements the Julia Iterator interface; iterating
14+
over `Stack` returns items in *First In, Last Out* (FILO) order, i.e. "from top
15+
to bottom" of the stack. There is also a `Iterators.reverse` function which
16+
iterates over the items in *First In, First Out* (FIFO) order, or "from bottom
17+
to top" of the stack.
18+
19+
```jldoctest
20+
julia> s = Stack{Int64}()
21+
Stack{Int64}(Deque [Int64[]])
22+
23+
julia> for i in 1:4
24+
push!(s, i)
25+
end
26+
27+
julia> for el in s # top to bottom iteration
28+
println(el)
29+
end
30+
4
31+
3
32+
2
33+
1
34+
35+
julia> for el in Iterators.reverse(s) # bottom to top iteration
36+
println(el)
37+
end
38+
1
39+
2
40+
3
41+
4
42+
```
43+
44+
## Constructors
45+
46+
```@autodocs
47+
Modules = [DataStructures]
48+
Pages = ["src/stack.jl"]
49+
Order = [:type]
50+
```
51+
52+
## Usage
53+
54+
The `Stack` type implements the following methods:
55+
56+
- [`==(x::Stack, y::Stack)`](@ref)
57+
- [`eltype(::Type{Stack{T}}) where {T}`](@ref)
58+
- [`empty!(s::Stack)`](@ref)
59+
- [`first(s::Stack)`](@ref)
60+
- [`isempty(s::Stack)`](@ref)
61+
- [`length(s::Stack)`](@ref)
62+
- [`pop!(s::Stack)`](@ref)
63+
- [`push!(s::Stack, x)`](@ref)
64+
65+
----------
66+
67+
```@autodocs
68+
Modules = [DataStructures]
69+
Pages = ["src/stack.jl"]
70+
Order = [:function]
71+
```
72+
73+
```@meta
74+
DocTestSetup = nothing
75+
```

docs/src/stack_and_queue.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/queue.jl

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
# FIFO queue
22

33
"""
4+
Queue{T}() where {T}
45
Queue{T}([blksize::Integer=1024])
56
6-
Create a `Queue` object containing elements of type `T`.
7+
Create a `Queue` object containing elements of type `T` for First In, First Out (FIFO) access.
8+
9+
# Parameters
10+
11+
- `T::Type` Queue element data type.
12+
- `blksize::Integer=1024` Unrolled linked-list bleck size (in bytes). Defualt = 1024.
13+
14+
# Examples
15+
```jldoctest
16+
julia> q_int = Queue{Int64}() # create a queue with int elements
17+
Queue{Int64}(Deque [Int64[]])
18+
19+
julia> q_float = Queue{Float64}() # create a queue with float elements
20+
Queue{Float64}(Deque [Float64[]])
21+
```
722
"""
823
mutable struct Queue{T}
924
store::Deque{T}
@@ -12,30 +27,63 @@ end
1227
Queue{T}() where {T} = Queue(Deque{T}())
1328
Queue{T}(blksize::Integer) where {T} = Queue(Deque{T}(blksize))
1429

15-
Base.isempty(s::Queue) = isempty(s.store)
16-
Base.length(s::Queue) = length(s.store)
17-
Base.eltype(::Type{Queue{T}}) where T = T
30+
"""
31+
isempty(q::Queue)
32+
33+
Check if queue `q` is empty.
34+
"""
35+
Base.isempty(q::Queue) = isempty(q.store)
36+
37+
"""
38+
length(q::Queue)
39+
40+
Return the number of elements in queue `q`.
41+
"""
42+
Base.length(q::Queue) = length(q.store)
43+
44+
"""
45+
eltype(::Type{Queue{T}}) where {T}
46+
47+
Return the type of the elements in the queue.
48+
"""
49+
Base.eltype(::Type{Queue{T}}) where {T} = T
50+
51+
"""
52+
first(q::Queue)
53+
54+
Get the first item from queue `q`.
55+
"""
56+
Base.first(q::Queue) = first(q.store)
57+
58+
"""
59+
last(q::Queue)
1860
19-
Base.first(s::Queue) = first(s.store)
61+
Get the last element in queue `q`.
62+
"""
2063
Base.last(s::Queue) = last(s.store)
2164

2265
"""
23-
push!(s::Queue, x)
66+
push!(q::Queue, x)
2467
25-
Inserts the value `x` to the end of the queue `s`.
68+
Inserts the value `x` to the end of the queue `q`.
2669
"""
27-
function Base.push!(s::Queue, x)
28-
push!(s.store, x)
29-
return s
70+
function Base.push!(q::Queue, x)
71+
push!(q.store, x)
72+
return q
3073
end
3174

3275
"""
33-
popfirst!(s::Queue)
76+
popfirst!(q::Queue)
3477
35-
Removes an element from the front of the queue `s` and returns it.
78+
Removes an element from the front of the queue `q` and returns it.
3679
"""
3780
Base.popfirst!(s::Queue) = popfirst!(s.store)
3881

82+
"""
83+
empty!(q::Queue)
84+
85+
Removes all elements from queue `q`.
86+
"""
3987
Base.empty!(s::Queue) = (empty!(s.store); s)
4088

4189
# Iterators
@@ -44,4 +92,9 @@ Base.iterate(q::Queue, s...) = iterate(q.store, s...)
4492

4593
Iterators.reverse(q::Queue) = Iterators.reverse(q.store)
4694

95+
"""
96+
==(x::Queue, y::Queue)
97+
98+
Verify if queues `x` and `y` are equivalent in their contents.
99+
"""
47100
Base.:(==)(x::Queue, y::Queue) = x.store == y.store

0 commit comments

Comments
 (0)