Skip to content

Commit

Permalink
Merge pull request #3604 from smichel17/building-sort
Browse files Browse the repository at this point in the history
Make number of recent values a parameter
  • Loading branch information
smichel17 authored Dec 23, 2021
2 parents 86c28a9 + a148cf4 commit 583da60
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ abstract class AGroupedImageListQuestAnswerFragment<I,T> : AbstractQuestFormAnsw
}
}

private fun getInitialItems(): List<GroupableDisplayItem<I>> {
return favs.get().mostCommonWithin(6, 30).padWith(topItems).toList()
}
private fun getInitialItems(): List<GroupableDisplayItem<I>> =
favs.get().mostCommonWithin(6, historyCount = 30, first = 1).padWith(topItems).toList()

override fun onClickOk() {
val item = selectedItem!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ private const val MAX_ENTRIES = 100
* items of the sequence, in their original order.
* If there are fewer than `target` unique items, continues counting items
* until that many are found, or the end of the sequence is reached.
* If the first item (i.e. the most recent one) is not null, it is always
* included, displacing the least-common of the other items if necessary.
* If the `first` most recent items are not null, they are always included,
* displacing the least-common of the other items if necessary.
*/
fun <T : Any> Sequence<T?>.mostCommonWithin(target: Int, historyCount: Int): Sequence<T> {
fun <T : Any> Sequence<T?>.mostCommonWithin(target: Int, historyCount: Int, first: Int): Sequence<T> {
val counts = this.countUniqueNonNull(target, historyCount)
val top = counts.keys.sortedByDescending { counts[it]!!.count }.take(target)
val latest = this.take(1).filterNotNull()
val latest = this.take(first).filterNotNull()
val items = (latest + top).distinct().take(target)
return items.sortedBy { counts[it]!!.indexOfFirst }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AddBuildingLevelsForm : AbstractQuestFormAnswerFragment<BuildingLevelsAnsw

private val lastPickedAnswers by lazy {
favs.get()
.mostCommonWithin(5, 30)
.mostCommonWithin(target = 5, historyCount = 30, first = 1)
.sortedWith(compareBy<BuildingLevelsAnswer> { it.levels }.thenBy { it.roofLevels })
.toList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,58 @@ class LastPickedValuesStoreTest {
assertEquals(
"Returns the most common items",
listOf(A, C),
sequenceOf(A, A, B, C, C).mostCommonWithin(2, 99).toList(),
sequenceOf(A, A, B, C, C).mostCommonWithin(2, 99, 1).toList(),
)
assertEquals(
"Sorted in the original order",
listOf(A, C, B),
sequenceOf(A, C, B, B, C).mostCommonWithin(4, 99).toList(),
sequenceOf(A, C, B, B, C).mostCommonWithin(4, 99, 1).toList(),
)
assertEquals(
"Doesn't return nulls",
listOf(A),
sequenceOf(A, null).mostCommonWithin(2, 99).toList()
sequenceOf(A, null).mostCommonWithin(2, 99, 1).toList()
)
}

@Test fun `mostCommonWithin first item special case`() {
@Test fun `mostCommonWithin first item(s) special case`() {
assertEquals(
"Included even if it is not the most common",
listOf(A, B),
sequenceOf(A, B, B, C, C).mostCommonWithin(2, 99).toList(),
sequenceOf(A, B, B, C, C).mostCommonWithin(2, 99, 1).toList(),
)
assertEquals(
"Not included if null",
listOf(B, C),
sequenceOf(null, B, C).mostCommonWithin(2, 99).toList(),
sequenceOf(null, B, C).mostCommonWithin(2, 99, 1).toList(),
)
assertEquals(
"Not duplicated if it was already among the most common",
listOf(A, B),
sequenceOf(A, B, A, B).mostCommonWithin(4, 99).toList(),
sequenceOf(A, B, A, B).mostCommonWithin(4, 99, 1).toList(),
)
}

@Test fun `mostCommonWithin counts the right number of items`() {
assertEquals(
"Always counts the minimum",
listOf(A, C),
sequenceOf(A, B, C, C).mostCommonWithin(2, 4).toList(),
sequenceOf(A, B, C, C).mostCommonWithin(2, 4, 1).toList(),
)
assertEquals(
"Counts more to find the target",
listOf(A, B, C),
sequenceOf(A, B, C, C).mostCommonWithin(3, 1).toList(),
sequenceOf(A, B, C, C).mostCommonWithin(3, 1, 1).toList(),
)
assertEquals(
"Counts nulls towards the minimum",
listOf(A, B),
sequenceOf(A, null, B, null, C, C).mostCommonWithin(2, 3).toList(),
sequenceOf(A, null, B, null, C, C).mostCommonWithin(2, 3, 1).toList(),
)
assertEquals(
"Doesn't count null toward the target",
listOf(A, B, C),
sequenceOf(A, null, B, null, C, C).mostCommonWithin(3, 2).toList(),
sequenceOf(A, null, B, null, C, C).mostCommonWithin(3, 2, 1).toList(),
)
}

Expand Down

0 comments on commit 583da60

Please sign in to comment.