MCPcopy Create free account
hub / github.com/KentBeck/BPlusTree3 / insert

Method insert

python/tests/test_optimized_bplus_tree.py:40–66  ·  view source on GitHub ↗

Insert with optimized array access.

(self, key, value)

Source from the content-addressed store, hash-verified

38 return self
39
40 def insert(self, key, value) -> Optional[Tuple[Any, "OptimizedLeafNode"]]:
41 """Insert with optimized array access."""
42 pos = self.find_position(key)
43
44 # Update existing key
45 if pos < self.num_keys and self.data[pos] == key:
46 self.data[self.capacity + pos] = value
47 return None
48
49 # Check if split needed
50 if self.num_keys >= self.capacity:
51 return self._split_and_insert(pos, key, value)
52
53 # Shift in single operation
54 if pos < self.num_keys:
55 # Move keys
56 self.data[pos + 1 : self.num_keys + 1] = self.data[pos : self.num_keys]
57 # Move values
58 start_val = self.capacity + pos
59 end_val = self.capacity + self.num_keys
60 self.data[start_val + 1 : end_val + 1] = self.data[start_val:end_val]
61
62 # Insert
63 self.data[pos] = key
64 self.data[self.capacity + pos] = value
65 self.num_keys += 1
66 return None
67
68 def _split_and_insert(
69 self, pos: int, key, value

Calls 2

find_positionMethod · 0.95
_split_and_insertMethod · 0.95

Tested by

no test coverage detected