class Postponed(CircularList):

    def __init__(self):
        CircularList.__init__(self)
        self.it = iter(self)
        self.preempt = QList()
        self.cm_to_list = {}

    def __len__(self):
        l = 0
        for k, v in self.cm_to_list.iteritems():
            l += len(v)
        l += len(self.preempt)
        return l

    def append_preempt(self, c):
        return self.preempt.append(c)
    
    def add_connection(self, keyable, c):
        if keyable not in self.cm_to_list:
            self.cm_to_list[keyable] = QList()
            self.prepend(keyable)
        self.cm_to_list[keyable].append(c)

    def pop_connection(self):
        if self.preempt:
            return self.preempt.popleft()
        keyable = self.it.next()
        l = self.cm_to_list[keyable]
        c = l.popleft()
        if len(l) == 0:
            self.remove(keyable)
            del self.cm_to_list[keyable]
        return c

    def remove_connection(self, keyable, c):
        # hmmm
        if c.urgent:
            self.preempt.remove(c)
            return
        l = self.cm_to_list[keyable]
        l.remove(c)
        if len(l) == 0:
            self.remove(keyable)
            del self.cm_to_list[keyable]
class Postponed(CircularList):

    def __init__(self):
        CircularList.__init__(self)
        self.it = iter(self)
        self.preempt = QList()
        self.cm_to_list = {}

    def __len__(self):
        l = 0
        for k, v in self.cm_to_list.iteritems():
            l += len(v)
        l += len(self.preempt)
        return l

    def append_preempt(self, c):
        return self.preempt.append(c)
    
    def add_connection(self, keyable, c):
        if keyable not in self.cm_to_list:
            self.cm_to_list[keyable] = QList()
            self.prepend(keyable)
        self.cm_to_list[keyable].append(c)

    def pop_connection(self):
        if self.preempt:
            return self.preempt.popleft()
        keyable = self.it.next()
        l = self.cm_to_list[keyable]
        c = l.popleft()
        if len(l) == 0:
            self.remove(keyable)
            del self.cm_to_list[keyable]
        return c

    def remove_connection(self, keyable, c):
        # hmmm
        if c.urgent:
            self.preempt.remove(c)
            return
        l = self.cm_to_list[keyable]
        l.remove(c)
        if len(l) == 0:
            self.remove(keyable)
            del self.cm_to_list[keyable]
 def add_connection(self, keyable, c):
     if keyable not in self.cm_to_list:
         self.cm_to_list[keyable] = QList()
         self.prepend(keyable)
     self.cm_to_list[keyable].append(c)
 def __init__(self):
     CircularList.__init__(self)
     self.it = iter(self)
     self.preempt = QList()
     self.cm_to_list = {}
 def __init__(self):
     CircularList.__init__(self)
     self.it = iter(self)
     self.preempt = QList()
     self.cm_to_list = {}