<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from weakref import ref


class SLLNode:
    """ Node model """
    __slots__ = ['next', 'value', 'weakref']

    def __init__(self, value=None, next=None, weakref=None):
        self.next = next
        self.value = value
        self.weakref = weakref

    def __call__(self):
        return self.value

    def __str__(self):
        return str(self.value)

    def __repr__(self):
        return str(self.value)


class SLList:
    """ SLList model """
    __slots__ = ['first', 'last', 'size']

    def __init__(self, iterable=None):
        self.first = None
        self.last = None
        self.size = 0
        if iterable:
            self.__extend(iterable)

    def __extend(self, iterable):
        for item in iterable:
            self.appendright(item)

    def __iter__(self):
        curr = self.first
        while curr:
            yield curr
            curr = curr.next

    def __str__(self):
        return str(list(self))

    def __getitem__(self, index):
        i = 0
        if index < 0 and index > self.size:
            raise IndexError("Not such index")
        curr = self.first
        while(curr != None and i < index):
            curr = curr.next
            i += 1
        return curr

    def __setitem__(self, index, value):
        node = self.__getitem__(index)
        if isinstance(value, SLLNode):
            value = value.value
        node.value = value

    def __get_prev(self, node):
        if not isinstance(node, SLLNode):
            raise TypeError("Object must be Node instance")
        if not self.first:
            raise TypeError("List is empty")
        if self.first == node:
            return None
        curr = self.first
        prev = None
        while(curr and curr != node):
            prev = curr
            curr = curr.next
        del curr
        return prev

    def appendleft(self, value):
        if isinstance(value, SLLNode):
            value = value.value

        head = self.first
        new_node = SLLNode(value=value, next=head, weakref=ref(self))
        self.first = new_node
        return new_node

    def append(self, value):
        return self.appendright(value)

    def appendright(self, value):
        if isinstance(value, SLLNode):
            value = value.value

        new_node = SLLNode(value=value, next=None, weakref=ref(self))
        if not self.first:
            self.first = new_node
        else:
            self.last.next = new_node
        self.last = new_node
        self.size += 1
        return new_node

    def popleft(self):
        if not self.first:
            raise RuntimeError("List is empty")
        del_node = self.first
        self.first = del_node.next
        if self.last == del_node:
            self.last = None
        self.size -= 1
        rval = del_node.value
        del del_node
        return rval

    def pop(self):
        return self.popright()

    def popright(self):
        if not self.first:
            raise RuntimeError("List is empty")
        del_node = self.last
        if self.first == del_node:
            self.last = None
            self.first = None
        else:
            prev = self.__get_prev(del_node)
            prev.next = None
            self.last = prev
        self.size -= 1
        rval = del_node.value
        del del_node
        return rval

    def remove(self, node):
        if not node.weakref == ref(self):
            raise RuntimeError("Node is not element of this list")
        prev = self.__get_prev(node)
        if not prev:
            self.popleft()
        else:
            prev.next = node.next
        rval = node.value
        del node
        return rval

if __name__ == '__main__':
    l = SLList(range(100, 115))
    print "Initialized list", l
    for i in range(0, 10):
        l.appendleft(i)
    print "After append left", l
    for i in range(0, 10):
        l.appendright(i)
    print "After append right", l
    for i in range(0, 10):
        l.popleft()
    print "After pop left", l
    for i in range(0, 10):
        l.popright()
    print "After pop right", l<commit_msg>fixed bugs, c&p cmp method<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-


class sllistnode(object):
    __slots__ = ('__next', '__value', '__owner')

    def __init__(self, value=None, next=None, owner=None):
        self.__next = next
        self.__value = value
        self.__owner = owner

    @property
    def next(self):
        return self.__next

    @property
    def value(self):
        return self.__value

    @property
    def owner(self):
        return self.__owner

    def __call__(self):
        return self.__value

    def __str__(self):
        return "sllistnode(%s)" % str(self.__value)

    def __repr__(self):
        return "<sllistnode(%s)>" % repr(self.__value)


class sllistiterator(object):
    __slots__ = ('__current_node', )

    def __init__(self, list):
        if not isinstance(list, sllist):
            raise TypeError('sllist argument expected')

        self.__current_node = list.first

    def next(self):
        if self.__current_node is None:
            raise StopIteration

        value = self.__current_node.value
        self.__current_node = self.__current_node.next

        return value

    def __iter__(self):
        return self


