Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs #770

Merged
merged 2 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/src/priority-queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ julia> # Julia code

julia> # Insert keys with associated priorities
pq["a"] = 10; pq["b"] = 5; pq["c"] = 15; pq
PriorityQueue{Any,Any,Base.Order.ForwardOrdering} with 3 entries:
PriorityQueue{Any, Any, Base.Order.ForwardOrdering} with 3 entries:
"b" => 5
"a" => 10
"c" => 15

julia> # Change the priority of an existing key
pq["a"] = 0; pq
PriorityQueue{Any,Any,Base.Order.ForwardOrdering} with 3 entries:
PriorityQueue{Any, Any, Base.Order.ForwardOrdering} with 3 entries:
"a" => 0
"b" => 5
"c" => 15
```
It is also possible to iterate over the priorities and elements of the queue in sorted order.
```jldoctest
julia> pq = PriorityQueue("a"=>2, "b"=>1, "c"=>3)
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
"b" => 1
"a" => 2
"c" => 3
Expand Down
6 changes: 3 additions & 3 deletions docs/src/robin_dict.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ Examples:

```jldoctest
julia> d = RobinDict{Int, Char}(1 => 'a', 2 => 'b')
RobinDict{Int64,Char} with 2 entries:
RobinDict{Int64, Char} with 2 entries:
2 => 'b'
1 => 'a'

julia> d[3] = 'c';

julia> collect(d)
3-element Array{Pair{Int64,Char},1}:
3-element Vector{Pair{Int64, Char}}:
2 => 'b'
3 => 'c'
1 => 'a'
Expand All @@ -31,7 +31,7 @@ julia> d[1]
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> d
RobinDict{Int64,Char} with 2 entries:
RobinDict{Int64, Char} with 2 entries:
3 => 'c'
1 => 'a'

Expand Down
6 changes: 3 additions & 3 deletions docs/src/swiss_dict.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ Examples:

```jldoctest
julia> d = SwissDict(1 => 'a', 2 => 'b')
SwissDict{Int64,Char} with 2 entries:
SwissDict{Int64, Char} with 2 entries:
1 => 'a'
2 => 'b'

julia> d[3] = 'c';

julia> collect(d)
3-element Array{Pair{Int64,Char},1}:
3-element Vector{Pair{Int64, Char}}:
1 => 'a'
2 => 'b'
3 => 'c'
Expand All @@ -30,7 +30,7 @@ julia> d[1]
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> d
SwissDict{Int64,Char} with 2 entries:
SwissDict{Int64, Char} with 2 entries:
1 => 'a'
3 => 'c'

Expand Down
79 changes: 62 additions & 17 deletions docs/src/trie.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,85 @@
```@meta
DocTestSetup = :(using DataStructures)
```

# Trie

An implementation of the Trie data structure. This is an associative
structure, with AbstractString keys:
structure, with iterable keys:

```julia
t = Trie{Int}()
t["Rob"] = 42
t["Roger"] = 24
haskey(t, "Rob") # true
get(t, "Rob", nothing) # 42
keys(t) # "Rob", "Roger"
keys(subtrie(t, "Ro")) # "b", "ger"
```jldoctest
julia> t = Trie{Char,Int}();


julia> t["Rob"] = 42;


julia> t["Roger"] = 24;


julia> haskey(t, "Rob")
true

julia> get(t, "Rob", nothing)
42

julia> keys(t)
2-element Vector{String}:
"Roger"
"Rob"

julia> keys(subtrie(t, "Ro"))
2-element Vector{String}:
"ger"
"b"
```

Note that the keys don't need to be `String`s:

```jldoctest
julia> t = Trie{Int,Char}();

julia> t[1:3] = 'a';

julia> t[[2,3,5]] = 'b';

