Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- concept Comparator[T]{
- Callable[T.compare, {T, T}, Ordering]
- }
- generic[K, C:Comparator[ref K]]
- struct Tree{
- var root: *Node;
- var c: C;
- }
- enum Ordering{
- case GT;
- case LT;
- case EQ;
- }
- implementation Tree{
- struct Node{
- var left: *Node;
- var right: *Node;
- var key: K;
- }
- generic[S]
- method find(self: ref Self, key: S): bool
- where Callable[C.compare, {ref K, S}, Ordering]
- {
- var node = self.root;
- while (node != null) {
- switch(self.c.compare(&node.key, key)){
- case GT: node = node.right;
- case LT: node = node.left;
- case EQ: return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement