-
Notifications
You must be signed in to change notification settings - Fork 6
/
match_queue.py
50 lines (40 loc) · 1.36 KB
/
match_queue.py
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
from functools import cmp_to_key
class MatchPriorityQueue(object):
"""
This priority queue keeps track of incoming match message for a specific order.
"""
def __init__(self, order):
self.order = order
self.queue = []
def __str__(self):
return ' '.join([str(i) for i in self.queue])
def is_empty(self):
return len(self.queue) == 0
def contains_order(self, order_id):
for _, _, other_order_id, _ in self.queue:
if other_order_id == order_id:
return True
return False
def insert(self, retries, price, order_id, other_quantity):
self.queue.append((retries, price, order_id, other_quantity))
def cmp_items(tup1, tup2):
if tup1[0] < tup2[0]:
return -1
elif tup1[0] > tup2[0]:
return 1
else:
if self.order.is_ask():
if tup1[1] < tup2[1]:
return 1
else:
return -1
else:
if tup1[1] < tup2[1]:
return -1
else:
return 1
self.queue = sorted(self.queue, key=cmp_to_key(cmp_items))
def delete(self):
if not self.queue:
return None
return self.queue.pop(0)