Skip to content

Commit

Permalink
comments added
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzama Zaid authored and Uzama Zaid committed Jul 20, 2022
1 parent 649e97c commit f03737f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions storage/p2p-network.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type P2PNetwork struct {
// the network topology contains list of trees
topology []*tree.Tree

// we use treap to store peers which has enough capacity (capacity > 0).
// we can easily identify the peer which has maximum capacity (the root node of the treap)
// we use treap to store peers which has free capacity (capacity > 0).
// we can easily identify the peer which has the most free capacity (the root node of the treap)
treap *treap.Treap

// keeps track of joint peer's id in a set data structure to ensure ids are unique
Expand Down Expand Up @@ -107,10 +107,10 @@ func (network *P2PNetwork) Trace() []string {

// add: adds the given peer to the network
func (network *P2PNetwork) add(peer *tree.Peer) {
// get peer which has more capacity from the treap
// get peer which has the most free capacity from the treap
parent := network.treap.Get()

// if there are no peers with enough capacity, then add the given peer into a new tree
// if there are no peers with free capacity, then add the given peer into a new tree
if parent == nil {
// new peer will become the root of the tree. so parent should be nil
peer.SetParent(nil)
Expand All @@ -121,21 +121,21 @@ func (network *P2PNetwork) add(peer *tree.Peer) {
// add the new tree into the network topology
network.topology = append(network.topology, tree)

// if the given peer has enough capacity, then insert it into the treap
// if the given peer has free capacity, then insert it into the treap
if peer.Capacity > 0 {
network.treap.Insert(peer)
}

return
}

// add the given peer into the children list of the parent peer which has more capacity
// add the given peer into the children list of the parent peer which has the most free capacity
parent.AddChild(peer)

// update the parent peer in the treap by delete and re insert it
network.treap.Delete(parent.Id)

// only insert peers into the treap, if they have enough capacity
// only insert peers into the treap, if they have free capacity
if parent.Capacity > 0 {
network.treap.Insert(parent)
}
Expand Down
6 changes: 3 additions & 3 deletions storage/treap/treap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// Treap: binary search tree + heap (max heap)
// Binary search tree property: left sub tree keys are less than root + right sub tree keys
// Heap property: children priorities are less than the parent priority
// Use treap to keep track of peers which has most capacity
// Use treap to keep track of peers which has the most free capacity
type Treap struct {
root *node
}
Expand All @@ -21,7 +21,7 @@ func NewTreap() *Treap {
}
}

// Get: returns the peer which has the most capacity (root node)
// Get: returns the peer which has the most free capacity (root node)
func (t *Treap) Get() *tree.Peer {
if t.root == nil {
return nil
Expand Down Expand Up @@ -74,7 +74,7 @@ func (t *Treap) DeepInsert(peer *tree.Peer) {
current := queue[0]
queue = queue[1:]

// insert only if the peer has enough capacity
// insert only if the peer has free capacity
if current.Capacity > 0 {
t.Insert(current)
}
Expand Down
2 changes: 1 addition & 1 deletion storage/tree/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type Peer struct {
Id int
MaxCapacity int
Capacity int // remaining capacity
Capacity int // free capacity
Parent *Peer
Children []*Peer
}
Expand Down

0 comments on commit f03737f

Please sign in to comment.