New slices package

This commit is contained in:
Matt Joiner 2016-07-12 16:40:14 +10:00
parent 7f6b9dbf7a
commit a4e140b939
4 changed files with 10 additions and 8 deletions

View File

@ -21,6 +21,7 @@ import (
"github.com/anacrolix/missinggo" "github.com/anacrolix/missinggo"
"github.com/anacrolix/missinggo/pproffd" "github.com/anacrolix/missinggo/pproffd"
"github.com/anacrolix/missinggo/pubsub" "github.com/anacrolix/missinggo/pubsub"
"github.com/anacrolix/missinggo/slices"
"github.com/anacrolix/sync" "github.com/anacrolix/sync"
"github.com/anacrolix/utp" "github.com/anacrolix/utp"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
@ -1779,7 +1780,7 @@ func (cl *Client) AddMagnet(uri string) (T *Torrent, err error) {
func (cl *Client) AddTorrent(mi *metainfo.MetaInfo) (T *Torrent, err error) { func (cl *Client) AddTorrent(mi *metainfo.MetaInfo) (T *Torrent, err error) {
T, _, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi)) T, _, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
var ss []string var ss []string
missinggo.CastSlice(&ss, mi.Nodes) slices.MakeInto(&ss, mi.Nodes)
cl.AddDHTNodes(ss) cl.AddDHTNodes(ss)
return return
} }

View File

@ -17,6 +17,7 @@ import (
"github.com/anacrolix/missinggo" "github.com/anacrolix/missinggo"
"github.com/anacrolix/missinggo/bitmap" "github.com/anacrolix/missinggo/bitmap"
"github.com/anacrolix/missinggo/prioritybitmap" "github.com/anacrolix/missinggo/prioritybitmap"
"github.com/anacrolix/missinggo/slices"
"github.com/bradfitz/iter" "github.com/bradfitz/iter"
"github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/bencode"
@ -675,5 +676,5 @@ func (c *connection) lastHelpful() time.Time {
if c.t.seeding() { if c.t.seeding() {
lasts = append(lasts, c.lastChunkSent) lasts = append(lasts, c.lastChunkSent)
} }
return missinggo.Max(time.Time.Before, missinggo.ConvertToSliceOfEmptyInterface(lasts)...).(time.Time) return missinggo.Max(time.Time.Before, slices.ToEmptyInterface(lasts)...).(time.Time)
} }

View File

@ -11,8 +11,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/anacrolix/missinggo" "github.com/anacrolix/missinggo/slices"
"github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/bencode"
) )
@ -85,7 +84,7 @@ func (info *Info) BuildFromFilePath(root string) (err error) {
if err != nil { if err != nil {
return return
} }
missinggo.SortSlice(info.Files, func(l, r FileInfo) bool { slices.Sort(info.Files, func(l, r FileInfo) bool {
return strings.Join(l.Path, "/") < strings.Join(r.Path, "/") return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
}) })
err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) { err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {

View File

@ -18,6 +18,7 @@ import (
"github.com/anacrolix/missinggo/itertools" "github.com/anacrolix/missinggo/itertools"
"github.com/anacrolix/missinggo/perf" "github.com/anacrolix/missinggo/perf"
"github.com/anacrolix/missinggo/pubsub" "github.com/anacrolix/missinggo/pubsub"
"github.com/anacrolix/missinggo/slices"
"github.com/bradfitz/iter" "github.com/bradfitz/iter"
"github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/bencode"
@ -442,7 +443,7 @@ func (t *Torrent) writeStatus(w io.Writer, cl *Client) {
fmt.Fprintf(w, "Pending peers: %d\n", len(t.peers)) fmt.Fprintf(w, "Pending peers: %d\n", len(t.peers))
fmt.Fprintf(w, "Half open: %d\n", len(t.halfOpen)) fmt.Fprintf(w, "Half open: %d\n", len(t.halfOpen))
fmt.Fprintf(w, "Active peers: %d\n", len(t.conns)) fmt.Fprintf(w, "Active peers: %d\n", len(t.conns))
missinggo.SortSlice(t.conns, worseConn) slices.Sort(t.conns, worseConn)
for i, c := range t.conns { for i, c := range t.conns {
fmt.Fprintf(w, "%2d. ", i+1) fmt.Fprintf(w, "%2d. ", i+1)
c.WriteStatus(w, t) c.WriteStatus(w, t)
@ -737,7 +738,7 @@ func (t *Torrent) extentPieces(off, _len int64) (pieces []int) {
// pieces, or has been in worser half of the established connections for more // pieces, or has been in worser half of the established connections for more
// than a minute. // than a minute.
func (t *Torrent) worstBadConn() *connection { func (t *Torrent) worstBadConn() *connection {
wcs := missinggo.HeapFromSlice(t.worstUnclosedConns(), worseConn) wcs := slices.AsHeap(t.worstUnclosedConns(), worseConn)
for wcs.Len() != 0 { for wcs.Len() != 0 {
c := heap.Pop(wcs).(*connection) c := heap.Pop(wcs).(*connection)
if c.UnwantedChunksReceived >= 6 && c.UnwantedChunksReceived > c.UsefulChunksReceived { if c.UnwantedChunksReceived >= 6 && c.UnwantedChunksReceived > c.UsefulChunksReceived {
@ -1318,7 +1319,7 @@ func (t *Torrent) SetMaxEstablishedConns(max int) (oldMax int) {
defer t.cl.mu.Unlock() defer t.cl.mu.Unlock()
oldMax = t.maxEstablishedConns oldMax = t.maxEstablishedConns
t.maxEstablishedConns = max t.maxEstablishedConns = max
wcs := missinggo.HeapFromSlice(append([]*connection(nil), t.conns...), worseConn) wcs := slices.AsHeap(append([]*connection(nil), t.conns...), worseConn)
for len(t.conns) > t.maxEstablishedConns && wcs.Len() > 0 { for len(t.conns) > t.maxEstablishedConns && wcs.Len() > 0 {
t.dropConnection(wcs.Pop().(*connection)) t.dropConnection(wcs.Pop().(*connection))
} }