From c77182f6f5ccbbeabdbc74ffc37b48d56cfd54f0 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 26 Apr 2024 12:47:42 -0400 Subject: [PATCH] Define rand for OrderedIndices --- Project.toml | 2 ++ src/lib/OrderedDictionaries/src/orderedindices.jl | 5 +++++ src/lib/OrderedDictionaries/test/runtests.jl | 11 +++++++++++ 3 files changed, 18 insertions(+) diff --git a/Project.toml b/Project.toml index c94b574..54d9fa3 100644 --- a/Project.toml +++ b/Project.toml @@ -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" @@ -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" diff --git a/src/lib/OrderedDictionaries/src/orderedindices.jl b/src/lib/OrderedDictionaries/src/orderedindices.jl index 066d054..a72658b 100644 --- a/src/lib/OrderedDictionaries/src/orderedindices.jl +++ b/src/lib/OrderedDictionaries/src/orderedindices.jl @@ -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))) diff --git a/src/lib/OrderedDictionaries/test/runtests.jl b/src/lib/OrderedDictionaries/test/runtests.jl index de5a078..15eba08 100644 --- a/src/lib/OrderedDictionaries/test/runtests.jl +++ b/src/lib/OrderedDictionaries/test/runtests.jl @@ -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])