Advertisement
tinyevil

Untitled

Jan 8th, 2019
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.62 KB | None | 0 0
  1. concept Comparator[T]{
  2.     Callable[T.compare, {T, T}, Ordering]
  3. }
  4.  
  5. generic[K, C:Comparator[ref K]]
  6. struct Tree{
  7.     var root: *Node;
  8.     var c: C;
  9. }
  10.  
  11. enum Ordering{
  12.     case GT;
  13.     case LT;
  14.     case EQ;
  15. }
  16.  
  17. implementation Tree{
  18.     struct Node{
  19.         var left: *Node;
  20.         var right: *Node;
  21.         var key: K;
  22.     }
  23.  
  24.     generic[S]
  25.     method find(self: ref Self, key: S): bool
  26.     where Callable[C.compare, {ref K, S}, Ordering]
  27.     {
  28.         var node = self.root;
  29.         while (node != null) {
  30.             switch(self.c.compare(&node.key, key)){
  31.             case GT: node = node.right;
  32.             case LT: node = node.left;
  33.             case EQ: return true;
  34.             }
  35.         }
  36.         return false;
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement