Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
Merge 619741d into 1bfd55a
Browse files Browse the repository at this point in the history
  • Loading branch information
rdeits committed Aug 8, 2018
2 parents 1bfd55a + 619741d commit 2c83801
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.7-alpha
julia 0.7-rc1

IterTools
StaticArrays 0.5.1
Expand Down
1 change: 0 additions & 1 deletion src/GeometryTypes.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
__precompile__()
module GeometryTypes

using StaticArrays
Expand Down
16 changes: 11 additions & 5 deletions src/baseutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ best_arg, best_val = argmax(f, iter)
Computes the first element of iterator, on which f takes its maximum.
"""
function argmax(f, iter)
state = start(iter)
best_arg, state = next(iter, state)
iter_result = iterate(iter)
if iter_result === nothing
throw(ArgumentError("Cannot compute the argmax of an empty collection"))
end
best_arg, state = iter_result
best_val = f(best_arg)
while !done(iter,state)
arg, state = next(iter,state)
while true
iter_result = iterate(iter, state)
if iter_result === nothing
break
end
arg, state = iter_result
val = f(arg)
if val > best_val
best_val = val
Expand All @@ -40,7 +47,6 @@ function argmax(f, iter)
best_arg, best_val
end


w_component(::Type{Point{N, T}}) where {N, T} = T(1)
w_component(::Type{Vec{N, T}}) where {N, T} = T(0)

Expand Down
13 changes: 13 additions & 0 deletions test/baseutils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@testset "argmax" begin
arg, val = GeometryTypes.argmax(sin, range(0, stop=π, length=101))
@test arg π/2
@test val 1

arg, val = GeometryTypes.argmax(cos, range(0, stop=π, length=101))
@test arg 0 # 0, not π, because argmax requires a subsequent value to be *strictly better* than the current best
@test val 1.0

@test @allocated(GeometryTypes.argmax(sin, range(0, stop=π, length=101))) == 0

@test_throws ArgumentError GeometryTypes.argmax(identity, [])
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using Compat.LinearAlgebra


@testset "GeometryTypes" begin
include("baseutils.jl")
include("convexhulls.jl")
include("polygons.jl")
include("hyperrectangles.jl")
Expand Down

0 comments on commit 2c83801

Please sign in to comment.