Skip to content

Commit

Permalink
Rename prio -> priority
Browse files Browse the repository at this point in the history
I didn't recognize the shortened version at first, changing to make
reading easier.
  • Loading branch information
Sean-Der committed Apr 16, 2024
1 parent c19dc46 commit 892c5ee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
38 changes: 20 additions & 18 deletions pkg/jitterbuffer/priority_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type PriorityQueue struct {
}

type node struct {
val *rtp.Packet
next *node
prev *node
prio uint16
val *rtp.Packet
next *node
prev *node
priority uint16
}

var (
Expand All @@ -39,44 +39,46 @@ func NewQueue() *PriorityQueue {
}
}

func newNode(val *rtp.Packet, prio uint16) *node {
func newNode(val *rtp.Packet, priority uint16) *node {
return &node{
val: val,
prev: nil,
next: nil,
prio: prio,
val: val,
prev: nil,
next: nil,
priority: priority,
}
}

// Find a packet in the queue with the provided sequence number,
// regardless of position (the packet is retained in the queue)
func (q *PriorityQueue) Find(sqNum uint16) (*rtp.Packet, error) {
if q.next.prio == sqNum {
if q.next.priority == sqNum {
return q.next.val, nil
}

if sqNum < q.next.prio {
if sqNum < q.next.priority {
return nil, ErrInvalidOperation
}

next := q.next
for next != nil {
if next.prio == sqNum {
if next.priority == sqNum {
return next.val, nil
}
next = next.next
}

return nil, ErrNotFound
}

// Push will insert a packet in to the queue in order of sequence number
func (q *PriorityQueue) Push(val *rtp.Packet, prio uint16) {
newPq := newNode(val, prio)
func (q *PriorityQueue) Push(val *rtp.Packet, priority uint16) {
newPq := newNode(val, priority)
if q.next == nil {
q.next = newPq
q.length++
return
}
if prio < q.next.prio {
if priority < q.next.priority {
newPq.next = q.next
q.next.prev = newPq
q.next = newPq
Expand All @@ -86,7 +88,7 @@ func (q *PriorityQueue) Push(val *rtp.Packet, prio uint16) {
head := q.next
prev := q.next
for head != nil {
if prio <= head.prio {
if priority <= head.priority {
break
}
prev = head
Expand Down Expand Up @@ -130,15 +132,15 @@ func (q *PriorityQueue) PopAt(sqNum uint16) (*rtp.Packet, error) {
if q.next == nil {
return nil, ErrInvalidOperation
}
if q.next.prio == sqNum {
if q.next.priority == sqNum {
val := q.next.val
q.next = q.next.next
return val, nil
}
pos := q.next
prev := q.next.prev
for pos != nil {
if pos.prio == sqNum {
if pos.priority == sqNum {
val := pos.val
prev.next = pos.next
if prev.next != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/jitterbuffer/priority_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestPriorityQueue(t *testing.T) {
pkt2 := &rtp.Packet{Header: rtp.Header{SequenceNumber: 5004, Timestamp: 500}, Payload: []byte{0x02}}
q.Push(pkt2, pkt2.SequenceNumber)
assert.Equal(q.next.next.val, pkt2)
assert.Equal(q.next.prio, uint16(5000))
assert.Equal(q.next.next.prio, uint16(5004))
assert.Equal(q.next.priority, uint16(5000))
assert.Equal(q.next.next.priority, uint16(5004))
})
t.Run("Appends many in order", func(*testing.T) {
q := NewQueue()
Expand All @@ -34,11 +34,11 @@ func TestPriorityQueue(t *testing.T) {
last = cur
cur = cur.next
if cur != nil {
assert.Equal(cur.prio, last.prio+1)
assert.Equal(cur.priority, last.priority+1)
}
}
assert.Equal(q.next.prio, uint16(5012))
assert.Equal(last.prio, uint16(5012+99))
assert.Equal(q.next.priority, uint16(5012))
assert.Equal(last.priority, uint16(5012+99))
})
t.Run("Can remove an element", func(*testing.T) {
pkt := &rtp.Packet{Header: rtp.Header{SequenceNumber: 5000, Timestamp: 500}, Payload: []byte{0x02}}
Expand All @@ -65,7 +65,7 @@ func TestPriorityQueue(t *testing.T) {
q.Push(pkt, pkt.SequenceNumber)
assert.Equal(pkt, q.next.val)
assert.Equal(uint16(101), q.Length())
assert.Equal(q.next.prio, uint16(5000))
assert.Equal(q.next.priority, uint16(5000))
})
t.Run("Can find", func(*testing.T) {
q := NewQueue()
Expand Down

0 comments on commit 892c5ee

Please sign in to comment.