julia> keys(t)
2-element Vector{Vector{Int64}}:
[2, 3, 5]
[1, 2, 3]
```

Constructors:

```julia
Trie(keys, values) # construct a Trie with the given keys and values
Trie(keys) # construct a Trie{Void} with the given keys and with values = nothing
Trie(keys) # construct a Trie{K,Nothing} with the given keys and with values = nothing
Trie(kvs::AbstractVector{(K, V)}) # construct a Trie from the given vector of (key, value) pairs
Trie(kvs::AbstractDict{K, V}) # construct a Trie from the given associative structure
```

This package also provides an iterator `partial_path(t::Trie, str)` for looping
over all the nodes encountered in searching for the given string `str`.
This package also provides an iterator `partial_path(t::Trie, prefix)` for looping
over all the nodes encountered in searching for the given `prefix`.
This obviates much of the boilerplate code needed in writing many trie
algorithms. For example, to test whether a trie contains any prefix of a
given string, use:
given string `str`, use:

```julia
seen_prefix(t::Trie, str) = any(v -> v.is_key, partial_path(t, str))
```

`find_prefixes` can be used to find all keys which are prefixes of the given string.

```julia
t = Trie(["A", "ABC", "ABCD", "BCE"])
find_prefixes(t, "ABCDE") # "A", "ABC", "ABCD"
```
```jldoctest
julia> t = Trie(["A", "ABC", "ABCD", "BCE"]);

julia> find_prefixes(t, "ABCDE")
3-element Vector{String}:
"A"
"ABC"
"ABCD"
```

```@meta
DocTestSetup = nothing
```
2 changes: 1 addition & 1 deletion src/heaps/arrays_as_heaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Return `true` if an array is heap-ordered according to the given order.

```jldoctest
julia> a = [1,2,3]
3-element Array{Int64,1}:
3-element Vector{Int64}:
1
2
3
Expand Down
27 changes: 17 additions & 10 deletions src/ordered_robin_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ are taken from 2-tuples `(key,value)` generated by the argument.
# Examples
```jldoctest
julia> OrderedRobinDict([("A", 1), ("B", 2)])
OrderedRobinDict{String,Int64} with 2 entries:
OrderedRobinDict{String, Int64} with 2 entries:
"A" => 1
"B" => 2
```
Expand All @@ -21,7 +21,7 @@ Alternatively, a sequence of pair arguments may be passed.

