File tree Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Original file line number Diff line number Diff line change 22
22
23
23
package gaio
24
24
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.
26
27
type timedHeap []* aiocb
27
28
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.
30
41
func (h timedHeap ) Swap (i , j int ) {
31
42
h [i ], h [j ] = h [j ], h [i ]
32
43
h [i ].idx = i
33
44
h [j ].idx = j
34
45
}
35
46
47
+ // Push adds an element to the heap.
48
+ // This method is used by the heap.Push function.
36
49
func (h * timedHeap ) Push (x interface {}) {
37
50
* h = append (* h , x .(* aiocb ))
38
51
n := len (* h )
39
52
(* h )[n - 1 ].idx = n - 1
40
53
}
54
+
55
+ // Pop removes and returns the element with the highest priority (lowest deadline).
56
+ // This method is used by the heap.Pop function.
41
57
func (h * timedHeap ) Pop () interface {} {
42
58
old := * h
43
59
n := len (old )
44
60
x := old [n - 1 ]
45
- old [n - 1 ] = nil // avoid memory leak
61
+ old [n - 1 ] = nil // Avoid memory leak
46
62
* h = old [0 : n - 1 ]
47
63
return x
48
64
}
You can’t perform that action at this time.
0 commit comments