micropython / micropython-lib

Core Python libraries ported to MicroPython
Other
2.3k stars 980 forks source link

Deque is not a deque #847

Closed Kacarott closed 2 months ago

Kacarott commented 2 months ago

A deque, as described in the docs, is:

Deques (double-ended queues) are a list-like container that support O(1) appends and pops from either side of the deque.

And yet the implementation seems to very clearly be just a list, calling itself a deque? And just like a list it has O(N) appends and pops from the left:

class deque:
    def __init__(self, iterable=None):
        if iterable is None:
            self.q = []
        else:
            self.q = list(iterable)

    def popleft(self):
        return self.q.pop(0)

    ...

    def appendleft(self, a):
        self.q.insert(0, a)
andrewleech commented 2 months ago

This older library was made for simple / basic API compatibility with a minimal subset of the cpython library api.

The faster C built-in version was recently expanded in master for the next release of micropython which should render this library obsolete. https://github.com/micropython/micropython/pull/10724

mattytrentini commented 2 months ago

I've raised #848 to remove that now-unnecessary deque implementation.