```jldoctest
julia> OrderedRobinDict("A"=>1, "B"=>2)
OrderedRobinDict{String,Int64} with 2 entries:
OrderedRobinDict{String, Int64} with 2 entries:
"A" => 1
"B" => 2
```
Expand Down Expand Up @@ -94,14 +94,15 @@ Remove all elements from a `collection`.
# Examples
```jldoctest
julia> A = OrderedRobinDict("a" => 1, "b" => 2)
OrderedRobinDict{String,Int64} with 2 entries:
OrderedRobinDict{String, Int64} with 2 entries:
"a" => 1
"b" => 2

julia> empty!(A);


julia> A
OrderedRobinDict{String,Int64} with 0 entries
OrderedRobinDict{String, Int64}()
```
"""
function Base.empty!(h::OrderedRobinDict{K,V}) where {K, V}
Expand Down Expand Up @@ -191,14 +192,15 @@ Return the value stored for the given key, or if no mapping for the key is prese
```jldoctest
julia> d = OrderedRobinDict("a"=>1, "b"=>2, "c"=>3);


julia> get!(d, "a", 5)
1

julia> get!(d, "d", 4)
4

julia> d
OrderedRobinDict{String,Int64} with 4 entries:
OrderedRobinDict{String, Int64} with 4 entries:
"a" => 1
"b" => 2
"c" => 3
Expand Down Expand Up @@ -251,6 +253,7 @@ key is present.
```jldoctest
julia> d = OrderedRobinDict("a"=>1, "b"=>2);


julia> get(d, "a", 3)
1

Expand Down Expand Up @@ -291,7 +294,7 @@ Determine whether a collection has a mapping for a given `key`.
# Examples
```jldoctest
julia> D = OrderedRobinDict('a'=>2, 'b'=>3)
OrderedRobinDict{Char,Int64} with 2 entries:
OrderedRobinDict{Char, Int64} with 2 entries:
'a' => 2
'b' => 3

Expand All @@ -313,7 +316,7 @@ Return the key matching argument `key` if one exists in `collection`, otherwise
# Examples
```jldoctest
julia> D = OrderedRobinDict('a'=>2, 'b'=>3)
OrderedRobinDict{Char,Int64} with 2 entries:
OrderedRobinDict{Char, Int64} with 2 entries:
'a' => 2
'b' => 3

Expand Down Expand Up @@ -364,13 +367,17 @@ Delete and return the mapping for `key` if it exists in `collection`, otherwise
```jldoctest
julia> d = OrderedRobinDict("a"=>1, "b"=>2, "c"=>3);


julia> pop!(d, "a")
1

julia> pop!(d, "d")
ERROR: KeyError: key "d" not found
Stacktrace:
[...]
[1] pop!(h::OrderedRobinDict{String, Int64}, key::String)
@ DataStructures ~/.julia/dev/DataStructures/src/ordered_robin_dict.jl:357
[2] top-level scope
@ none:1

julia> pop!(d, "e", 4)
4
Expand All @@ -389,12 +396,12 @@ Delete the mapping for the given key in a collection, and return the collection.
# Examples
```jldoctest
julia> d = OrderedRobinDict("a"=>1, "b"=>2)
OrderedRobinDict{String,Int64} with 2 entries:
OrderedRobinDict{String, Int64} with 2 entries:
"a" => 1
"b" => 2

julia> delete!(d, "b")
OrderedRobinDict{String,Int64} with 1 entry:
OrderedRobinDict{String, Int64} with 1 entry:
"a" => 1
```
"""
Expand Down
18 changes: 9 additions & 9 deletions src/priorityqueue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ lowest priority element.

```jldoctest
julia> PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
"c" => 1
"a" => 2
"b" => 3
Expand Down Expand Up @@ -214,13 +214,13 @@ Insert the a key `k` into a priority queue `pq` with priority `v`.

```jldoctest
julia> a = PriorityQueue(PriorityQueue("a"=>1, "b"=>2, "c"=>3))
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
"a" => 1
"b" => 2
"c" => 3

julia> enqueue!(a, "d"=>4)
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 4 entries:
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 4 entries:
"a" => 1
"b" => 2
"c" => 3
Expand Down Expand Up @@ -255,7 +255,7 @@ Remove and return the lowest priority key from a priority queue.

```jldoctest
julia> a = PriorityQueue(Base.Order.Forward, ["a" => 2, "b" => 3, "c" => 1])
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
"c" => 1
"a" => 2
"b" => 3
Expand All @@ -264,7 +264,7 @@ julia> dequeue!(a)
"c"

julia> a
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 2 entries:
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
"a" => 2
"b" => 3
```
Expand All @@ -280,7 +280,7 @@ Remove and return a the lowest priority key and value from a priority queue as a

```jldoctest
julia> a = PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
"c" => 1
"a" => 2
"b" => 3
Expand All @@ -289,7 +289,7 @@ julia> dequeue_pair!(a)
"c" => 1

julia> a
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 2 entries:
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
"a" => 2
"b" => 3
```
Expand Down Expand Up @@ -319,12 +319,12 @@ Delete the mapping for the given key in a priority queue, and return the priorit
# Examples
```jldoctest
julia> q = PriorityQueue(Base.Order.Forward, "a"=>2, "b"=>3, "c"=>1)
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
"c" => 1
"a" => 2
"b" => 3
julia> delete!(q, "b")
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 2 entries:
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
"c" => 1
"a" => 2
```
Expand Down
Loading