chore: remove usages of sort.Sort (#6689)

improve language stats rounding:

- Add tests (I had to omit some edge cases as the current method is
non-determistic in some cases, due to random order of map access).
- Document the algorithm used.
- Lower the amount of calculations that need to be done.
- Because of the aforementioned non-determistic don't use stable sort
and instead regular sort.

better sorting for `RepositoryList`:

- Add testing
- Use `slices.Sortfunc` instead of `sort.Sort`.
- Remove the methods needed for `sort.Sort`.

better git tag sorter:

- Use `slices.SortFunc` instead of `sort.Sort`.
- Remove `tagSorter` and its related methods.
- Added testing.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6689
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-01-26 13:30:00 +00:00 committed by Gusted
parent 048af05db9
commit 270a2c7fa3
8 changed files with 97 additions and 51 deletions

View file

@ -4,9 +4,10 @@
package repo
import (
"cmp"
"context"
"math"
"sort"
"slices"
"strings"
"code.gitea.io/gitea/models/db"
@ -67,34 +68,37 @@ func (stats LanguageStatList) getLanguagePercentages() map[string]float32 {
return langPerc
}
// Rounds to 1 decimal point, target should be the expected sum of percs
// Use the quota method to round the percentages to one decimal place while
// keeping the sum of the percentages at 100%.
func roundByLargestRemainder(percs map[string]float32, target float32) {
// Tracks the difference between the sum of percentage and 100%.
leftToDistribute := int(target * 10)
keys := make([]string, 0, len(percs))
type key struct {
language string
remainder float64
}
keys := make([]key, 0, len(percs))
for k, v := range percs {
percs[k] = v * 10
floored := math.Floor(float64(percs[k]))
floored, frac := math.Modf(float64(v * 10))
percs[k] = float32(floored)
leftToDistribute -= int(floored)
keys = append(keys, k)
keys = append(keys, key{language: k, remainder: frac})
}
// Sort the keys by the largest remainder
sort.SliceStable(keys, func(i, j int) bool {
_, remainderI := math.Modf(float64(percs[keys[i]]))
_, remainderJ := math.Modf(float64(percs[keys[j]]))
return remainderI > remainderJ
// Sort the fractional part in an ascending order.
slices.SortFunc(keys, func(b, a key) int {
return cmp.Compare(a.remainder, b.remainder)
})
// Increment the values in order of largest remainder
// As long as the sum of 100% is not reached, add 0.1% percentage.
for _, k := range keys {
percs[k] = float32(math.Floor(float64(percs[k])))
if leftToDistribute > 0 {
percs[k]++
percs[k.language]++
leftToDistribute--
}
percs[k] /= 10
percs[k.language] /= 10
}
}