Skip to content

Commit c8240b6

Browse files
authored
change findall into for comprehension (jump-dev#1224)
* change findall into for comprehension * change indexing for iteration on terms * term by term iteration in getindex * fix const out of loop * simplify without outerloop * sorted output * sort indices * sort terms by output index * revert sorting * use findall with fix * make tests order-independent * remove sort of RHSs * white space
1 parent b254c29 commit c8240b6

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/Utilities/functions.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,7 @@ function scalar_terms_at_index(
355355
terms::Vector{<:Union{MOI.VectorAffineTerm,MOI.VectorQuadraticTerm}},
356356
i::Int,
357357
)
358-
I = findall(t -> t.output_index == i, terms)
359-
return map(i -> terms[i].scalar_term, I)
358+
return [term.scalar_term for term in terms if term.output_index == i]
360359
end
361360
function Base.getindex(it::ScalarFunctionIterator{<:VAF}, i::Integer)
362361
return SAF(scalar_terms_at_index(it.f.terms, i), it.f.constants[i])
@@ -378,11 +377,14 @@ function Base.getindex(
378377
I::AbstractVector,
379378
) where {T}
380379
terms = MOI.VectorAffineTerm{T}[]
381-
constant = Vector{T}(undef, length(I))
382-
for (i, j) in enumerate(I)
383-
g = it[j]
384-
append!(terms, map(t -> MOI.VectorAffineTerm(i, t), g.terms))
385-
constant[i] = g.constant
380+
# assume at least one term per index
381+
sizehint!(terms, length(I))
382+
constant = it.f.constants[I]
383+
for term in it.f.terms
384+
idx = findfirst(Base.Fix1(==, term.output_index), I)
385+
if idx !== nothing
386+
push!(terms, MOI.VectorAffineTerm(idx, term.scalar_term))
387+
end
386388
end
387389
return VAF(terms, constant)
388390
end

test/Utilities/functions.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ end
246246
@test g.constant == 5
247247
h = it[[3, 1]]
248248
@test h isa MOI.VectorAffineFunction
249-
@test h.terms == MOI.VectorAffineTerm.([1, 1, 2, 2, 2], MOI.ScalarAffineTerm.([2, 6, 7, 1, 4], [z, x, y, z, x]))
249+
@test sort(h.terms, by=t->t.output_index) == MOI.VectorAffineTerm.([1, 1, 2, 2, 2], MOI.ScalarAffineTerm.([2, 6, 7, 1, 4], [z, x, y, z, x]))
250250
@test MOIU.constant_vector(h) == [5, 2]
251251
F = MOIU.operate(vcat, Int, it[[1, 2]], it[3])
252252
@test F isa MOI.VectorAffineFunction{Int}
253-
@test F.terms == MOI.VectorAffineTerm.([1, 1, 1, 2, 2, 2, 2, 3, 3], MOI.ScalarAffineTerm.([7, 1, 4, 1, 9, 3, 1, 2, 6], [y, z, x, x, z, y, y, z, x]))
253+
@test sort(F.terms, by=t->t.output_index) == MOI.VectorAffineTerm.([1, 1, 1, 2, 2, 2, 2, 3, 3], MOI.ScalarAffineTerm.([7, 1, 4, 1, 9, 3, 1, 2, 6], [y, z, x, x, z, y, y, z, x]))
254254
@test MOIU.constant_vector(F) == MOIU.constant_vector(f)
255255
end
256256
@testset "Indexing on VectorQuadraticFunction" begin

0 commit comments

Comments
 (0)