summaryrefslogtreecommitdiff
path: root/requests/timesheap.go
blob: 73c46c8a3a1868530a4a445c42b0f2133b37f4a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 *  Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */

// File requests/timesheap.go provides minimum heap implementation for requestTime.
// timesHeap structure provides easy API for usage in higher layers of requests
// package. It uses timesHeapContainer for heap implementation.

package requests

import (
	"container/heap"
)

// timesHeap implements requestTime heap. Its methods build API easy to be used
// by higher layers of requests package.
type timesHeap struct {
	con timesHeapContainer
}

// newTimesHeap creates new timesHeap object and initializes it as an empty heap.
func newTimesHeap() *timesHeap {
	h := new(timesHeap)
	heap.Init(&h.con)
	return h
}

// Len returns size of the heap.
func (h *timesHeap) Len() int {
	return h.con.Len()
}

// Min returns the minimum heap element (associated with the earliest time).
// The returned element is not removed from the heap. Size of the heap doesn't
// change. It panics if the heap is empty.
func (h *timesHeap) Min() requestTime {
	if h.con.Len() == 0 {
		panic("cannot get Min of empty heap")
	}
	return h.con[0]
}

// Pop removes and returns the minimum heap element that is associated with
// earliest time. Heap's size is decreased by one.
// Method panics if the heap is empty.
func (h *timesHeap) Pop() requestTime {
	return heap.Pop(&h.con).(requestTime)
}

// Push adds the requestTime element to the heap, keeping its minimum value
// property. Heap's size is increased by one.
func (h *timesHeap) Push(t requestTime) {
	heap.Push(&h.con, t)
}