Skip to content

Commit

Permalink
Make the BTree size aware, and add BTree.size() function
Browse files Browse the repository at this point in the history
  • Loading branch information
ByronBecker committed Feb 4, 2023
1 parent cc6367a commit dc07d30
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
33 changes: 26 additions & 7 deletions src/BTree.mo
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ module {
var count = 0;
};
});
var size = 0;
order = btreeOrder;
}
};


/// Allows one to quickly create a BTree using an array of key value pairs
public func createBTreeWithKVPairs<K, V>(order: Nat, compare: (K, K) -> O.Order, kvPairs: [(K, V)]): BTree<K, V> {
Expand All @@ -49,6 +49,9 @@ module {
t;
};

/// Get the current count of key-value pairs present in the BTree
public func size<K, V>(tree: BTree<K, V>): Nat { tree.size };


/// Retrieves the value corresponding to the key of BTree if it exists
public func get<K, V>(tree: BTree<K, V>, compare: (K, K) -> O.Order, key: K): ?V {
Expand All @@ -67,7 +70,14 @@ module {
};

switch(insertResult) {
case (#insert(ov)) { ov };
case (#insert(ov)) {
switch(ov) {
// if inserted a value that was not previously there, increment the tree size counter
case null { tree.size += 1 };
case _ {};
};
ov
};
case (#promote({ kv; leftChild; rightChild; })) {
tree.root := #internal({
data = {
Expand All @@ -83,6 +93,8 @@ module {
else { null }
});
});
// promotion always comes from inserting a new element, so increment the tree size counter
tree.size += 1;

null
}
Expand All @@ -99,14 +111,14 @@ module {
case (#keyFound(deleteIndex)) {
leafNode.data.count -= 1;
let (_, deletedValue) = AU.deleteAndShiftValuesOver<(K, V)>(leafNode.data.kvs, deleteIndex);
tree.size -= 1;
?deletedValue
};
case _ { null }
}

};
case (#internal(internalNode)) {
switch(internalDeleteHelper(internalNode, tree.order, compare, key, false)) {
let deletedValueResult = switch(internalDeleteHelper(internalNode, tree.order, compare, key, false)) {
case (#delete(value)) { value };
case (#mergeChild({ internalChild; deletedValue })) {
if (internalChild.data.count > 0) {
Expand All @@ -122,9 +134,15 @@ module {
};
deletedValue
}
}
}
};

switch(deletedValueResult) {
// if deleted a value from the BTree, decrement the size
case (?deletedValue) { tree.size -= 1 };
case null {}
};
deletedValueResult
}
}
};

Expand Down Expand Up @@ -1176,6 +1194,7 @@ module {
public func toText<K, V>(t: BTree<K, V>, keyToText: K -> Text, valueToText: V -> Text): Text {
var textOutput = "BTree={";
textOutput #= "root=" # rootToText<K, V>(t.root, keyToText, valueToText) # "; ";
textOutput #= "size=" # Nat.toText(t.size) # "; ";
textOutput #= "order=" # Nat.toText(t.order) # "; ";
textOutput # "}";
};
Expand All @@ -1188,7 +1207,7 @@ module {
keyEquals: (K, K) -> Bool,
valueEquals: (V, V) -> Bool
): Bool {
if (t1.order != t2.order) return false;
if (t1.order != t2.order or t1.size != t2.size) return false;

nodeEquals(t1.root, t2.root, keyEquals, valueEquals);
};
Expand Down
1 change: 1 addition & 0 deletions src/Types.mo
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module {

public type BTree<K, V> = {
var root: Node<K, V>;
var size: Nat;
order: Nat;
};
}
3 changes: 3 additions & 0 deletions test/BTreeDeleteOrder4PropertyTest.mo
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ let btreePropertyTests = S.suite("check validity of mass insertion/deletion", [
var count = 0;
}
});
var size = 0;
order = 4;
}))
),
Expand Down Expand Up @@ -87,6 +88,7 @@ let btreePropertyTests = S.suite("check validity of mass insertion/deletion", [
var count = 0;
}
});
var size = 0;
order = 4;
}))
),
Expand Down Expand Up @@ -121,6 +123,7 @@ let btreePropertyTests = S.suite("check validity of mass insertion/deletion", [
var count = 0;
}
});
var size = 0;
order = 4;
}))
),
Expand Down
3 changes: 3 additions & 0 deletions test/BTreeDeleteOrder6PropertyTest.mo
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ let btreePropertyTests = S.suite("check validity of mass insertion/deletion", [
var count = 0;
}
});
var size = 0;
order = 6;
}))
),
Expand Down Expand Up @@ -87,6 +88,7 @@ let btreePropertyTests = S.suite("check validity of mass insertion/deletion", [
var count = 0;
}
});
var size = 0;
order = 6;
}))
),
Expand Down Expand Up @@ -121,6 +123,7 @@ let btreePropertyTests = S.suite("check validity of mass insertion/deletion", [
var count = 0;
}
});
var size = 0;
order = 6;
}))
),
Expand Down
Loading

0 comments on commit dc07d30

Please sign in to comment.