diff options
| author | Marijn Haverbeke <[email protected]> | 2011-05-12 17:24:54 +0200 |
|---|---|---|
| committer | Marijn Haverbeke <[email protected]> | 2011-05-12 21:30:44 +0200 |
| commit | 3816e57fd2a8ab19e4ac6d4b3ddd5b49d5973ff2 (patch) | |
| tree | 508982ed2f789aedd89eebd529343d9dc88b8e01 /src/lib/ufind.rs | |
| parent | Transitional change to make extfmt output lowercase module name (diff) | |
| download | rust-3816e57fd2a8ab19e4ac6d4b3ddd5b49d5973ff2.tar.xz rust-3816e57fd2a8ab19e4ac6d4b3ddd5b49d5973ff2.zip | |
Downcase std modules again, move to :: for module dereferencing
This should be a snapshot transition.
Diffstat (limited to 'src/lib/ufind.rs')
| -rw-r--r-- | src/lib/ufind.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/lib/ufind.rs b/src/lib/ufind.rs new file mode 100644 index 00000000..faa77305 --- /dev/null +++ b/src/lib/ufind.rs @@ -0,0 +1,37 @@ +import option::none; +import option::some; + +// A very naive implementation of union-find with unsigned integer nodes. + +type node = option::t[uint]; +type ufind = rec(mutable vec[mutable node] nodes); + +fn make() -> ufind { + let vec[mutable node] v = vec(mutable none[uint]); + _vec::pop(v); // FIXME: botch + ret rec(mutable nodes=v); +} + +fn make_set(&ufind ufnd) -> uint { + auto idx = _vec::len(ufnd.nodes); + ufnd.nodes += vec(mutable none[uint]); + ret idx; +} + +fn find(&ufind ufnd, uint n) -> uint { + alt (ufnd.nodes.(n)) { + case (none[uint]) { ret n; } + case (some[uint](?m)) { + // TODO: "be" + ret find(ufnd, m); + } + } +} + +fn union(&ufind ufnd, uint m, uint n) { + auto m_root = find(ufnd, m); + auto n_root = find(ufnd, n); + auto ptr = some[uint](n_root); + ufnd.nodes.(m_root) = ptr; +} + |