From a4e140b939210705a9a24c434df23415954bbfa1 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 12 Jul 2016 16:40:14 +1000 Subject: [PATCH] New slices package --- client.go | 3 ++- connection.go | 3 ++- metainfo/metainfo.go | 5 ++--- torrent.go | 7 ++++--- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index e5cdf0c2..1491b25d 100644 --- a/client.go +++ b/client.go @@ -21,6 +21,7 @@ import ( "github.com/anacrolix/missinggo" "github.com/anacrolix/missinggo/pproffd" "github.com/anacrolix/missinggo/pubsub" + "github.com/anacrolix/missinggo/slices" "github.com/anacrolix/sync" "github.com/anacrolix/utp" "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) { T, _, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi)) var ss []string - missinggo.CastSlice(&ss, mi.Nodes) + slices.MakeInto(&ss, mi.Nodes) cl.AddDHTNodes(ss) return } diff --git a/connection.go b/connection.go index a58647ad..80ef2999 100644 --- a/connection.go +++ b/connection.go @@ -17,6 +17,7 @@ import ( "github.com/anacrolix/missinggo" "github.com/anacrolix/missinggo/bitmap" "github.com/anacrolix/missinggo/prioritybitmap" + "github.com/anacrolix/missinggo/slices" "github.com/bradfitz/iter" "github.com/anacrolix/torrent/bencode" @@ -675,5 +676,5 @@ func (c *connection) lastHelpful() time.Time { if c.t.seeding() { 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) } diff --git a/metainfo/metainfo.go b/metainfo/metainfo.go index 9e5fddb1..f50190eb 100644 --- a/metainfo/metainfo.go +++ b/metainfo/metainfo.go @@ -11,8 +11,7 @@ import ( "strings" "time" - "github.com/anacrolix/missinggo" - + "github.com/anacrolix/missinggo/slices" "github.com/anacrolix/torrent/bencode" ) @@ -85,7 +84,7 @@ func (info *Info) BuildFromFilePath(root string) (err error) { if err != nil { 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, "/") }) err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) { diff --git a/torrent.go b/torrent.go index af6b2f25..179d61ea 100644 --- a/torrent.go +++ b/torrent.go @@ -18,6 +18,7 @@ import ( "github.com/anacrolix/missinggo/itertools" "github.com/anacrolix/missinggo/perf" "github.com/anacrolix/missinggo/pubsub" + "github.com/anacrolix/missinggo/slices" "github.com/bradfitz/iter" "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, "Half open: %d\n", len(t.halfOpen)) 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 { fmt.Fprintf(w, "%2d. ", i+1) 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 // than a minute. func (t *Torrent) worstBadConn() *connection { - wcs := missinggo.HeapFromSlice(t.worstUnclosedConns(), worseConn) + wcs := slices.AsHeap(t.worstUnclosedConns(), worseConn) for wcs.Len() != 0 { c := heap.Pop(wcs).(*connection) 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() oldMax = t.maxEstablishedConns 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 { t.dropConnection(wcs.Pop().(*connection)) } -- 2.48.1