Skip to content

Commit

Permalink
Define rand for OrderedIndices
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 26, 2024
1 parent b8fd114 commit c77182f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66"
Expand All @@ -35,6 +36,7 @@ KaHyPar = "0.3.1"
LinearAlgebra = "1.7"
Metis = "1.4"
PackageExtensionCompat = "1"
Random = "1.7"
SimpleTraits = "0.9"
SparseArrays = "1.7"
SplitApplyCombine = "1.2.2"
Expand Down
5 changes: 5 additions & 0 deletions src/lib/OrderedDictionaries/src/orderedindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ function Dictionaries.deletetoken!(indices::OrderedIndices, token)
return indices
end

using Random: Random
function Dictionaries.randtoken(rng::Random.AbstractRNG, indices::OrderedIndices)
return rand(rng, Base.OneTo(length(indices)))
end

# Circumvents https://github.com/andyferris/Dictionaries.jl/pull/140
function Base.map(f, indices::OrderedIndices)
return OrderedDictionary(indices, map(f, ordered_indices(indices)))
Expand Down
11 changes: 11 additions & 0 deletions src/lib/OrderedDictionaries/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ using Test: @test, @testset
@test i[ords[1]] == "x1"
@test i[ords[2]] == "x2"
@test i[ords[3]] == "x3"

i = OrderedIndices(["x1", "x2", "x3"])
d = Dictionary(["x1", "x2", "x3"], zeros(Int, 3))
for _ in 1:50
r = rand(i)
@test r i
d[r] += 1
end
for k in i
@test d[k] > 0
end
end
@testset "OrderedDictionaries" begin
d = OrderedDictionary(["x1", "x2", "x3"], [1, 2, 3])
Expand Down

0 comments on commit c77182f

Please sign in to comment.