mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-10 21:09:22 +01:00
Changed implementation of queue.
This commit is contained in:
parent
ebb30d7530
commit
67e072c746
102
src/fix.py
102
src/fix.py
@ -140,89 +140,91 @@ class set(object):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __getstate__(self):
|
## def __getstate__(self):
|
||||||
return self.d
|
## return self.d
|
||||||
|
|
||||||
def __setstate__(self, d):
|
## def __setstate__(self, d):
|
||||||
self.d = d
|
## self.d = d
|
||||||
|
|
||||||
|
|
||||||
class queue(dict):
|
class queue(object):
|
||||||
"""An FIFO Queue, O(1) for all operations."""
|
__slots__ = ('front', 'back')
|
||||||
__slots__ = ('first', 'last')
|
|
||||||
def __init__(self, seq=()):
|
def __init__(self, seq=()):
|
||||||
self.first = 0
|
self.back = []
|
||||||
self.last = 0
|
self.front = []
|
||||||
for elt in seq:
|
for elt in seq:
|
||||||
self.enqueue(elt)
|
self.enqueue(elt)
|
||||||
|
|
||||||
def enqueue(self, elt):
|
def enqueue(self, elt):
|
||||||
dict.__setitem__(self, self.last, elt)
|
self.back.append(elt)
|
||||||
self.last += 1
|
|
||||||
|
|
||||||
def peek(self):
|
|
||||||
try:
|
|
||||||
return dict.__getitem__(self, self.first)
|
|
||||||
except KeyError:
|
|
||||||
raise IndexError, 'peek into empty queue'
|
|
||||||
|
|
||||||
def dequeue(self):
|
def dequeue(self):
|
||||||
try:
|
try:
|
||||||
ret = dict.__getitem__(self, self.first)
|
return self.front.pop()
|
||||||
dict.__delitem__(self, self.first)
|
except IndexError:
|
||||||
self.first += 1
|
self.back.reverse()
|
||||||
return ret
|
self.front = self.back
|
||||||
except KeyError:
|
self.back = []
|
||||||
raise IndexError, 'dequeue from empty queue'
|
return self.front.pop()
|
||||||
|
|
||||||
|
def peek(self):
|
||||||
|
if self.front:
|
||||||
|
return self.front[-1]
|
||||||
|
else:
|
||||||
|
return self.back[0]
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self.last - self.first
|
return len(self.front) + len(self.back)
|
||||||
|
|
||||||
def __contains__(self, elt):
|
def __contains__(self, elt):
|
||||||
return elt in self.itervalues()
|
return elt in self.front or elt in self.back
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for i in xrange(self.first, self.last):
|
for elt in reviter(self.front):
|
||||||
yield dict.__getitem__(self, i)
|
yield elt
|
||||||
|
for elt in self.back:
|
||||||
|
yield elt
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if len(self) == len(other):
|
if len(self) == len(other):
|
||||||
for (x, y) in zip(self, other):
|
otheriter = iter(other)
|
||||||
if x != y:
|
for elt in self:
|
||||||
|
otherelt = otheriter.next()
|
||||||
|
if not (elt == otherelt):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return 'queue([%s])' % ', '.join(map(repr, self.itervalues()))
|
return 'queue([%s])' % ', '.join(map(repr, self))
|
||||||
|
|
||||||
def __getitem__(self, i):
|
def __getitem__(self, oidx):
|
||||||
try:
|
(m, idx) = divmod(oidx, len(self))
|
||||||
if i >= 0:
|
if m and m != -1:
|
||||||
return dict.__getitem__(self, i+self.first)
|
raise IndexError, oidx
|
||||||
|
if len(self.front) > idx:
|
||||||
|
return self.front[-(idx+1)]
|
||||||
else:
|
else:
|
||||||
return dict.__getitem__(self, i+self.last)
|
return self.back[(idx-len(self.front))]
|
||||||
except KeyError:
|
|
||||||
raise IndexError, i
|
|
||||||
|
|
||||||
def __setitem__(self, i, v):
|
def __setitem__(self, oidx, value):
|
||||||
try:
|
(m, idx) = divmod(oidx, len(self))
|
||||||
if i >= 0:
|
if m and m != -1:
|
||||||
dict.__setitem__(self, i+self.first, v)
|
raise IndexError, oidx
|
||||||
|
if len(self.front) > idx:
|
||||||
|
self.front[-(idx+1)] = value
|
||||||
else:
|
else:
|
||||||
dict.__setitem__(self, i+self.last, v)
|
self.back[(idx-len(self.front))] = value
|
||||||
except KeyError:
|
|
||||||
raise IndexError, i
|
|
||||||
|
|
||||||
def __getstate__(self):
|
def __getstate__(self):
|
||||||
return (self.first, self.last, dict(self))
|
return (list(self),)
|
||||||
|
|
||||||
|
def __setstate__(self, (L,)):
|
||||||
|
L.reverse()
|
||||||
|
self.front = L
|
||||||
|
self.back = []
|
||||||
|
|
||||||
def __setstate__(self, (first, last, d)):
|
|
||||||
self.first = first
|
|
||||||
self.last = last
|
|
||||||
for (k, v) in d.iteritems():
|
|
||||||
dict.__setitem__(self, k, v)
|
|
||||||
|
|
||||||
class MaxLengthQueue(queue):
|
class MaxLengthQueue(queue):
|
||||||
__slots__ = ('length',)
|
__slots__ = ('length',)
|
||||||
|
Loading…
Reference in New Issue
Block a user