# -*- coding: utf-8  -*-
#
# Copyright (C) 2012 Ben Kurtovic <ben.kurtovic@verizon.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import sys

__all__ = ["SmartList"]

class SmartList(list):
    def __init__(self, iterable=None):
        if iterable:
            super(SmartList, self).__init__(iterable)
        else:
            super(SmartList, self).__init__()
        self._children = {}

    def __getslice__(self, start, stop):
        sublist = super(SmartList, self).__getslice__(start, stop)
        sliceinfo = [start, stop, 1]
        child = _ListProxy(self, sliceinfo)
        self._children[id(child)] = (child, sliceinfo)
        return child

    # def __setslice__(self, start, stop):

    def append(self, obj):
        super(SmartList, self).append(obj)
        for child, (start, stop, step) in self._children.itervalues():
            if stop >= len(self) - 1 and stop != sys.maxint:
                self._children[id(child)][1][1] += 1


class _ListProxy(list):
    def __init__(self, parent, sliceinfo):
        super(_ListProxy, self).__init__()
        self._parent = parent
        self._sliceinfo = sliceinfo

    def __len__(self):
        return (self._stop - self._start) / self._step

    def __iter__(self):
        i = self._start
        while i < self._stop:
            yield self._parent[i]
            i += self._step

    def __getitem__(self, index):
        return self._render()[index]

    def __getslice__(self, start, stop):
        return self._render()[start:stop]

    @property
    def _start(self):
        return self._sliceinfo[0]

    @property
    def _stop(self):
        return self._sliceinfo[1]

    @property
    def _step(self):
        return self._sliceinfo[2]

    def _render(self):
        return self._parent[self._start:self._stop:self._step]

    def append(self, obj):
        self._parent.insert(self._stop, obj)

    def insert(self, index, obj):
        self._parent.insert(self._start + index, obj)

@@ -1675,10 +1675,8 @@
 %0A   
- #
  def
@@ -1706,18 +1706,438 @@
 rt, stop
-):
+, iterable):%0A        obj = list(iterable)%0A        super(SmartList, self).__setslice__(start, stop, obj)%0A        diff = len(obj) - stop + start%0A        for child, (ch_start, ch_stop, step) in self._children.itervalues():%0A            if ch_start %3E= stop:%0A                self._children%5Bid(child)%5D%5B1%5D%5B0%5D += diff%0A            if ch_stop %3E= stop and ch_stop != sys.maxint:%0A                self._children%5Bid(child)%5D%5B1%5D%5B1%5D += diff
 %0A%0A    de
@@ -2564,16 +2564,77 @@
 ceinfo%0A%0A
+    def __repr__(self):%0A        return repr(self._render())%0A%0A
     def 
@@ -2950,32 +2950,32 @@
 , start, stop):%0A
-
         return s
@@ -2997,24 +2997,236 @@
 tart:stop%5D%0A%0A
+    def __setitem__(self, index, obj):%0A        self._parent%5Bself._start + index%5D = obj%0A%0A    def __setslice__(self, start, stop, iterable):%0A        self._parent%5Bself._start + start:self._start + stop%5D = iterable%0A%0A
     @propert

Implement SmartList.setslice, ListProxy.{repr,setitem,setslice}