2003-04-22 10:27:11 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
Data structures for Python.
|
|
|
|
"""
|
|
|
|
|
2003-08-07 04:48:44 +02:00
|
|
|
## from __future__ import generators
|
2003-04-22 13:00:28 +02:00
|
|
|
|
2003-10-05 14:47:19 +02:00
|
|
|
import fix
|
2003-04-22 13:00:28 +02:00
|
|
|
|
2003-04-22 10:27:11 +02:00
|
|
|
import types
|
2003-10-24 11:53:03 +02:00
|
|
|
import string
|
2003-04-22 10:27:11 +02:00
|
|
|
|
|
|
|
class RingBuffer(object):
|
2003-10-04 15:20:41 +02:00
|
|
|
"""Class to represent a fixed-size ring buffer."""
|
2003-04-22 13:02:39 +02:00
|
|
|
__slots__ = ('L', 'i', 'full', 'maxSize')
|
2003-04-22 10:27:11 +02:00
|
|
|
def __init__(self, maxSize, seq=()):
|
|
|
|
if maxSize <= 0:
|
|
|
|
raise ValueError, 'maxSize must be > 0.'
|
2003-04-22 13:00:28 +02:00
|
|
|
self.maxSize = maxSize
|
2003-08-26 18:38:28 +02:00
|
|
|
self.reset()
|
|
|
|
for elt in seq:
|
|
|
|
self.append(elt)
|
|
|
|
|
|
|
|
def reset(self):
|
2003-04-22 13:00:28 +02:00
|
|
|
self.full = False
|
2003-04-22 10:27:11 +02:00
|
|
|
self.L = []
|
2003-04-22 13:00:28 +02:00
|
|
|
self.i = 0
|
2003-04-22 10:27:11 +02:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.L)
|
2003-04-22 13:00:28 +02:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if self.__class__ == other.__class__ and \
|
|
|
|
self.maxSize == other.maxSize and len(self) == len(other):
|
|
|
|
iterator = iter(other)
|
|
|
|
for elt in self:
|
|
|
|
otherelt = iterator.next()
|
|
|
|
if not elt == otherelt:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return False
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-22 10:27:11 +02:00
|
|
|
def __nonzero__(self):
|
|
|
|
return len(self) > 0
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-22 10:27:11 +02:00
|
|
|
def __contains__(self, elt):
|
|
|
|
return elt in self.L
|
|
|
|
|
|
|
|
def append(self, elt):
|
2003-04-22 13:00:28 +02:00
|
|
|
if self.full:
|
|
|
|
self.L[self.i] = elt
|
|
|
|
self.i += 1
|
|
|
|
self.i %= len(self.L)
|
2003-04-22 10:27:11 +02:00
|
|
|
else:
|
2003-04-22 13:00:28 +02:00
|
|
|
if len(self) >= self.maxSize:
|
|
|
|
self.full = True
|
|
|
|
self.append(elt)
|
|
|
|
else:
|
|
|
|
self.L.append(elt)
|
2003-04-22 10:27:11 +02:00
|
|
|
|
|
|
|
def extend(self, seq):
|
|
|
|
for elt in seq:
|
|
|
|
self.append(elt)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-22 10:27:11 +02:00
|
|
|
def __getitem__(self, idx):
|
2003-04-22 13:00:28 +02:00
|
|
|
if self.full:
|
|
|
|
oidx = idx
|
|
|
|
if type(oidx) == types.SliceType:
|
|
|
|
L = []
|
2003-08-19 12:41:19 +02:00
|
|
|
for i in xrange(*slice.indices(oidx, len(self))):
|
2003-04-22 13:00:28 +02:00
|
|
|
L.append(self[i])
|
|
|
|
return L
|
|
|
|
else:
|
|
|
|
(m, idx) = divmod(oidx, len(self.L))
|
|
|
|
if m and m != -1:
|
|
|
|
raise IndexError, oidx
|
|
|
|
idx = (idx + self.i) % len(self.L)
|
|
|
|
return self.L[idx]
|
2003-04-22 10:27:11 +02:00
|
|
|
else:
|
2003-04-22 13:00:28 +02:00
|
|
|
if type(idx) == types.SliceType:
|
|
|
|
L = []
|
2003-08-19 12:41:19 +02:00
|
|
|
for i in xrange(*slice.indices(idx, len(self))):
|
2003-04-22 13:00:28 +02:00
|
|
|
L.append(self[i])
|
|
|
|
return L
|
|
|
|
else:
|
|
|
|
return self.L[idx]
|
2003-04-22 10:27:11 +02:00
|
|
|
|
|
|
|
def __setitem__(self, idx, elt):
|
2003-04-22 13:00:28 +02:00
|
|
|
if self.full:
|
|
|
|
oidx = idx
|
|
|
|
if type(oidx) == types.SliceType:
|
2003-08-19 12:41:19 +02:00
|
|
|
range = xrange(*slice.indices(oidx, len(self)))
|
2003-04-22 13:00:28 +02:00
|
|
|
if len(range) != len(elt):
|
|
|
|
raise ValueError, 'seq must be the same length as slice.'
|
|
|
|
else:
|
|
|
|
for (i, x) in zip(range, elt):
|
|
|
|
self[i] = x
|
|
|
|
else:
|
|
|
|
(m, idx) = divmod(oidx, len(self.L))
|
|
|
|
if m and m != -1:
|
|
|
|
raise IndexError, oidx
|
|
|
|
idx = (idx + self.i) % len(self.L)
|
|
|
|
self.L[idx] = elt
|
|
|
|
else:
|
|
|
|
if type(idx) == types.SliceType:
|
2003-08-19 12:41:19 +02:00
|
|
|
range = xrange(*slice.indices(idx, len(self)))
|
2003-04-22 13:00:28 +02:00
|
|
|
if len(range) != len(elt):
|
|
|
|
raise ValueError, 'seq must be the same length as slice.'
|
|
|
|
else:
|
|
|
|
for (i, x) in zip(range, elt):
|
|
|
|
self[i] = x
|
|
|
|
else:
|
|
|
|
self.L[idx] = elt
|
2003-04-22 10:27:11 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2003-04-22 13:00:28 +02:00
|
|
|
if self.full:
|
|
|
|
return 'RingBuffer(%r, %r)' % (self.maxSize, list(self))
|
|
|
|
else:
|
|
|
|
return 'RingBuffer(%r, %r)' % (self.maxSize, list(self))
|
|
|
|
|
2003-04-22 13:02:39 +02:00
|
|
|
def __getstate__(self):
|
|
|
|
return (self.maxSize, self.full, self.i, self.L)
|
|
|
|
|
|
|
|
def __setstate__(self, (maxSize, full, i, L)):
|
|
|
|
self.maxSize = maxSize
|
|
|
|
self.full = full
|
|
|
|
self.i = i
|
|
|
|
self.L = L
|
|
|
|
|
2003-04-22 13:00:28 +02:00
|
|
|
|
|
|
|
class queue(object):
|
2003-10-04 15:20:41 +02:00
|
|
|
"""Queue class for handling large queues. Queues smaller than 1,000 or so
|
|
|
|
elements are probably better served by the smallqueue class.
|
|
|
|
"""
|
2003-04-22 13:00:28 +02:00
|
|
|
__slots__ = ('front', 'back')
|
|
|
|
def __init__(self, seq=()):
|
|
|
|
self.back = []
|
|
|
|
self.front = []
|
|
|
|
for elt in seq:
|
|
|
|
self.enqueue(elt)
|
|
|
|
|
2003-08-26 18:38:28 +02:00
|
|
|
def reset(self):
|
|
|
|
self.back[:] = []
|
|
|
|
self.front[:] = []
|
|
|
|
|
2003-04-22 13:00:28 +02:00
|
|
|
def enqueue(self, elt):
|
|
|
|
self.back.append(elt)
|
|
|
|
|
|
|
|
def dequeue(self):
|
|
|
|
try:
|
|
|
|
return self.front.pop()
|
|
|
|
except IndexError:
|
|
|
|
self.back.reverse()
|
|
|
|
self.front = self.back
|
|
|
|
self.back = []
|
|
|
|
return self.front.pop()
|
|
|
|
|
|
|
|
def peek(self):
|
|
|
|
if self.front:
|
|
|
|
return self.front[-1]
|
|
|
|
else:
|
|
|
|
return self.back[0]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.front) + len(self.back)
|
|
|
|
|
2003-07-31 08:20:58 +02:00
|
|
|
def __nonzero__(self):
|
|
|
|
return bool(self.back or self.front)
|
|
|
|
|
2003-04-22 13:00:28 +02:00
|
|
|
def __contains__(self, elt):
|
|
|
|
return elt in self.front or elt in self.back
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for elt in reviter(self.front):
|
|
|
|
yield elt
|
|
|
|
for elt in self.back:
|
|
|
|
yield elt
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if len(self) == len(other):
|
|
|
|
otheriter = iter(other)
|
|
|
|
for elt in self:
|
|
|
|
otherelt = otheriter.next()
|
|
|
|
if not (elt == otherelt):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return 'queue([%s])' % ', '.join(map(repr, self))
|
2003-04-22 10:27:11 +02:00
|
|
|
|
|
|
|
def __getitem__(self, oidx):
|
2003-06-03 05:24:06 +02:00
|
|
|
if len(self) == 0:
|
|
|
|
raise IndexError, 'queue index out of range'
|
2003-04-29 15:00:43 +02:00
|
|
|
if type(oidx) == types.SliceType:
|
|
|
|
L = []
|
2003-08-19 12:41:19 +02:00
|
|
|
for i in xrange(*slice.indices(oidx, len(self))):
|
2003-04-29 15:00:43 +02:00
|
|
|
L.append(self[i])
|
|
|
|
return L
|
2003-04-22 13:00:28 +02:00
|
|
|
else:
|
2003-04-29 15:00:43 +02:00
|
|
|
(m, idx) = divmod(oidx, len(self))
|
|
|
|
if m and m != -1:
|
|
|
|
raise IndexError, oidx
|
|
|
|
if len(self.front) > idx:
|
|
|
|
return self.front[-(idx+1)]
|
|
|
|
else:
|
|
|
|
return self.back[(idx-len(self.front))]
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-22 13:00:28 +02:00
|
|
|
def __setitem__(self, oidx, value):
|
2003-06-03 05:24:06 +02:00
|
|
|
if len(self) == 0:
|
|
|
|
raise IndexError, 'queue index out of range'
|
2003-04-29 15:00:43 +02:00
|
|
|
if type(oidx) == types.SliceType:
|
2003-08-23 01:10:08 +02:00
|
|
|
range = xrange(*slice.indices(oidx, len(self)))
|
2003-04-29 15:00:43 +02:00
|
|
|
if len(range) != len(value):
|
|
|
|
raise ValueError, 'seq must be the same length as slice.'
|
|
|
|
else:
|
2003-06-02 08:47:53 +02:00
|
|
|
for i in range:
|
|
|
|
(m, idx) = divmod(oidx, len(self))
|
|
|
|
if m and m != -1:
|
|
|
|
raise IndexError, oidx
|
2003-04-29 15:00:43 +02:00
|
|
|
for (i, x) in zip(range, value):
|
|
|
|
self[i] = x
|
2003-04-22 13:00:28 +02:00
|
|
|
else:
|
2003-04-29 15:00:43 +02:00
|
|
|
(m, idx) = divmod(oidx, len(self))
|
|
|
|
if m and m != -1:
|
|
|
|
raise IndexError, oidx
|
|
|
|
if len(self.front) > idx:
|
|
|
|
self.front[-(idx+1)] = value
|
|
|
|
else:
|
2003-05-29 19:06:04 +02:00
|
|
|
self.back[idx-len(self.front)] = value
|
|
|
|
|
|
|
|
def __delitem__(self, oidx):
|
|
|
|
if type(oidx) == types.SliceType:
|
2003-08-23 01:10:08 +02:00
|
|
|
range = xrange(*slice.indices(oidx, len(self)))
|
2003-05-29 19:06:04 +02:00
|
|
|
for i in range:
|
|
|
|
del self[i]
|
|
|
|
else:
|
|
|
|
(m, idx) = divmod(oidx, len(self))
|
|
|
|
if m and m != -1:
|
|
|
|
raise IndexError, oidx
|
|
|
|
if len(self.front) > idx:
|
|
|
|
del self.front[-(idx+1)]
|
|
|
|
else:
|
|
|
|
del self.back[idx-len(self.front)]
|
2003-04-22 10:27:11 +02:00
|
|
|
|
2003-04-22 13:00:28 +02:00
|
|
|
def __getstate__(self):
|
|
|
|
return (list(self),)
|
|
|
|
|
|
|
|
def __setstate__(self, (L,)):
|
|
|
|
L.reverse()
|
|
|
|
self.front = L
|
|
|
|
self.back = []
|
|
|
|
|
2003-10-04 15:20:41 +02:00
|
|
|
class smallqueue(list):
|
|
|
|
__slots__ = ()
|
|
|
|
def enqueue(self, elt):
|
|
|
|
self.append(elt)
|
|
|
|
|
|
|
|
def dequeue(self):
|
|
|
|
return self.pop(0)
|
|
|
|
|
|
|
|
def peek(self):
|
|
|
|
return self[0]
|
|
|
|
|
2003-10-09 05:59:36 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return 'smallqueue([%s])' % ', '.join(map(repr, self))
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self[:] = []
|
|
|
|
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-22 13:16:03 +02:00
|
|
|
class MaxLengthQueue(queue):
|
|
|
|
__slots__ = ('length',)
|
|
|
|
def __init__(self, length, seq=()):
|
|
|
|
self.length = length
|
|
|
|
queue.__init__(self, seq)
|
2003-04-22 13:15:15 +02:00
|
|
|
|
2003-04-22 13:16:03 +02:00
|
|
|
def __getstate__(self):
|
|
|
|
return (self.length, queue.__getstate__(self))
|
|
|
|
|
|
|
|
def __setstate__(self, (length, q)):
|
|
|
|
self.length = length
|
|
|
|
queue.__setstate__(self, q)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-22 13:16:03 +02:00
|
|
|
def enqueue(self, elt):
|
|
|
|
queue.enqueue(self, elt)
|
|
|
|
if len(self) > self.length:
|
|
|
|
self.dequeue()
|
|
|
|
|
2003-10-16 21:36:33 +02:00
|
|
|
|
|
|
|
class TwoWayDictionary(dict):
|
|
|
|
__slots__ = ()
|
|
|
|
def __init__(self, seq=(), **kwargs):
|
2003-10-16 21:40:14 +02:00
|
|
|
if hasattr(seq, 'iteritems'):
|
|
|
|
seq = seq.iteritems()
|
|
|
|
elif hasattr(seq, 'items'):
|
|
|
|
seq = seq.items()
|
2003-10-16 21:36:33 +02:00
|
|
|
for (key, value) in seq:
|
|
|
|
self[key] = value
|
|
|
|
self[value] = key
|
|
|
|
for (key, value) in kwargs.iteritems():
|
|
|
|
self[key] = value
|
|
|
|
self[value] = key
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
dict.__setitem__(self, key, value)
|
|
|
|
dict.__setitem__(self, value, key)
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
value = self[key]
|
|
|
|
dict.__delitem__(self, key)
|
|
|
|
dict.__delitem__(self, value)
|
2003-10-24 11:53:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PersistentDictionary(dict):
|
|
|
|
_trans = string.maketrans('\r\n', ' ')
|
|
|
|
_locals = locals
|
|
|
|
_globals = globals
|
|
|
|
def __init__(self, filename, globals=None, locals=None):
|
|
|
|
mylocals = locals
|
|
|
|
myglobals = globals
|
|
|
|
if mylocals is None:
|
|
|
|
mylocals = self._locals()
|
|
|
|
if myglobals is None:
|
|
|
|
myglobals = self._globals()
|
|
|
|
self.filename = filename
|
|
|
|
try:
|
|
|
|
fd = file(filename, 'r')
|
|
|
|
s = fd.read()
|
|
|
|
fd.close()
|
|
|
|
s.translate(self._trans)
|
|
|
|
super(self.__class__, self).__init__(eval(s, myglobals, mylocals))
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
fd = file(self.filename, 'w')
|
|
|
|
fd.write(repr(self))
|
|
|
|
fd.close()
|