|
@@ -32,29 +32,72 @@ class SmartList(list): |
|
|
super(SmartList, self).__init__() |
|
|
super(SmartList, self).__init__() |
|
|
self._children = {} |
|
|
self._children = {} |
|
|
|
|
|
|
|
|
def __getslice__(self, start, stop): |
|
|
|
|
|
sublist = super(SmartList, self).__getslice__(start, stop) |
|
|
|
|
|
sliceinfo = [start, stop, 1] |
|
|
|
|
|
|
|
|
def __getitem__(self, key): |
|
|
|
|
|
if not isinstance(key, slice): |
|
|
|
|
|
return super(SmartList, self).__getitem__(key) |
|
|
|
|
|
sliceinfo = [key.start, key.stop, 1 if not key.step else key.step] |
|
|
child = _ListProxy(self, sliceinfo) |
|
|
child = _ListProxy(self, sliceinfo) |
|
|
self._children[id(child)] = (child, sliceinfo) |
|
|
self._children[id(child)] = (child, sliceinfo) |
|
|
return child |
|
|
return child |
|
|
|
|
|
|
|
|
|
|
|
def __setitem__(self, key, item): |
|
|
|
|
|
if not isinstance(key, slice): |
|
|
|
|
|
return super(SmartList, self).__setitem__(key, item) |
|
|
|
|
|
item = list(item) |
|
|
|
|
|
super(SmartList, self).__setitem__(key, item) |
|
|
|
|
|
diff = len(item) - key.stop + key.start |
|
|
|
|
|
if diff: |
|
|
|
|
|
for child, (start, stop, step) in self._children.itervalues(): |
|
|
|
|
|
if start >= key.stop: |
|
|
|
|
|
self._children[id(child)][1][0] += diff |
|
|
|
|
|
if stop >= key.stop and stop != sys.maxint: |
|
|
|
|
|
self._children[id(child)][1][1] += diff |
|
|
|
|
|
|
|
|
|
|
|
__delitem__ |
|
|
|
|
|
|
|
|
|
|
|
def __getslice__(self, start, stop): |
|
|
|
|
|
return self.__getitem__(slice(start, stop)) |
|
|
|
|
|
|
|
|
def __setslice__(self, start, stop, iterable): |
|
|
def __setslice__(self, start, stop, iterable): |
|
|
obj = list(iterable) |
|
|
|
|
|
super(SmartList, self).__setslice__(start, stop, obj) |
|
|
|
|
|
diff = len(obj) - stop + start |
|
|
|
|
|
for child, (ch_start, ch_stop, step) in self._children.itervalues(): |
|
|
|
|
|
if ch_start >= stop: |
|
|
|
|
|
self._children[id(child)][1][0] += diff |
|
|
|
|
|
if ch_stop >= stop and ch_stop != sys.maxint: |
|
|
|
|
|
self._children[id(child)][1][1] += diff |
|
|
|
|
|
|
|
|
self.__setitem__(slice(start, stop), iterable) |
|
|
|
|
|
|
|
|
def append(self, obj): |
|
|
|
|
|
super(SmartList, self).append(obj) |
|
|
|
|
|
|
|
|
def __delslice__(self, start, stop): |
|
|
|
|
|
self.__delitem__(slice(start, stop)) |
|
|
|
|
|
|
|
|
|
|
|
__add__ |
|
|
|
|
|
|
|
|
|
|
|
__radd__ |
|
|
|
|
|
|
|
|
|
|
|
__iadd__ |
|
|
|
|
|
|
|
|
|
|
|
__mul__ |
|
|
|
|
|
|
|
|
|
|
|
__rmul__ |
|
|
|
|
|
|
|
|
|
|
|
__imul__ |
|
|
|
|
|
|
|
|
|
|
|
def append(self, item): |
|
|
|
|
|
super(SmartList, self).append(item) |
|
|
for child, (start, stop, step) in self._children.itervalues(): |
|
|
for child, (start, stop, step) in self._children.itervalues(): |
|
|
if stop >= len(self) - 1 and stop != sys.maxint: |
|
|
if stop >= len(self) - 1 and stop != sys.maxint: |
|
|
self._children[id(child)][1][1] += 1 |
|
|
self._children[id(child)][1][1] += 1 |
|
|
|
|
|
|
|
|
|
|
|
count |
|
|
|
|
|
|
|
|
|
|
|
index |
|
|
|
|
|
|
|
|
|
|
|
extend |
|
|
|
|
|
|
|
|
|
|
|
insert |
|
|
|
|
|
|
|
|
|
|
|
pop |
|
|
|
|
|
|
|
|
|
|
|
remove |
|
|
|
|
|
|
|
|
|
|
|
reverse |
|
|
|
|
|
|
|
|
|
|
|
sort |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _ListProxy(list): |
|
|
class _ListProxy(list): |
|
|
def __init__(self, parent, sliceinfo): |
|
|
def __init__(self, parent, sliceinfo): |
|
@@ -65,26 +108,71 @@ class _ListProxy(list): |
|
|
def __repr__(self): |
|
|
def __repr__(self): |
|
|
return repr(self._render()) |
|
|
return repr(self._render()) |
|
|
|
|
|
|
|
|
|
|
|
__lt__ |
|
|
|
|
|
|
|
|
|
|
|
__le__ |
|
|
|
|
|
|
|
|
|
|
|
__eq__ |
|
|
|
|
|
|
|
|
|
|
|
__ne__ |
|
|
|
|
|
|
|
|
|
|
|
__gt__ |
|
|
|
|
|
|
|
|
|
|
|
__ge__ |
|
|
|
|
|
|
|
|
|
|
|
__nonzero__ |
|
|
|
|
|
|
|
|
def __len__(self): |
|
|
def __len__(self): |
|
|
return (self._stop - self._start) / self._step |
|
|
return (self._stop - self._start) / self._step |
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, key): |
|
|
|
|
|
return self._render()[key] |
|
|
|
|
|
|
|
|
|
|
|
def __setitem__(self, key, item): |
|
|
|
|
|
if isinstance(key, slice): |
|
|
|
|
|
adjusted = slice(key.start + self._start, key.stop + self._stop, |
|
|
|
|
|
key.step) |
|
|
|
|
|
self._parent[adjusted] = item |
|
|
|
|
|
else: |
|
|
|
|
|
self._parent[self._start + index] = item |
|
|
|
|
|
|
|
|
|
|
|
__delitem__ |
|
|
|
|
|
|
|
|
def __iter__(self): |
|
|
def __iter__(self): |
|
|
i = self._start |
|
|
i = self._start |
|
|
while i < self._stop: |
|
|
while i < self._stop: |
|
|
yield self._parent[i] |
|
|
yield self._parent[i] |
|
|
i += self._step |
|
|
i += self._step |
|
|
|
|
|
|
|
|
def __getitem__(self, index): |
|
|
|
|
|
return self._render()[index] |
|
|
|
|
|
|
|
|
def __reversed__(self): |
|
|
|
|
|
i = self._stop - 1 |
|
|
|
|
|
while i >= self._start: |
|
|
|
|
|
yield self._parent[i] |
|
|
|
|
|
i -= self._step |
|
|
|
|
|
|
|
|
def __getslice__(self, start, stop): |
|
|
|
|
|
return self._render()[start:stop] |
|
|
|
|
|
|
|
|
def __contains__(self, item): |
|
|
|
|
|
return item in self._render() |
|
|
|
|
|
|
|
|
def __setitem__(self, index, obj): |
|
|
|
|
|
self._parent[self._start + index] = obj |
|
|
|
|
|
|
|
|
def __getslice__(self, start, stop): |
|
|
|
|
|
return self.__getitem__(slice(start, stop)) |
|
|
|
|
|
|
|
|
def __setslice__(self, start, stop, iterable): |
|
|
def __setslice__(self, start, stop, iterable): |
|
|
self._parent[self._start + start:self._start + stop] = iterable |
|
|
|
|
|
|
|
|
self.__setitem__(slice(start, stop), iterable) |
|
|
|
|
|
|
|
|
|
|
|
def __delslice__(self, start, stop): |
|
|
|
|
|
self.__delitem__(slice(start, stop)) |
|
|
|
|
|
|
|
|
|
|
|
__add__ |
|
|
|
|
|
|
|
|
|
|
|
__radd__ |
|
|
|
|
|
|
|
|
|
|
|
__iadd__ |
|
|
|
|
|
|
|
|
|
|
|
__mul__ |
|
|
|
|
|
|
|
|
|
|
|
__rmul__ |
|
|
|
|
|
|
|
|
|
|
|
__imul__ |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def _start(self): |
|
|
def _start(self): |
|
@@ -104,5 +192,19 @@ class _ListProxy(list): |
|
|
def append(self, obj): |
|
|
def append(self, obj): |
|
|
self._parent.insert(self._stop, obj) |
|
|
self._parent.insert(self._stop, obj) |
|
|
|
|
|
|
|
|
|
|
|
count |
|
|
|
|
|
|
|
|
|
|
|
index |
|
|
|
|
|
|
|
|
|
|
|
extend |
|
|
|
|
|
|
|
|
def insert(self, index, obj): |
|
|
def insert(self, index, obj): |
|
|
self._parent.insert(self._start + index, obj) |
|
|
self._parent.insert(self._start + index, obj) |
|
|
|
|
|
|
|
|
|
|
|
pop |
|
|
|
|
|
|
|
|
|
|
|
remove |
|
|
|
|
|
|
|
|
|
|
|
reverse |
|
|
|
|
|
|
|
|
|
|
|
sort |