Skip to content

Commit e794383

Browse files
authored
Implement Base.allequal and Base.allunique for weight vectors (#894)
* Implement `Base.allequal` and `Base.allunique` for weight vectors * Combine definitions
1 parent 7ca72c7 commit e794383

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StatsBase"
22
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
33
authors = ["JuliaStats"]
4-
version = "0.34.1"
4+
version = "0.34.2"
55

66
[deps]
77
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"

src/weights.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@ Base.:(==)(x::UnitWeights, y::UnitWeights) = (x.len == y.len)
385385
Base.isequal(x::AbstractWeights, y::AbstractWeights) = false
386386
Base.:(==)(x::AbstractWeights, y::AbstractWeights) = false
387387

388+
# https://github.com/JuliaLang/julia/pull/43354
389+
if VERSION >= v"1.8.0-DEV.1494" # 98e60ffb11ee431e462b092b48a31a1204bd263d
390+
Base.allequal(wv::AbstractWeights) = allequal(wv.values)
391+
Base.allequal(::UnitWeights) = true
392+
end
393+
Base.allunique(wv::AbstractWeights) = allunique(wv.values)
394+
Base.allunique(wv::UnitWeights) = length(wv) <= 1
395+
388396
##### Weighted sum #####
389397

390398
## weighted sum over vectors

test/weights.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,4 +574,40 @@ end
574574
end
575575
end
576576

577+
@testset "allequal and allunique" begin
578+
# General weights
579+
for f in (weights, aweights, fweights, pweights)
580+
@test allunique(f(Float64[]))
581+
@test allunique(f([0.4]))
582+
@test allunique(f([0.4, 0.3]))
583+
@test !allunique(f([0.4, 0.4]))
584+
@test allunique(f([0.4, 0.3, 0.5]))
585+
@test !allunique(f([0.4, 0.4, 0.5]))
586+
@test allunique(f([0.4, 0.3, 0.5, 0.35]))
587+
@test !allunique(f([0.4, 0.3, 0.5, 0.4]))
588+
589+
if isdefined(Base, :allequal)
590+
@test allequal(f(Float64[]))
591+
@test allequal(f([0.4]))
592+
@test allequal(f([0.4, 0.4]))
593+
@test !allequal(f([0.4, 0.3]))
594+
@test allequal(f([0.4, 0.4, 0.4, 0.4]))
595+
@test !allunique(f([0.4, 0.4, 0.3, 0.4]))
596+
end
597+
end
598+
599+
# Uniform weights
600+
@test allunique(uweights(0))
601+
@test allunique(uweights(1))
602+
@test !allunique(uweights(2))
603+
@test !allunique(uweights(5))
604+
605+
if isdefined(Base, :allequal)
606+
@test allequal(uweights(0))
607+
@test allequal(uweights(1))
608+
@test allequal(uweights(2))
609+
@test allequal(uweights(5))
610+
end
611+
end
612+
577613
end # @testset StatsBase.Weights

0 commit comments

Comments
 (0)