Skip to content

Commit

Permalink
closest
Browse files Browse the repository at this point in the history
  • Loading branch information
louisponet committed Sep 8, 2020
1 parent 8dd286f commit 6b4396e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/PStdLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ module PStdLib
include("Geometry.jl")
include("Physics.jl")
include("array.jl")
export fillcopy,
norm2,
getfirst,
separate!,
separate,
separateperm!,
separateperm,
fillcopy,
findclosest,
getclosest

include("string.jl")
include("math.jl")
include("ThreadCaches.jl")
Expand Down
50 changes: 43 additions & 7 deletions src/array.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@export norm2(a::AbstractArray) =
norm2(a::AbstractArray) =
dot(a, a)

Base.zeros(a::AbstractArray{T}) where T =
Expand All @@ -7,7 +7,7 @@ Base.zeros(a::AbstractArray{T}) where T =
"""
Like `filter()[1]`.
"""
@export function getfirst(f::Function, A)
function getfirst(f::Function, A)
for el in A
if f(el)
return el
Expand All @@ -20,7 +20,7 @@ end
Like `separate` but a vector `B` can be given in which the result will be stored.
"""
@export function separate!(f::Function, B::T, A::T) where T
function separate!(f::Function, B::T, A::T) where T
nt, nf = 0, length(A)+1
@inbounds for a in A
if f(a)
Expand All @@ -39,15 +39,15 @@ end
Returns an `array` and `index` where `array[1:index-1]` holds all the values of `A` for which `f` returns `true`, and `array[index:end]` holds those for which `f` returns `false`.
"""
@export separate(f::Function, A::AbstractVector) =
separate(f::Function, A::AbstractVector) =
separate!(f, similar(A), A)

"""
separateperm!(f::Function, ix::AbstractVector{Int}, A::AbstractVector)
Like `separateperm` but accepts a preallocated index vector `ix`.
"""
@export function separateperm!(f::Function, ix::AbstractVector{Int}, A::AbstractVector)
function separateperm!(f::Function, ix::AbstractVector{Int}, A::AbstractVector)
la = length(A)
@assert la <= length(ix) "Please supply an id vector that is of sufficient length (at least $la)."
true_id = 1
Expand All @@ -69,7 +69,7 @@ end
Returns a `Vector{Int}` that can be used with `permute!` to separate `A` into values for which `f` returns `true` and those for which `f` returns `false`, and the first index where `f` returns false.
"""
@export function separateperm(f::Function, A::AbstractVector)
function separateperm(f::Function, A::AbstractVector)
ix = Vector{Int}(undef, length(A))
return separateperm!(f, ix, A)
end
Expand All @@ -80,10 +80,46 @@ end
Fills an array with deep copies of x.
"""
@export function fillcopy(x::T, dims...) where T
function fillcopy(x::T, dims...) where T
out = Array{T, length(dims)}(undef, dims...)
for i in eachindex(out)
out[i] = deepcopy(x)
end
return out
end

"""
findclosest(A::AbstractArray, v; by=(x,y) -> norm(x-y))
Returns the index to the closest value in `A` to `v`, using the two argument function of `by`.
"""
function findclosest(A::AbstractArray{T}, v; by::Function=(x,y) -> norm(x-y)) where {T}
dist = typemax(typeof(by(A[1], v)))
closestid = 0
for (i, a) in enumerate(A)
t = by(a, v)
if t < dist
closestid = i
dist = t
end
end
return closestid
end

"""
getclosest(A::AbstractArray, v; by=(x,y) -> norm(x-y))
Returns the closest value in `A` to `v`, using the two argument function of `by`.
"""
function getclosest(A::AbstractArray{T}, v; by::Function=(x,y) -> norm(x-y)) where {T}
dist = typemax(typeof(by(A[1], v)))
closestval = A[1]
for a in A
t = by(a, v)
if t < dist
closestval = a
dist = t
end
end
return closestval
end

0 comments on commit 6b4396e

Please sign in to comment.