"""Doubly linked list over a pool of nodes stored as three parallel lists.

Node i is (val[i], nxt[i], prv[i]); a handle is the node's pool index.
Index 0 is a sentinel: nxt[0] is the first node, prv[0] the last, so the
"no node" value 0 falls out naturally at both ends. Removed nodes are put on
a free chain threaded through nxt and reused before the pool grows.

The operations are closures stored on the instance (no bound-method objects,
state in fast closure cells); next/prev/get are the pool lists' own C-level
__getitem__ and front/back are C-level partials, so they run without a Python
frame.
"""

from functools import partial


class DList:
    def __init__(self) -> None:
        val = [0]   # values; slot 0 is the sentinel
        nxt = [0]   # next links; nxt[0] = front
        prv = [0]   # prev links; prv[0] = back
        fh = 0      # head of the free chain (through nxt), 0 = empty
        n = 0       # number of elements

        def push_back(v):
            nonlocal fh, n
            b = prv[0]
            x = fh
            if x:
                fh = nxt[x]
                val[x] = v
                nxt[x] = 0
                prv[x] = b
            else:
                x = len(val)
                val.append(v)
                nxt.append(0)
                prv.append(b)
            nxt[b] = x
            prv[0] = x
            n += 1
            return x

        def push_front(v):
            nonlocal fh, n
            f = nxt[0]
            x = fh
            if x:
                fh = nxt[x]
                val[x] = v
                nxt[x] = f
                prv[x] = 0
            else:
                x = len(val)
                val.append(v)
                nxt.append(f)
                prv.append(0)
            prv[f] = x
            nxt[0] = x
            n += 1
            return x

        def insert_after(node, v):
            nonlocal fh, n
            q = nxt[node]
            x = fh
            if x:
                fh = nxt[x]
                val[x] = v
                nxt[x] = q
                prv[x] = node
            else:
                x = len(val)
                val.append(v)
                nxt.append(q)
                prv.append(node)
            nxt[node] = x
            prv[q] = x
            n += 1
            return x

        def insert_before(node, v):
            nonlocal fh, n
            p = prv[node]
            x = fh
            if x:
                fh = nxt[x]
                val[x] = v
                nxt[x] = node
                prv[x] = p
            else:
                x = len(val)
                val.append(v)
                nxt.append(node)
                prv.append(p)
            nxt[p] = x
            prv[node] = x
            n += 1
            return x

        def remove(x):
            nonlocal fh, n
            p = prv[x]
            q = nxt[x]
            nxt[p] = q
            prv[q] = p
            nxt[x] = fh
            fh = x
            n -= 1
            return val[x]

        def pop_front():
            nonlocal fh, n
            x = nxt[0]
            if not x:
                return None
            q = nxt[x]
            nxt[0] = q
            prv[q] = 0
            nxt[x] = fh
            fh = x
            n -= 1
            return val[x]

        def pop_back():
            nonlocal fh, n
            x = prv[0]
            if not x:
                return None
            p = prv[x]
            prv[0] = p
            nxt[p] = 0
            nxt[x] = fh
            fh = x
            n -= 1
            return val[x]

        def move_to_front(x):
            f = nxt[0]
            if f == x:
                return
            p = prv[x]
            q = nxt[x]
            nxt[p] = q
            prv[q] = p
            nxt[x] = f
            prv[x] = 0
            prv[f] = x
            nxt[0] = x

        def length():
            return n

        self.push_back = push_back
        self.push_front = push_front
        self.insert_after = insert_after
        self.insert_before = insert_before
        self.remove = remove
        self.pop_front = pop_front
        self.pop_back = pop_back
        self.move_to_front = move_to_front
        self.next = nxt.__getitem__
        self.prev = prv.__getitem__
        self.get = val.__getitem__
        self.front = partial(nxt.__getitem__, 0)
        self.back = partial(prv.__getitem__, 0)
        self._len = length

    def __len__(self) -> int:
        return self._len()
