Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Utilities/Utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ include("cachingoptimizer.jl")
include("universalfallback.jl")

include("CleverDicts.jl")
include("lazy_iterators.jl")

end # module
34 changes: 34 additions & 0 deletions src/Utilities/lazy_iterators.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
struct EmptyVector{T} <: AbstractVector{T} end
Base.size(::EmptyVector) = (0,)
Base.isempty(::EmptyVector) = true
Base.eltype(::EmptyVector{T}) where {T} = T
Base.iterate(::EmptyVector) = nothing

"""
struct LazyMap{T, VT}
f::Function
data::VT
end

Iterator over the elements of `data` mapped by `f`. This is similar to
`Base.Generator(f, data)` except that the `eltype` of a `LazyMap` is given at
construction while the `eltype` of `Base.Generator(f, data)` is `Any`.
"""
struct LazyMap{T, VT}
f::Function
data::VT
end
function LazyMap{T}(f::Function, data) where {T}
return LazyMap{T, typeof(data)}(f, data)
end
Base.size(it::LazyMap) = size(it.data)
function Base.iterate(it::LazyMap, args...)
elem_state = iterate(it.data, args...)
if elem_state === nothing
return nothing
else
return it.f(elem_state[1]), elem_state[2]
end
end
Base.IteratorSize(it::LazyMap) = Base.IteratorSize(it.data)
Base.eltype(::LazyMap{T}) where {T} = T
3 changes: 3 additions & 0 deletions test/Utilities/Utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ end
@testset "CleverDicts" begin
include("CleverDicts.jl")
end
@testset "Lazy iterators" begin
include("lazy_iterators.jl")
end
25 changes: 25 additions & 0 deletions test/Utilities/lazy_iterators.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Test
using MathOptInterface
const MOI = MathOptInterface
const MOIU = MOI.Utilities

@testset "EmptyVector{$T}" for T in [Int, Float64]
v = MOIU.EmptyVector{T}()
@test size(v) == (0,)
@test isempty(v)
@test eltype(v) == T
@test iterate(v) === nothing
c = collect(v)
@test c isa Vector{T}
@test isempty(c)
end

@testset "LazyMap{$T}" for T in [Int, Float64]
v = MOIU.LazyMap{T}(x -> x^2, [2, 3])
@test size(v) == (2,)
@test !isempty(v)
@test eltype(v) == T
c = collect(v)
@test c isa Vector{T}
@test c == [4, 9]
end