class sllist(object):
    __slots__ = ('__first', '__last', '__size', )

    def __init__(self, iterable=None):
        self.__first = None
        self.__last = None
        self.__size = 0
        if iterable:
            self.__extend(iterable)

    @property
    def first(self):
        return self.__first

    @property
    def last(self):
        return self.__last

    @property
    def size(self):
        return self.__size

    def __extend(self, iterable):
        for item in iterable:
            self.appendright(item)

    def __delitem__(self, index):
        to_del = self.__getitem__(index)
        self.remove(to_del)

    def __getitem__(self, index):
        if abs(index) >= self.__size:
            raise IndexError("list index out of range (%s) %s" % (index, self))
        i = 0
        if index < 0:
            index = self.__size + index
        if not self.__first:
            raise IndexError("list index out of range (%s) %s" % (index, self))
        curr = self.__first
        while(curr != None and i < index):
            curr = curr.next
            i += 1
        return curr

    def __len__(self):
        return self.__size

    def __setitem__(self, index, value):
        node = self.__getitem__(index)
        if isinstance(value, sllistnode):
            value = value.value
        node.value = value

    def __cmp__(self, other):
        result = len(self) - len(other)
        if result < 0:
            return -1
        elif result > 0:
            return 1

        for sval, oval in zip(self, other):
            result = cmp(sval, oval)
            if result != 0:
                return result

        return 0

    def __str__(self):
        if self.__first is not None:
            return 'sllist([' + ', '.join((str(x) for x in self)) + '])'
        else:
            return 'sllist()'

    def __repr__(self):
        if self.__first is not None:
            return 'sllist([' + ', '.join((repr(x) for x in self)) + '])'
        else:
            return 'sllist()'

    def __iter__(self):
        return sllistiterator(self)

    def __get_prev(self, node):
        if not isinstance(node, sllistnode):
            raise TypeError("Object must be Node instance")
        if not self.__first:
            raise IndexError("List is empty")
        if self.__first == node:
            return None
        curr = self.__first
        prev = None
        while(curr and curr != node):
            prev = curr
            curr = curr.next
        return prev

    def appendleft(self, value):
        if isinstance(value, sllistnode):
            value = value.value
        new_node = sllistnode(value=value, next=self.__first, owner=self)
        self.__first = new_node
        self.__size += 1
        return new_node

    def insert(self, value, before=None):
        if before is None:
            return self.appendright(value)
        else:
            return self.insertbefore(before, value)

    def insertafter(self, node, value):
        if not isinstance(node, sllistnode):
            raise TypeError("node must be instance of sllistnode")
        if isinstance(value, sllistnode):
            value = value.value
        new_node = sllistnode(value=value, next=None, owner=self)
        new_node._sllistnode__next = node.next
        node._sllistnode__next = new_node
        self.__size += 1
        return new_node

    def insertbefore(self, node, value):
        if not isinstance(node, sllistnode):
            raise TypeError("node must be instance of sllistnode")
        if isinstance(value, sllistnode):
            value = value.value
        new_node = sllistnode(value=value, next=None, owner=self)
        prev = self.__get_prev(node)
        if prev:
            prev._sllistnode__next = new_node
            new_node._sllistnode__next = node
        else:
            new_node._sllistnode__next = node
            self.__first = new_node
        self.__size += 1
        return new_node

    def append(self, value):
        return self.appendright(value)

    def appendright(self, value):
        if isinstance(value, sllistnode):
            value = value.value

        new_node = sllistnode(value=value, next=None, owner=self)
        if not self.__first:
            self.__first = new_node
        else:
            self.__last._sllistnode__next = new_node
        self.__last = new_node
        self.__size += 1
        return new_node

    def popleft(self):
        if not self.__first:
            raise RuntimeError("List is empty")
        del_node = self.__first
        self.__first = del_node.next
        if self.__last == del_node:
            self.__last = None
        self.__size -= 1
        return del_node.value

    def pop(self):
        return self.popright()

    def popright(self):
        if not self.__first:
            raise RuntimeError("List is empty")
        del_node = self.__last
        if self.__first == del_node:
            self.__last = None
            self.__first = None
        else:
            prev = self.__get_prev(del_node)
            prev._sllistnode__next = None
            self.__last = prev
        self.__size -= 1
        return del_node.value

    def remove(self, node):
        if not isinstance(node, sllistnode):
            raise TypeError("node must be a sllistnode")
        if self.__first is None:
            raise RuntimeError("List is empty")
        if node.owner != self:
            raise RuntimeError("Node is not element of this list")
        prev = self.__get_prev(node)
        if not prev:
            self.popleft()
        else:
            prev._sllistnode__next = node.next
            self.__size -= 1
        return node.value
{'fraction_non_alphanumeric': 0.09600794797687862, 'fraction_numerical': 0.003973988439306358, 'mean_word_length': 1.779367469879518, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5349974', 'n_tokens_mistral': 3278, 'n_tokens_neox': 3186, 'n_words': 906}