StablePartition the channels based on unread count
All checks were successful
the build was successful
All checks were successful
the build was successful
This commit is contained in:
parent
218f3ffa08
commit
348fc5f33f
|
|
@ -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
59
pkg/util/algorithm.go
Normal 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))
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user