-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScheduleMap.java
89 lines (74 loc) · 1.89 KB
/
ScheduleMap.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.*;
public class ScheduleMap<T> {
public static class LongEntry<T> {
private long _key;
private T _value;
private LongEntry(long key, T value) {
_key = key;
_value = value;
}
public long getKey() { return _key; }
public T getValue() { return _value; }
}
private ArrayList<LongEntry<T>> _elements;
private int _size;
public ScheduleMap() {
_elements = new ArrayList<LongEntry<T>>();
}
public int size() {
return _size;
}
public long firstKey() {
return _elements.get(0).getKey();
}
public LongEntry<T> pollFirstEntry() {
_size--;
swap(0, _size);
bubbleDown(0);
return _elements.get(_size);
}
public void put(long value, T element) {
if (_elements.size() <= _size) {
_elements.add(null);
}
_elements.set(_size, new LongEntry<T>(value, element));
bubbleUp(_size);
_size++;
}
private void swap(int a, int b) {
var tmp = _elements.get(a);
_elements.set(a, _elements.get(b));
_elements.set(b, tmp);
}
private int parent(int index) {
return (index - 1) / 2;
}
private int left(int index) {
return 2 * index + 1;
}
private int right(int index) {
return 2 * index + 2;
}
private void bubbleUp(int index) {
if (index == 0)
return;
var p = parent(index);
if (_elements.get(index).getKey() < _elements.get(p).getKey()) {
swap(index, p);
bubbleUp(p);
}
}
private void bubbleDown(int index) {
var l = left(index);
var r = right(index);
var lval = l < _size ? _elements.get(l).getKey() : Long.MAX_VALUE;
var rval = r < _size ? _elements.get(r).getKey() : Long.MAX_VALUE;
if (lval <= rval && lval < _elements.get(index).getKey()) {
swap(index, l);
bubbleDown(l);
} else if (rval < lval && rval < _elements.get(index).getKey()) {
swap(index, r);
bubbleDown(r);
}
}
}