+ def merge(self, old):
+ """Try to merge two buckets into one.
+
+ @type old: L{KBucket}
+ @param old: the bucket to merge into this one
+ @return: whether a merge was done or not
+ """
+ # Decide if we should do a merge
+ if len(self.nodes) + old.len() > K:
+ return False
+
+ # Set the range to cover the other's as well
+ self.min = min(self.min, old.min)
+ self.max = max(self.max, old.max)
+
+ # Transfer the other's nodes to this bucket, merging the sorting
+ i = 0
+ for node in old.list():
+ while i < len(self.nodes) and self.nodes[i].lastSeen <= node.lastSeen:
+ i += 1
+ self.nodes.insert(i, node)
+ i += 1
+
+ return True
+