nitc :: IndexMatches :: defaultinit
# An array of IndexMatch instances returned by the ModelIndex
#
# Index matches can be sorted, filtered and truncated.
#
# Thanks to the fluent interface, the index matches can be manipulated in chain
# from a model index query:
#
# ~~~nitish
# var res = index.find("Foo").
# uniq.
# sort(new ScoreComparator, new MEntityComparator).
# limit(10).
# sort(new VisibilityComparator)
# ~~~
class IndexMatches
super Array[IndexMatch]
# Create a new ModelMatches from an array of matches
#
# Elements are copied.
init from_matches(matches: Array[IndexMatch]) do self.add_all matches
# Sort the matches with `comparator` (or a list of comparators)
#
# Return a new IndexMatches instance with the sorted results.
#
# When more than one comparator is given, the comparators are applied in a
# pipeline where the `n`th comparator is applied only if the `n-1`th comparator
# returned 0.
fun sort(comparator: ScoreComparator...): IndexMatches do
var res = to_a
if comparator.length == 1 then
comparator.first.sort res
else
var comparators = new MatchComparators(comparator)
comparators.sort res
end
return new IndexMatches.from_matches(res)
end
# Limit the matches with `limit`
#
# Return a new IndexMatches instance with only the `limit` first matches.
fun limit(limit: Int): IndexMatches do
var res = new Array[IndexMatch]
for match in self do
if res.length >= limit then break
res.add match
end
return new IndexMatches.from_matches(res)
end
# Remove doublons from the matches
#
# Preverse the lowest score of all the matches for a MEntity.
fun uniq: IndexMatches do
var scores = new HashMap[MEntity, IndexMatch]
var res = new Array[IndexMatch]
for match in self do
var mentity = match.mentity
if scores.has_key(mentity) then
var older = scores[mentity]
if match.score < older.score then older.score = match.score
else
scores[mentity] = match
res.add match
end
end
return new IndexMatches.from_matches(res)
end
# Reset score of each matches to follow `self` order
#
# Usefull when you need to apply one sorter over another.
fun rerank: IndexMatches do
var res = new IndexMatches
for match in self do
res.add match
match.score = res.length
end
return res
end
# Aggregate the mentities for all the matches
#
# Preserve the match order.
fun mentities: Array[MEntity] do
var res = new Array[MEntity]
for match in self do res.add match.mentity
return res
end
end
src/model/model_index.nit:429,1--520,3