Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reorganize code, add tests for tree, add 'make test'
- Loading branch information
1 parent
668f28d
commit da19925
Showing
7 changed files
with
217 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
enum handle<T> = @T; | ||
enum handle<T> { | ||
_handle(@T) | ||
} | ||
|
||
impl methods<T> for handle<T> { | ||
fn get() -> @T { *self } | ||
|
||
fn with(f: fn(T)) { | ||
f(**self) | ||
} | ||
} | ||
|
||
fn handle<T:copy>(t: T) -> handle<T> { | ||
_handle(@t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// FIXME--mut should be inherited | ||
type point<A> = { mut x: A, mut y: A }; | ||
type size<A> = { mut width: A, mut height: A }; | ||
type rect<A> = { mut origin: point<A>, mut size: size<A> }; | ||
|
||
enum au = int; | ||
|
||
fn point<A:copy>(x: A, y: A) -> point<A> { | ||
{mut x: x, mut y: y} | ||
} | ||
|
||
fn size<A:copy>(w: A, h: A) -> size<A> { | ||
{mut width: w, mut height: h} | ||
} | ||
|
||
fn box<A:copy>(x: A, y: A, w: A, h: A) -> rect<A> { | ||
{mut origin: point(x, y), | ||
mut size: size(w, h)} | ||
} | ||
|
||
fn zero_rect_au() -> rect<au> { | ||
let z = au(0); | ||
{mut origin: point(z, z), mut size: size(z, z)} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
type fields<T> = @{ | ||
mut parent: option<T>, | ||
mut first_child: option<T>, | ||
mut last_child: option<T>, | ||
mut prev_sibling: option<T>, | ||
mut next_sibling: option<T> | ||
}; | ||
|
||
iface tree { | ||
fn tree_fields() -> fields<self>; | ||
} | ||
|
||
fn each_child<T:copy tree>( | ||
node: T, f: fn(T) -> bool) { | ||
|
||
let mut p = node.tree_fields().first_child; | ||
loop { | ||
alt p { | ||
none { ret; } | ||
some(c) { | ||
if !f(c) { ret; } | ||
p = c.tree_fields().next_sibling; | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn empty<T>() -> fields<T> { | ||
@{mut parent: none, | ||
mut first_child: none, | ||
mut last_child: none, | ||
mut prev_sibling: none, | ||
mut next_sibling: none} | ||
} | ||
|
||
fn add_child<T:copy tree>( | ||
node: T, child: T) { | ||
|
||
let child_tf = child.tree_fields(); | ||
alt child_tf.parent { | ||
some(_) { fail "Already has a parent"; } | ||
none { child_tf.parent = some(node); } | ||
} | ||
|
||
assert child_tf.prev_sibling == none; | ||
assert child_tf.next_sibling == none; | ||
|
||
let node_tf = node.tree_fields(); | ||
alt node_tf.last_child { | ||
none { | ||
node_tf.first_child = some(child); | ||
} | ||
|
||
some(lc) { | ||
let lc_tf = lc.tree_fields(); | ||
assert lc_tf.next_sibling == none; | ||
lc_tf.next_sibling = some(child); | ||
child_tf.prev_sibling = some(lc); | ||
} | ||
} | ||
|
||
node_tf.last_child = some(child); | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
enum dummy = @{ | ||
fields: fields<dummy>, | ||
value: uint | ||
}; | ||
|
||
impl of tree for dummy { | ||
fn tree_fields() -> fields<dummy> { self.fields } | ||
} | ||
|
||
fn new_dummy(v: uint) -> dummy { | ||
dummy(@{fields: empty(), value: v}) | ||
} | ||
|
||
#[test] | ||
fn add_child_0() { | ||
let children = [new_dummy(0u), | ||
new_dummy(1u), | ||
new_dummy(2u)]; | ||
let p = new_dummy(3u); | ||
|
||
for vec::each(children) {|c| | ||
add_child(p, c); | ||
} | ||
|
||
let mut i = 0u; | ||
for each_child(p) {|c| | ||
assert c.value == i; | ||
i += 1u; | ||
} | ||
assert i == children.len(); | ||
} | ||
} |