1 module neton.lease.LeaseQueue;
2 
3 import neton.lease.Lessor;
4 
5 // LeaseWithTime contains lease object with a time.
6 // For the lessor's lease heap, time identifies the lease expiration time.
7 // For the lessor's lease checkpoint heap, the time identifies the next lease checkpoint time.
8 struct LeaseWithTime
9 {
10 	long id;
11 	// Unix nanos timestamp.
12 	long time;
13 	int index;
14 }
15 
16 struct LeaseQueue
17 {
18 	private LeaseWithTime[] _queue;
19 
20 	int Len()
21 	{
22 		return cast(int)(_queue.length);
23 	}
24 
25 	bool Less(int i, int j)
26 	{
27 		return _queue[i].time < _queue[j].time;
28 	}
29 
30 	void Swap(int i, int j)
31 	{
32 		auto temp = _queue[j];
33 		_queue[j] = _queue[i];
34 		_queue[i] = temp;
35 		_queue[i].index = i;
36 		_queue[j].index = j;
37 	}
38 
39 	void Push(LeaseWithTime item)
40 	{
41 		auto n = _queue.length;
42 		item.index = cast(int)n;
43 		_queue ~= item;
44 	}
45 
46 	LeaseWithTime Pop()
47 	{
48 		auto old = _queue.dup;
49 		auto n = old.length;
50 		auto item = old[n - 1];
51 		item.index = -1; // for safety
52 		_queue = old[0 .. n - 1];
53 		return item;
54 	}
55 
56 	void clear()
57 	{
58 		_queue.length = 0;
59 	}
60 
61 	LeaseWithTime get(int idx)
62 	{
63 		return _queue[idx];
64 	}
65 }