[6b0b624] | 1 | #pragma once |
---|
[6e3ae00] | 2 | |
---|
| 3 | extern "C" { |
---|
| 4 | #define NULL 0 |
---|
| 5 | #define assert(cond) if (! (cond)) { printf("Assertion failed: (%s) at %s:%d\n", #cond, __FILE__, __LINE__); abort(); } |
---|
| 6 | } |
---|
| 7 | |
---|
| 8 | // #include <types.h> |
---|
| 9 | // #include <lib.h> |
---|
| 10 | |
---|
[fd54fef] | 11 | trait Comparable(T) { |
---|
[6e3ae00] | 12 | int ?<?(T, T); |
---|
| 13 | }; |
---|
| 14 | |
---|
[fd54fef] | 15 | forall(T | Comparable(T)) |
---|
[6e3ae00] | 16 | int ?==?(T t1, T t2); |
---|
| 17 | |
---|
[fd54fef] | 18 | forall(T | Comparable(T)) |
---|
[6e3ae00] | 19 | int ?>?(T t1, T t2); |
---|
| 20 | |
---|
[627f585] | 21 | // xxx - unbound type variable problems when trying to use new instead of create |
---|
| 22 | // forall( otype T, ttype Params | { void ?{}(T *, Params); } ) T * new( Params p ); |
---|
| 23 | |
---|
[6e3ae00] | 24 | // To-do: properly use height or balance factor |
---|
| 25 | // Right now I'm recomputing the height for each |
---|
| 26 | // node multiple times. It's Theta-log(n), but still.. |
---|
| 27 | |
---|
| 28 | // Balanced Binary Search Tree of void pointers; almost an AVL tree - |
---|
| 29 | // just needs to make use of the balance factor properly |
---|
| 30 | // Operations: |
---|
| 31 | // ?{}, ^?{} |
---|
| 32 | // create - allocate a new tree. Just a wrapper around malloc which also calls the tree constructor. |
---|
| 33 | // find - search through the tree for the given key; return the associated value |
---|
| 34 | // empty - return true if the tree is empty |
---|
| 35 | // insert - insert node with key and value pair. Returns 0 on success |
---|
| 36 | // remove - remove node with the given key, returns 0 on success, 1 on failure |
---|
| 37 | // copy - deep copy of a tree |
---|
| 38 | // for_each - applies the given function to every data element in the tree |
---|
| 39 | // assumes that a non-zero return value is an error, will return |
---|
| 40 | // the error code from func |
---|
| 41 | |
---|
| 42 | // temporary: need forward decl to get around typedef problem |
---|
[fd54fef] | 43 | forall(K | Comparable(K), V) |
---|
[6e3ae00] | 44 | struct tree; |
---|
| 45 | |
---|
[fd54fef] | 46 | forall(K | Comparable(K), V) |
---|
[6e3ae00] | 47 | struct tree { |
---|
| 48 | K key; |
---|
| 49 | V value; |
---|
| 50 | tree(K, V) * parent; |
---|
| 51 | tree(K, V) * left; |
---|
| 52 | tree(K, V) * right; |
---|
| 53 | int balance; |
---|
| 54 | }; |
---|
| 55 | |
---|
[fd54fef] | 56 | forall(K | Comparable(K), V) |
---|
[2afec66] | 57 | void ?{}(tree(K, V) &t, K key, V value); |
---|
[6e3ae00] | 58 | |
---|
[fd54fef] | 59 | forall(K | Comparable(K), V) |
---|
[2afec66] | 60 | void ^?{}(tree(K, V) & t); |
---|
[6e3ae00] | 61 | |
---|
[fd54fef] | 62 | forall(K | Comparable(K), V) |
---|
[6e3ae00] | 63 | tree(K, V) * create(K key, V value); |
---|
| 64 | |
---|
[fd54fef] | 65 | forall(K | Comparable(K), V) |
---|
[6e3ae00] | 66 | V * find(tree(K, V) * t, K key); |
---|
| 67 | |
---|
[fd54fef] | 68 | forall(K | Comparable(K), V) |
---|
[6e3ae00] | 69 | int empty(tree(K, V) * t); |
---|
| 70 | |
---|
| 71 | // returns the root of the tree |
---|
[fd54fef] | 72 | forall(K | Comparable(K), V) |
---|
[6e3ae00] | 73 | int insert(tree(K, V) ** t, K key, V value); |
---|
| 74 | |
---|
[fd54fef] | 75 | forall(K | Comparable(K), V) |
---|
[6e3ae00] | 76 | int remove(tree(K, V) ** t, K key); |
---|
| 77 | |
---|
[fd54fef] | 78 | forall(K | Comparable(K), V) |
---|
[6e3ae00] | 79 | void copy(tree(K, V) * src, tree(K, V) ** ret); |
---|
| 80 | |
---|
[fd54fef] | 81 | forall(K | Comparable(K), V) |
---|
[6e3ae00] | 82 | void for_each(tree(K, V) * t, void (*func)(V)); |
---|
| 83 | |
---|
| 84 | // // Helper function to print trees |
---|
| 85 | // forall(otype K | Comparable(K), otype V) |
---|
| 86 | // void printTree(tree * t, int level){ |
---|
| 87 | // if (empty(t)){ |
---|
| 88 | // return; |
---|
| 89 | // } |
---|
| 90 | |
---|
| 91 | // printTree(t->left, level+1); |
---|
| 92 | // printf("key: %d, value: %s, level: %d\n", t->key, t->value, level); |
---|
| 93 | // printTree(t->right, level+1); |
---|
| 94 | // } |
---|
| 95 | |
---|
| 96 | // // inorder traversal of t |
---|
| 97 | // // prints each key, followed by the value |
---|
| 98 | // forall(otype K | Comparable(K), otype V) |
---|
| 99 | // void printTree(tree(K, V) * t){ |
---|
| 100 | // printTree(t, 0); |
---|
| 101 | // } |
---|