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
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ internal class MatchResultSet<T : MatchResultItem>(
) {
private var lowestConfidence: Float = 0f

private val treeSet = TreeSet { o1: T, o2: T ->
// Reverse order for descending sort
-1 * o1.confidence.compareTo(o2.confidence)
}
private val treeSet = TreeSet(
compareByDescending<T> { it.confidence }.thenByDescending { it.subjectId },
)

fun add(element: T): MatchResultSet<T> {
if (lowestConfidence > element.confidence) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, am I reading this wrong or if we first add a high confidence score (say 99), then we would not add any lower results and return a list of just 1 result? Is this the behaviour that we want?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, lowestConfidence is updated only when the set is full. That's confusing...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like if confidence and subjectId are now a composite comparison key, so should work as intended.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are comparing confidence only. The confusion comes from the fact that lowestConfidence is initialized with 0 and updated only when the set is full.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a classic LeetCode problem 🥇. If we only need the top N elements, fully sorting the entire list is unnecessary and can be avoided to enhance performance.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,23 @@ class MatchResultSetTest {
),
)
}

// On equal confidence scores sort by id
@Test
fun `Stores results sorted descending by confidence and id`() {
val set = MatchResultSet<FingerprintMatchResult.Item>(3)

set.add(FingerprintMatchResult.Item("4", 0.4f))
set.add(FingerprintMatchResult.Item("1", 0.4f))
set.add(FingerprintMatchResult.Item("3", 0.3f))
set.add(FingerprintMatchResult.Item("2", 0.3f))

assertThat(set.toList()).isEqualTo(
listOf(
FingerprintMatchResult.Item("4", 0.4f),
FingerprintMatchResult.Item("1", 0.4f),
FingerprintMatchResult.Item("3", 0.3f),
),
)
}
}