Skip to content

Commit

Permalink
Replaced all custom append functions with the new native function.
Browse files Browse the repository at this point in the history
  • Loading branch information
royger committed Nov 4, 2010
1 parent cd192ac commit 07fd0d3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 83 deletions.
23 changes: 4 additions & 19 deletions Choke/ChokeMgr.go
Expand Up @@ -41,21 +41,6 @@ func NewChokeMgr(st stats.Stats, pm peers.PeerMgr) (c *ChokeMgr, err os.Error) {
return
}


func appendPeer(slice []*PeerChoke, data *PeerChoke) []*PeerChoke {
l := len(slice)
if l + 1 > cap(slice) { // reallocate
// Allocate 10 more slots
newSlice := make([]*PeerChoke, (l+10))
// The copy function is predeclared and works for any slice type.
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:l+1]
slice[l] = data
return slice
}

func (l Speed) Len() int { return len(l) }
func (l Speed) Less(i, j int) bool { return l[i].speed < l[j].speed }
func (l Speed) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
Expand All @@ -68,7 +53,7 @@ func SelectInterested(peers []*PeerChoke) (interested []*PeerChoke) {
interested = make([]*PeerChoke, 0, 10)
for _, peer := range(peers) {
if peer.peer_interested && !peer.snubbed {
interested = appendPeer(interested, peer)
interested = append(interested, peer)
}
}
return
Expand All @@ -78,7 +63,7 @@ func SelectUninterested(peers []*PeerChoke) (uninterested []*PeerChoke) {
uninterested = make([]*PeerChoke, 0, 10)
for _, peer := range(peers) {
if !peer.peer_interested {
uninterested = appendPeer(uninterested, peer)
uninterested = append(uninterested, peer)
}
}
return
Expand All @@ -88,7 +73,7 @@ func SelectChoked(peers []*PeerChoke) (choked []*PeerChoke) {
choked = make([]*PeerChoke, 0, 10)
for _, peer := range(peers) {
if !peer.unchoke && peer.am_choking {
choked = appendPeer(choked, peer)
choked = append(choked, peer)
}
}
return
Expand Down Expand Up @@ -139,7 +124,7 @@ func (c *ChokeMgr) RequestPeers() []*PeerChoke {
if stat, ok := stats[addr]; ok {
p.speed = stat.Speed
}
peers = appendPeer(peers, p)
peers = append(peers, p)
}
//log.Println("ChokeMgr -> Finished processing peer")
}
Expand Down
2 changes: 1 addition & 1 deletion Peers/Peer.go
Expand Up @@ -456,7 +456,7 @@ func (p *Peer) Close() {
p.stats.Update(p.addr, 0, 0)
if p.wire != nil {
p.wire.Close()
p.wire = nil
//p.wire = nil
}
// Finished
close(p.incoming)
Expand Down
16 changes: 1 addition & 15 deletions Peers/PieceData.go
Expand Up @@ -136,7 +136,7 @@ func (pd *PieceData) SearchPeers(rpiece, rblock, size int64, our_addr string) (o
// Add to return array
//others[i] = addr
//i++
others = appendString(others, addr)
others = append(others, addr)
// Remove from list
pd.peers[addr][ref] = 0, false
// If peer list is empty, remove peer
Expand Down Expand Up @@ -247,17 +247,3 @@ func (pd *PieceData) Clean() {
}
}
}

func appendString(slice []string, data string) []string {
l := len(slice)
if l + 1 > cap(slice) { // reallocate
// Allocate 10 more slots
newSlice := make([]string, (l+10))
// The copy function is predeclared and works for any slice type.
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:l+1]
slice[l] = data
return slice
}
54 changes: 6 additions & 48 deletions bencode/decode.go
Expand Up @@ -124,7 +124,7 @@ func (torrent *Input) scanTorrent(metainfo *Torrent) (err os.Error) {
switch s {
case "announce":
metainfo.Announce = torrent.getString()
metainfo.Announce_list = appendString(metainfo.Announce_list, metainfo.Announce)
metainfo.Announce_list = append(metainfo.Announce_list, metainfo.Announce)
case "comment":
metainfo.Comment = torrent.getString()
case "created by":
Expand All @@ -140,7 +140,7 @@ func (torrent *Input) scanTorrent(metainfo *Torrent) (err os.Error) {
case "announce-list":
torrent.match(list)
for e := torrent.match(list); e == nil; e = torrent.match(list) {
metainfo.Announce_list = appendString(metainfo.Announce_list, torrent.getString())
metainfo.Announce_list = append(metainfo.Announce_list, torrent.getString())
torrent.match(end)
}
torrent.match(end)
Expand Down Expand Up @@ -214,7 +214,7 @@ func (torrent *Input) scanTorrent(metainfo *Torrent) (err os.Error) {
metainfo.Infohash += "4:pathl"
for i, e := 0, torrent.match(str); e == nil; i, e = i+1, torrent.match(str) {
s := torrent.getString()
p = appendString(p, s)
p = append(p, s)
metainfo.Infohash += strconv.Itoa(len(s)) + ":" + s
}
torrent.match(end)
Expand All @@ -227,7 +227,7 @@ func (torrent *Input) scanTorrent(metainfo *Torrent) (err os.Error) {
}
torrent.match(end)
metainfo.Infohash += "e"
metainfo.Info.Files = appendFile(metainfo.Info.Files, File{Length: l, Path: p, Md5sum: m})
metainfo.Info.Files = append(metainfo.Info.Files, File{Length: l, Path: p, Md5sum: m})
}
torrent.match(end)
metainfo.Infohash += "e"
Expand Down Expand Up @@ -288,7 +288,7 @@ func (input *Input) scanTracker(tracker *Tracker) (err os.Error) {
port = input.getInteger()
}
}
tracker.Peers = appendPeer(tracker.Peers, Peer{Peer_id: id, Ip: ip, Port: port})
tracker.Peers = append(tracker.Peers, Peer{Peer_id: id, Ip: ip, Port: port})
input.match(end)
}
input.match(end)
Expand All @@ -298,7 +298,7 @@ func (input *Input) scanTracker(tracker *Tracker) (err os.Error) {
for i := 0; i < len(peers); i = i+6 {
ip := fmt.Sprintf("%d.%d.%d.%d", peers[i+0], peers[i+1], peers[i+2], peers[i+3])
port := int64(binary.BigEndian.Uint16(peers[i+4:i+6]))
tracker.Peers = appendPeer(tracker.Peers, Peer{Ip: ip, Port: port})
tracker.Peers = append(tracker.Peers, Peer{Ip: ip, Port: port})
}
}
}
Expand All @@ -307,48 +307,6 @@ func (input *Input) scanTracker(tracker *Tracker) (err os.Error) {
return
}

func appendString(slice []string, data string) []string {
l := len(slice)
if l + 1 > cap(slice) { // reallocate
// Allocate 10 more slots
newSlice := make([]string, (l+10))
// The copy function is predeclared and works for any slice type.
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:l+1]
slice[l] = data
return slice
}

func appendFile(slice []File, data File) []File {
l := len(slice)
if l + 1 > cap(slice) { // reallocate
// Allocate 10 more slots
newSlice := make([]File, (l+10))
// The copy function is predeclared and works for any slice type.
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:l+1]
slice[l] = data
return slice
}

func appendPeer(slice []Peer, data Peer) []Peer {
l := len(slice)
if l + 1 > cap(slice) { // reallocate
// Allocate 10 more slots
newSlice := make([]Peer, (l+10))
// The copy function is predeclared and works for any slice type.
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:l+1]
slice[l] = data
return slice
}

func Parse(file io.ReadCloser) (metainfo *Torrent, err os.Error) {
metainfo = new(Torrent)
metainfo.Announce_list = make([]string, 0, 10)
Expand Down

0 comments on commit 07fd0d3

Please sign in to comment.