def __init__(self, seq=None): if isinstance(seq, btuple): self._blist = seq._blist elif seq is not None: self._blist = blist(seq) else: self._blist = blist() self._hash = -1
def __mul__(self, k): if not isinstance(k, int): raise TypeError("can't multiply sequence by non-int of type '%s'" % str(type(int))) rv = self.__class__() rv._key = self._key rv._blist = sum((blist([x])*k for x in self._blist), blist()) return rv
def __init__(self, iterable=(), key=None): self._key = key if key is not None and not hasattr(key, '__call__'): raise TypeError("'%s' object is not callable" % str(type(key))) if ((isinstance(iterable,type(self)) or isinstance(self,type(iterable))) and iterable._key is key): self._blist = blist(iterable._blist) else: self._blist = blist() for v in iterable: self.add(v)
def _btuple_or_tuple(self, other, f): if isinstance(other, btuple): rv = f(self._blist, other._blist) elif isinstance(other, tuple): rv = f(self._blist, blist(other)) else: return NotImplemented if isinstance(rv, blist): rv = btuple(rv) return rv
def spinlock(steps, insertions): # 'blist' improves the performance of list insertion lst = blist([0]) value = 1 pos = 0 for _ in xrange(insertions): pos = (pos + steps) % len(lst) pos += 1 lst.insert(pos, value) value += 1 return lst, pos
def values(self): return blist(self[key] for key in self)
def items(self): return blist((key, self[key]) for key in self)