Skip to content

Instantly share code, notes, and snippets.

View smvv's full-sized avatar

Sander Mathijs van Veen smvv

View GitHub Profile
" =============================================================================
" Vim config file -- Sander Mathijs van Veen <smvv@kompiler.org> -- since 2011
" =============================================================================
set nocompatible " Be iMproved!
" Terminal configuration
" -----------------------------------------------------------------------------
set background=dark
@smvv
smvv / btree.rs
Last active December 19, 2015 17:28
/*
The errors below are fixed by adding additional curly braces to the macro body.
This is a bug in the macro system.
btree.rs:508:20: 508:24 error: unresolved name `list`. Did you mean `t`?
btree.rs:508 assert!(list.len() >= values.len());
^~~~
<core-macros>:52:4: 69:5 note: in expansion of assert!
btree.rs:508:12: 508:48 note: expansion site
btree.rs:505:4: 525:5 note: in expansion of check_values!
@smvv
smvv / linked_list.rs
Last active December 19, 2015 17:09 — forked from jasom/gist:5988857
use std::io;
use std::util;
enum List {
Cons(uint,~List),
Nil()
}
fn cons(x : uint, l : ~List) -> ~List{
return ~Cons(x,l);
@smvv
smvv / btree.rs
Last active December 19, 2015 16:08
/*
rustc -O -L. --test -o btree btree.rs
btree.rs:68:24: 79:7 error: mismatched types: expected `~[~str]` but found `std::iterator::MapIterator<,&std::option::Option<TreeItem<T,U>>,~(),std::vec::VecIterator<,std::option::Option<TreeItem<T,U>>>>` (expected vector but found struct std::iterator::MapIterator)
btree.rs:68 let buf : ~[~str] = tree.nodes.iter().transform(|x| {
btree.rs:69 ~"\t".repeat(indent).push_str(match *x {
btree.rs:70 Some(TreeNode { key: k, value: tree }) => {
btree.rs:71 let buf = ~"Node(key=...)\n";
btree.rs:72 buf //buf.push_str(tree.to_str())
btree.rs:73 }
...
use std::io;
struct Tree<K, V> {
used: uint,
nodes: [Option<TreeItem<K, V>>, ..3],
}
enum TreeItem<K, V> {
TreeNode { key: K, value: ~Tree<K, V> },
TreeLeaf { key: K, value: V },
fn insert_node<K: Eq + Ord, V>(tree: &mut BTree<K, V>, key: K,
value: V) -> bool {
debug!("insert key-value pair: %? -> %?", key, value);
if tree.is_empty() {
insert_leaf(tree, 0, key, value);
return true;
}
let capacity = tree.nodes.len();
@smvv
smvv / btree.rs
Last active December 19, 2015 13:19
//! A B-tree is a tree data structure that keeps data sorted and allows
//! searches, sequential access, insertions, deletions in logarithmic time.
//! B-trees are different from binary search trees because a b-tree node can
//! have more than two children (also known as the *degree* of a b-tree).
//!
//! Basic example:
//!
//! ~~~ rust
//! let mut s = BTree::new();
//!
@smvv
smvv / gist:5943633
Last active December 19, 2015 10:49
use std::vec;
struct Message;
impl Message {
pub fn with_ptr<T>(&self, f: &fn(*u8, uint) -> T) -> T {
unsafe {
let data = vec::raw::to_ptr([1 as u8]);
let len = 1 as uint;
@smvv
smvv / gist:5934520
Last active December 19, 2015 09:39
// The number of keys is chosen to vary between d and 2d, where d is the
// minimum number of keys and d+1 is the minimum degree (branching factor) of
// the tree. In this case, d = 2 which results in degree = 5.
static BTreeDegree : uint = 5;
pub struct BTree<K, V> {
priv pairs: [NodeContent<K, V>, ..BTreeDegree],
}
enum NodeContent<K, V> {
@smvv
smvv / gist:5571416
Last active December 17, 2015 07:18
struct Env {
out: ~str,
}
impl Env {
fn write(&self, msg: ~str) {
self.out += msg;
}
}