Last active
December 18, 2015 02:19
-
-
Save rubber-duck/5710188 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use std::uint; use std::vec; | |
static node_size : uint = 32; | |
struct HashArrayMappedTrie<T> { | |
root : Node<T>, | |
length : uint | |
} | |
#[deriving(Clone)] | |
enum Node<T> { | |
Values(~[T]), | |
Children(@[Node<T>]), | |
} | |
fn from_vec<T>(source : &[T]) -> Node<T> { | |
// if source is larger than node need to chunk it to child nodes | |
if source.len() > node_size { | |
// calculate closest power of node_size to source.len() | |
let chunk_size = node_size.to_float().pow(source.len().to_float().log(node_size.to_float()).trunc()) as uint; | |
// split source in chunk_size and recursively call from_vec on each chunk | |
let chunks = source.len() / chunk_size; | |
let mut result : ~[Node<T>] = ~[]; | |
for uint::range(0, chunks) |chunk| { | |
vec::push(&result, from_vec(vec::const_slice(source, chunk * chunk_size, (chunk + 1) * chunk_size))); | |
} | |
Children(source) | |
} else { | |
// values array doesn't need to be of node_size length, it can be smaller | |
Values(source) | |
} | |
} | |
fn main() { | |
let tree : Node<int> = from_vec([1, .. 128]); | |
} |
This file contains hidden or 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
main.rs:10:11: 10:16 error: mismatched types: expected `@[Node<<V103>>]` but found `&@[Node<'a>]` (expected vector but found &-ptr) | |
main.rs:10 #[deriving(Clone)] | |
^~~~~ | |
main.rs:27:17: 27:23 error: mismatched types: expected `@[Node<<V32>>]` but found `&['a]` (expected enum Node but found type parameter) | |
main.rs:27 Children(source) | |
^~~~~~ | |
main.rs:30:15: 30:21 error: mismatched types: expected `~[<V33>]` but found `&['a]` ([] storage differs: expected ~ but found &) | |
main.rs:30 Values(source) | |
^~~~~~ | |
error: aborting due to 3 previous errors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment