Skip to content

Commit f9f01f8

Browse files
committed
add comments to time.go
1 parent a7d67ac commit f9f01f8

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

time.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,43 @@
2222

2323
package gaio
2424

25-
// a heap for sorted timeout
25+
// timedHeap is a min-heap that sorts aiocb elements by their deadline.
26+
// It implements the heap.Interface from the container/heap package.
2627
type timedHeap []*aiocb
2728

28-
func (h timedHeap) Len() int { return len(h) }
29-
func (h timedHeap) Less(i, j int) bool { return h[i].deadline.Before(h[j].deadline) }
29+
// Len returns the number of elements in the heap.
30+
func (h timedHeap) Len() int {
31+
return len(h)
32+
}
33+
34+
// Less reports whether the element with index i should sort before the element with index j.
35+
// It is used to order the elements by their deadline in ascending order.
36+
func (h timedHeap) Less(i, j int) bool {
37+
return h[i].deadline.Before(h[j].deadline)
38+
}
39+
40+
// Swap swaps the elements with indexes i and j.
3041
func (h timedHeap) Swap(i, j int) {
3142
h[i], h[j] = h[j], h[i]
3243
h[i].idx = i
3344
h[j].idx = j
3445
}
3546

47+
// Push adds an element to the heap.
48+
// This method is used by the heap.Push function.
3649
func (h *timedHeap) Push(x interface{}) {
3750
*h = append(*h, x.(*aiocb))
3851
n := len(*h)
3952
(*h)[n-1].idx = n - 1
4053
}
54+
55+
// Pop removes and returns the element with the highest priority (lowest deadline).
56+
// This method is used by the heap.Pop function.
4157
func (h *timedHeap) Pop() interface{} {
4258
old := *h
4359
n := len(old)
4460
x := old[n-1]
45-
old[n-1] = nil // avoid memory leak
61+
old[n-1] = nil // Avoid memory leak
4662
*h = old[0 : n-1]
4763
return x
4864
}

0 commit comments

Comments
 (0)