# Abuser iterator for sorting array, see `sort_fa`
# Implements a sort by permutation
private class SortAbuserIterator[E]
super Iterator[CompareQuery[E]]
# The index of the big loop
var i: Int = 0
# The index of the small loop
var j: Int = 0
# The array to sort
var array: Array[E]
# The query used to communicate with the user.
# For ecological concerns, a unique CompareQuery is instatiated.
var query: nullable CompareQuery[E] = null
redef fun item do return query.as(not null)
init
do
# Initialize the algorithm, see `next` for the rest
if not is_ok then return
query = new CompareQuery[E](array[i], array[j])
end
redef fun is_ok do return i < array.length - 1
redef fun next
do
# Process the last query
if item.res > 0 then
var tmp = array[i]
array[i] = array[j]
array[j] = tmp
end
# Get the next iteration
j += 1
if j >= array.length then
# End of small loop
i += 1
j = i + 1
end
if not is_ok then return
# Prepare the next query
item.a = array[i]
item.b = array[j]
item.res = 0
end
end
lib/for_abuse/for_abuse.nit:93,1--135,3