Skip to content

Instantly share code, notes, and snippets.

@ezyang
ezyang / index.html
Created December 7, 2012 21:21
Text rotation in D3.js
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script src="http://d3js.org/d3.v2.js"></script>
</head>
<body>
<script type="text/javascript">
/**
* A convenient function for taking a transformation (with rotation) on
@ezyang
ezyang / index.html
Created December 11, 2012 19:44
Tweening lines with kinks
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script src="http://d3js.org/d3.v3.js"></script>
<style>
path {stroke-width: 4px; fill:none; stroke:#000}
circle {fill:#000}
</style>
</head>
@ezyang
ezyang / gist:7886849
Last active December 30, 2015 21:19
pub unsafe fn alloc_mega_group(mblocks: nat) -> ~Block {
let n = MBLOCK_GROUP_BLOCKS(mblocks);
let mut prev_link = &mut free_mblock_list.p;
let mut best: Option<(&mut BlockData, &mut BlockMeta)> = None;
loop {
match prev_link {
&None => break,
// NB: need to match it out, so we don't take out a
// reference on prev_link that will interfere with the
// take()
pub fn new(n: nat) -> MBlock {
let ptr = unsafe { aligned_alloc_raw(MBLOCK_SIZE as size_t, (MBLOCK_SIZE * n) as size_t) };
assert!(!ptr::is_null(ptr));
let mb = MBlock{ptr: ptr};
// XXX should this really be here?
unsafe {
let mut block = mb.first_block_ptr();
let mut bd = mb.first_bdescr_ptr();
while (block <= mb.last_block_ptr()) {
(*bd).data.start = cast_ptr(block);
pub fn alloc_mega_group(mblocks: nat) -> ~Block {
let n = MBLOCK_GROUP_BLOCKS(mblocks);
enum SearchResult<'a> {
PerfectMatch(~Block),
BestMatch(&'a mut BlockData, &'a mut BlockMeta),
NoMatch
}
// tail-recursive style makes the borrow-checker *a lot*
// happier, since some of the aliasing effects that occur when
// local mutable variables are reused go away (fresh lexical
#[allow(dead_code)];
use std::util;
enum List<T> {
Cons(T, ~List<T>),
Nil
}
fn pop<T>(prev: &mut ~List<T>) -> Option<~List<T>> {
pub fn freplace<T>(mut src: T, dest: &mut T) -> T {
util::swap(dest, &mut src);
src
}
fn pop<T>(prev: &mut ~List<T>) -> Option<~List<T>> {
Some(freplace(util::replace(match prev {
&~Cons(_, ref mut rs) => rs,
&~Nil => return None
fn pop<T>(prev: &mut ~List<T>) -> Option<~List<T>> {
let xs = util::replace(match prev {
&~Cons(_, ref mut rs) => rs,
&~Nil => return None
}, ~Nil);
Some(util::replace(prev, xs))
}
Less => {
let inserted = find_or_insert(&mut save.left, key, value);
skew(save);
split(save);
inserted
}
enum Bar {
Z(int),
S(~Bar)
}
fn foo<'a>(x: &'a mut Bar) -> &'a int {
match x {
&Z(_) => {
fail!()
},