StablePartition the channels based on unread count
All checks were successful
the build was successful

This commit is contained in:
Peter Stuifzand 2018-09-02 11:12:07 +02:00
parent 218f3ffa08
commit 348fc5f33f
Signed by: peter
GPG Key ID: 374322D56E5209E8
2 changed files with 64 additions and 0 deletions

View File

@ -34,6 +34,7 @@ import (
"p83.nl/go/ekster/pkg/fetch"
"p83.nl/go/ekster/pkg/microsub"
"p83.nl/go/ekster/pkg/util"
"github.com/gomodule/redigo/redis"
"willnorris.com/go/microformats"
@ -185,6 +186,10 @@ func (b *memoryBackend) ChannelsGetList() ([]microsub.Channel, error) {
}
}
}
util.StablePartition(channels, 0, len(channels), func(i int) bool {
return channels[i].Unread > 0
})
return channels, nil
}

59
pkg/util/algorithm.go Normal file
View File

@ -0,0 +1,59 @@
package util
import "reflect"
func Rotate(a interface{}, f, k, l int) int {
swapper := reflect.Swapper(a)
if f == k {
return l
}
if k == l {
return f
}
next := k
for {
swapper(f, next)
f++
next++
if f == k {
k = next
}
if next == l {
break
}
}
ret := f
for next = k; next != l; {
swapper(f, next)
f++
next++
if f == k {
k = next
} else if next == l {
next = k
}
}
return ret
}
func StablePartition(a interface{}, f, l int, p func(i int) bool) int {
n := l - f
if n == 0 {
return f
}
if n == 1 {
t := f
if p(f) {
t += 1
}
return t
}
m := f + (n / 2)
return Rotate(a, StablePartition(a, f, m, p), m, StablePartition(a, m, l, p))
}