Skip to content

Commit e9c2d07

Browse files
articuno12norvig
authored andcommitted
Updated implementation of FIFOQueue (aimacode#403)
* Added test for FIFOQueue * Updated FIFOQueue * Updated FIFOQueue * FIFOQueue using deque * fixed flake8 warnings
1 parent 072f685 commit e9c2d07

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

tests/test_utils.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22
from utils import * # noqa
3-
3+
import random
44

55
def test_removeall_list():
66
assert removeall(4, []) == []
@@ -189,6 +189,53 @@ def test_expr():
189189
assert (expr('GP(x, z) <== P(x, y) & P(y, z)')
190190
== Expr('<==', GP(x, z), P(x, y) & P(y, z)))
191191

192+
def test_FIFOQueue() :
193+
# Create an object
194+
queue = FIFOQueue()
195+
# Generate an array of number to be used for testing
196+
test_data = [ random.choice(range(100)) for i in range(100) ]
197+
# Index of the element to be added in the queue
198+
front_head = 0
199+
# Index of the element to be removed from the queue
200+
back_head = 0
201+
while front_head < 100 or back_head < 100 :
202+
if front_head == 100 : # only possible to remove
203+
# check for pop and append method
204+
assert queue.pop() == test_data[back_head]
205+
back_head += 1
206+
elif back_head == front_head : # only possible to push element into queue
207+
queue.append(test_data[front_head])
208+
front_head += 1
209+
# else do it in a random manner
210+
elif random.random() < 0.5 :
211+
assert queue.pop() == test_data[back_head]
212+
back_head += 1
213+
else :
214+
queue.append(test_data[front_head])
215+
front_head += 1
216+
# check for __len__ method
217+
assert len(queue) == front_head - back_head
218+
# chek for __contains__ method
219+
if front_head - back_head > 0 :
220+
assert random.choice(test_data[back_head:front_head]) in queue
221+
222+
# check extend method
223+
test_data1 = [ random.choice(range(100)) for i in range(50) ]
224+
test_data2 = [ random.choice(range(100)) for i in range(50) ]
225+
# append elements of test data 1
226+
queue.extend(test_data1)
227+
# append elements of test data 2
228+
queue.extend(test_data2)
229+
# reset front_head
230+
front_head = 0
231+
232+
while front_head < 50 :
233+
assert test_data1[front_head] == queue.pop()
234+
front_head += 1
235+
236+
while front_head < 100 :
237+
assert test_data2[front_head - 50] == queue.pop()
238+
front_head += 1
192239

193240
if __name__ == '__main__':
194241
pytest.main()

utils.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ def __ge__(self, odict):
598598
# ______________________________________________________________________________
599599
# Queues: Stack, FIFOQueue, PriorityQueue
600600

601-
# TODO: Possibly use queue.Queue, queue.PriorityQueue
601+
# TODO: queue.PriorityQueue
602602
# TODO: Priority queues may not belong here -- see treatment in search.py
603603

604604

@@ -634,29 +634,32 @@ class FIFOQueue(Queue):
634634

635635
"""A First-In-First-Out Queue."""
636636

637-
def __init__(self):
638-
self.A = []
639-
self.start = 0
637+
def __init__(self, maxlen=None, items=[]):
638+
self.queue = collections.deque(items, maxlen)
640639

641640
def append(self, item):
642-
self.A.append(item)
643-
644-
def __len__(self):
645-
return len(self.A) - self.start
641+
if not self.queue.maxlen or len(self.queue) < self.queue.maxlen:
642+
self.queue.append(item)
643+
else:
644+
raise Exception('FIFOQueue is full')
646645

647646
def extend(self, items):
648-
self.A.extend(items)
647+
if not self.queue.maxlen or len(self.queue) + len(items) <= self.queue.maxlen:
648+
self.queue.extend(items)
649+
else:
650+
raise Exception('FIFOQueue max length exceeded')
649651

650652
def pop(self):
651-
e = self.A[self.start]
652-
self.start += 1
653-
if self.start > 5 and self.start > len(self.A) / 2:
654-
self.A = self.A[self.start:]
655-
self.start = 0
656-
return e
653+
if len(self.queue) > 0:
654+
return self.queue.popleft()
655+
else :
656+
raise Exception('FIFOQueue is empty')
657+
658+
def __len__(self):
659+
return len(self.queue)
657660

658661
def __contains__(self, item):
659-
return item in self.A[self.start:]
662+
return item in self.queue
660663

661664

662665
class PriorityQueue(Queue):

0 commit comments

Comments
 (0)