Ejemplo n.º 1
0
 def __getitem__(self, key: K) -> Optional[V]:
     dummy = Entry(key, None)
     leaf = self.root.find_leaf(key)
     if dummy in leaf.entries:
         i = leaf.entries.index(dummy)
         return leaf.entries[i].value
     return None
Ejemplo n.º 2
0
    def delete(self, key: K) -> ChildStatus:
        """
        Remove the entry given by key
        If we're deficient, signal to the parent by returning
        REBALANCE
        """
        dummy = Entry(key, None)
        i = bisect_left(self.entries, dummy)

        if self.entries[i] == dummy:
            del self.entries[i]
            if len(self.entries) < self.tree.MIN_KEYS:
                return ChildStatus.REBALANCE
        return ChildStatus.DONE
Ejemplo n.º 3
0
    def insert(self, key: K, value: V) -> ChildStatus:
        """
        Insert the key and value into this leaf.
        Notify the parent of the result.
        We either:
            1. Are done
            2. Split
        """

        e = Entry(key, value)
        i = bisect_left(self.entries, e)

        if e in self.entries:
            # replace the old entry
            self.entries[i] = e
        else:
            # insert the entry
            self.entries.insert(i, e)

        if len(self.entries) > self.tree.MAX_KEYS:
            return ChildStatus.REBALANCE

        # inserted without a problem
        return ChildStatus.DONE
Ejemplo n.º 4
0
 def __contains__(self, key: K) -> bool:
     dummy = Entry(key, None)
     leaf = self.root.find_leaf(key)
     return dummy in leaf.entries