From d3884d1b6c6c326446f31dfc8ceb7b2e1a7b1c83 Mon Sep 17 00:00:00 2001 From: Jonas Schulze Date: Mon, 8 Nov 2021 13:22:02 +0100 Subject: [PATCH 1/2] Fix outdated doctests by running `make.jl` with `doctest=:fix` inside `makedocs`. Julia version was 1.6.2. --- docs/src/priority-queue.md | 6 +++--- docs/src/robin_dict.md | 6 +++--- docs/src/swiss_dict.md | 6 +++--- src/heaps/arrays_as_heaps.jl | 2 +- src/ordered_robin_dict.jl | 27 ++++++++++++++++--------- src/priorityqueue.jl | 18 ++++++++--------- src/robin_dict.jl | 27 ++++++++++++++++--------- src/swiss_dict.jl | 39 +++++++++++++++++++++--------------- 8 files changed, 76 insertions(+), 55 deletions(-) diff --git a/docs/src/priority-queue.md b/docs/src/priority-queue.md index f98113522..188d04bbd 100644 --- a/docs/src/priority-queue.md +++ b/docs/src/priority-queue.md @@ -33,14 +33,14 @@ 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 @@ -48,7 +48,7 @@ PriorityQueue{Any,Any,Base.Order.ForwardOrdering} with 3 entries: 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 diff --git a/docs/src/robin_dict.md b/docs/src/robin_dict.md index eee655634..849e308e0 100644 --- a/docs/src/robin_dict.md +++ b/docs/src/robin_dict.md @@ -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' @@ -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' diff --git a/docs/src/swiss_dict.md b/docs/src/swiss_dict.md index b620404d8..916de95a0 100644 --- a/docs/src/swiss_dict.md +++ b/docs/src/swiss_dict.md @@ -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' @@ -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' diff --git a/src/heaps/arrays_as_heaps.jl b/src/heaps/arrays_as_heaps.jl index 39a7e53f3..a20f46729 100644 --- a/src/heaps/arrays_as_heaps.jl +++ b/src/heaps/arrays_as_heaps.jl @@ -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 diff --git a/src/ordered_robin_dict.jl b/src/ordered_robin_dict.jl index fcbe29a36..63b14b6bb 100644 --- a/src/ordered_robin_dict.jl +++ b/src/ordered_robin_dict.jl @@ -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 ``` @@ -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 ``` @@ -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} @@ -191,6 +192,7 @@ 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 @@ -198,7 +200,7 @@ 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 @@ -251,6 +253,7 @@ key is present. ```jldoctest julia> d = OrderedRobinDict("a"=>1, "b"=>2); + julia> get(d, "a", 3) 1 @@ -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 @@ -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 @@ -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 @@ -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 ``` """ diff --git a/src/priorityqueue.jl b/src/priorityqueue.jl index 2efd402ee..ba499447a 100644 --- a/src/priorityqueue.jl +++ b/src/priorityqueue.jl @@ -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 @@ -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 @@ -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 @@ -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 ``` @@ -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 @@ -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 ``` @@ -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 ``` diff --git a/src/robin_dict.jl b/src/robin_dict.jl index 8cec85205..1e3b32f1b 100644 --- a/src/robin_dict.jl +++ b/src/robin_dict.jl @@ -12,7 +12,7 @@ are taken from 2-tuples `(key,value)` generated by the argument. # Examples ```jldoctest julia> RobinDict([("A", 1), ("B", 2)]) -RobinDict{String,Int64} with 2 entries: +RobinDict{String, Int64} with 2 entries: "B" => 2 "A" => 1 ``` @@ -21,7 +21,7 @@ Alternatively, a sequence of pair arguments may be passed. ```jldoctest julia> RobinDict("A"=>1, "B"=>2) -RobinDict{String,Int64} with 2 entries: +RobinDict{String, Int64} with 2 entries: "B" => 2 "A" => 1 ``` @@ -259,14 +259,15 @@ Remove all elements from a `collection`. # Examples ```jldoctest julia> A = RobinDict("a" => 1, "b" => 2) -RobinDict{String,Int64} with 2 entries: +RobinDict{String, Int64} with 2 entries: "b" => 2 "a" => 1 julia> empty!(A); + julia> A -RobinDict{String,Int64} with 0 entries +RobinDict{String, Int64}() ``` """ function Base.empty!(h::RobinDict{K,V}) where {K, V} @@ -310,6 +311,7 @@ Return the value stored for the given key, or if no mapping for the key is prese ```jldoctest julia> d = RobinDict("a"=>1, "b"=>2, "c"=>3); + julia> get!(d, "a", 5) 1 @@ -317,7 +319,7 @@ julia> get!(d, "d", 4) 4 julia> d -RobinDict{String,Int64} with 4 entries: +RobinDict{String, Int64} with 4 entries: "c" => 3 "b" => 2 "a" => 1 @@ -374,6 +376,7 @@ key is present. ```jldoctest julia> d = RobinDict("a"=>1, "b"=>2); + julia> get(d, "a", 3) 1 @@ -414,7 +417,7 @@ Determine whether a collection has a mapping for a given `key`. # Examples ```jldoctest julia> D = RobinDict('a'=>2, 'b'=>3) -RobinDict{Char,Int64} with 2 entries: +RobinDict{Char, Int64} with 2 entries: 'a' => 2 'b' => 3 @@ -436,7 +439,7 @@ Return the key matching argument `key` if one exists in `collection`, otherwise # Examples ```jldoctest julia> D = RobinDict('a'=>2, 'b'=>3) -RobinDict{Char,Int64} with 2 entries: +RobinDict{Char, Int64} with 2 entries: 'a' => 2 'b' => 3 @@ -512,13 +515,17 @@ Delete and return the mapping for `key` if it exists in `collection`, otherwise ```jldoctest julia> d = RobinDict("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::RobinDict{String, Int64}, key0::String) + @ DataStructures ~/.julia/dev/DataStructures/src/robin_dict.jl:505 + [2] top-level scope + @ none:1 julia> pop!(d, "e", 4) 4 @@ -547,12 +554,12 @@ Delete the mapping for the given key in a collection, and return the collection. # Examples ```jldoctest julia> d = RobinDict("a"=>1, "b"=>2) -RobinDict{String,Int64} with 2 entries: +RobinDict{String, Int64} with 2 entries: "b" => 2 "a" => 1 julia> delete!(d, "b") -RobinDict{String,Int64} with 1 entry: +RobinDict{String, Int64} with 1 entry: "a" => 1 ``` """ diff --git a/src/swiss_dict.jl b/src/swiss_dict.jl index 3fcc71931..9de3e9c5c 100644 --- a/src/swiss_dict.jl +++ b/src/swiss_dict.jl @@ -12,18 +12,18 @@ are taken from 2-tuples `(key,value)` generated by the argument. # Examples ```jldoctest julia> SwissDict([("A", 1), ("B", 2)]) -SwissDict{String,Int64} with 2 entries: - "B" => 2 +SwissDict{String, Int64} with 2 entries: "A" => 1 + "B" => 2 ``` Alternatively, a sequence of pair arguments may be passed. ```jldoctest julia> SwissDict("A"=>1, "B"=>2) -SwissDict{String,Int64} with 2 entries: - "B" => 2 +SwissDict{String, Int64} with 2 entries: "A" => 1 + "B" => 2 ``` """ mutable struct SwissDict{K,V} <: AbstractDict{K,V} @@ -356,14 +356,15 @@ Remove all elements from a `collection`. # Examples ```jldoctest julia> A = SwissDict("a" => 1, "b" => 2) -SwissDict{String,Int64} with 2 entries: - "b" => 2 +SwissDict{String, Int64} with 2 entries: "a" => 1 + "b" => 2 julia> empty!(A); + julia> A -SwissDict{String,Int64} with 0 entries +SwissDict{String, Int64}() ``` """ function Base.empty!(h::SwissDict{K,V}) where {K, V} @@ -410,6 +411,7 @@ Return the value stored for the given key, or if no mapping for the key is prese ```jldoctest julia> d = SwissDict("a"=>1, "b"=>2, "c"=>3); + julia> get!(d, "a", 5) 1 @@ -417,10 +419,10 @@ julia> get!(d, "d", 4) 4 julia> d -SwissDict{String,Int64} with 4 entries: - "c" => 3 - "b" => 2 +SwissDict{String, Int64} with 4 entries: "a" => 1 + "b" => 2 + "c" => 3 "d" => 4 ``` """ @@ -480,6 +482,7 @@ key is present. ```jldoctest julia> d = SwissDict("a"=>1, "b"=>2); + julia> get(d, "a", 3) 1 @@ -520,7 +523,7 @@ Determine whether a collection has a mapping for a given `key`. # Examples ```jldoctest julia> D = SwissDict('a'=>2, 'b'=>3) -SwissDict{Char,Int64} with 2 entries: +SwissDict{Char, Int64} with 2 entries: 'a' => 2 'b' => 3 @@ -542,7 +545,7 @@ Return the key matching argument `key` if one exists in `collection`, otherwise # Examples ```jldoctest julia> D = SwissDict('a'=>2, 'b'=>3) -SwissDict{Char,Int64} with 2 entries: +SwissDict{Char, Int64} with 2 entries: 'a' => 2 'b' => 3 @@ -575,13 +578,17 @@ Delete and return the mapping for `key` if it exists in `collection`, otherwise ```jldoctest julia> d = SwissDict("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::SwissDict{String, Int64}, key::String) + @ DataStructures ~/.julia/dev/DataStructures/src/swiss_dict.jl:599 + [2] top-level scope + @ none:1 julia> pop!(d, "e", 4) 4 @@ -617,12 +624,12 @@ Delete the mapping for the given key in a collection, and return the collection. # Examples ```jldoctest julia> d = SwissDict("a"=>1, "b"=>2) -SwissDict{String,Int64} with 2 entries: - "b" => 2 +SwissDict{String, Int64} with 2 entries: "a" => 1 + "b" => 2 julia> delete!(d, "b") -SwissDict{String,Int64} with 1 entry: +SwissDict{String, Int64} with 1 entry: "a" => 1 ``` """ From 20d807aa5108d8b62c87792088d8dc7759309b46 Mon Sep 17 00:00:00 2001 From: Jonas Schulze Date: Mon, 8 Nov 2021 13:28:13 +0100 Subject: [PATCH 2/2] Update trie docs --- docs/src/trie.md | 79 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/docs/src/trie.md b/docs/src/trie.md index 64313845a..28741a8c6 100644 --- a/docs/src/trie.md +++ b/docs/src/trie.md @@ -1,32 +1,68 @@ +```@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)) @@ -34,7 +70,16 @@ 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" -``` \ No newline at end of file +```jldoctest +julia> t = Trie(["A", "ABC", "ABCD", "BCE"]); + +julia> find_prefixes(t, "ABCDE") +3-element Vector{String}: + "A" + "ABC" + "ABCD" +``` + +```@meta +DocTestSetup = nothing +```