Exemplo n.º 1
0
class display_name_prefix(object):
    def __init__(self):
        self.next_suffix = 0
        self.holes = RBTree()

    def new_suffix(self):
        if len(self.holes) == 0:
            suffix = self.next_suffix
            self.next_suffix += 1
        else:
            first_node = self.holes.firstNode()
            suffix = first_node.key
            self.holes.deleteNode(first_node)
        return suffix

    def putback_suffix(self, suffix):
        if suffix + 1 != self.next_suffix:
            self.holes.insertNode(suffix, suffix)
            return

        self.next_suffix = suffix
        while True:
            prev_suffix = self.next_suffix - 1
            prev_suffix_node = self.holes.findNode(prev_suffix)
            if not prev_suffix_node:
                return
            self.holes.deleteNode(prev_suffix_node)
            self.next_suffix = prev_suffix

    def empty(self):
        return self.next_suffix == 0
Exemplo n.º 2
0
class display_name_prefix(object):
    def __init__(self):
        self.next_suffix = 0
        self.holes = RBTree()

    def new_suffix(self):
        if len(self.holes) == 0:
            suffix = self.next_suffix
            self.next_suffix += 1
        else:
            first_node = self.holes.firstNode()
            suffix = first_node.key
            self.holes.deleteNode(first_node)
        return suffix

    def putback_suffix(self, suffix):
        if suffix + 1 != self.next_suffix:
            self.holes.insertNode(suffix, suffix)
            return

        self.next_suffix = suffix
        while True:
            prev_suffix = self.next_suffix - 1
            prev_suffix_node = self.holes.findNode(prev_suffix)
            if not prev_suffix_node:
                return
            self.holes.deleteNode(prev_suffix_node)
            self.next_suffix = prev_suffix

    def empty(self):
        return self.next_suffix == 0