2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-04-14 10:30:31 -05:00
|
|
|
#![feature(managed_boxes)]
|
2013-10-23 03:49:18 -05:00
|
|
|
|
2013-12-31 17:46:27 -06:00
|
|
|
use std::cell::RefCell;
|
2014-06-11 21:33:52 -05:00
|
|
|
use std::gc::{Gc, GC};
|
2013-12-31 17:46:27 -06:00
|
|
|
|
2013-04-22 23:19:58 -05:00
|
|
|
pub struct Entry<A,B> {
|
|
|
|
key: A,
|
|
|
|
value: B
|
|
|
|
}
|
2013-01-26 00:46:32 -06:00
|
|
|
|
2013-04-22 23:19:58 -05:00
|
|
|
pub struct alist<A,B> {
|
2013-09-17 01:37:54 -05:00
|
|
|
eq_fn: extern "Rust" fn(A,A) -> bool,
|
2014-06-11 21:33:52 -05:00
|
|
|
data: Gc<RefCell<Vec<Entry<A,B>>>>,
|
2013-04-22 23:19:58 -05:00
|
|
|
}
|
2012-03-07 17:13:31 -06:00
|
|
|
|
2013-07-18 19:12:46 -05:00
|
|
|
pub fn alist_add<A:'static,B:'static>(lst: &alist<A,B>, k: A, v: B) {
|
2013-12-31 17:46:27 -06:00
|
|
|
let mut data = lst.data.borrow_mut();
|
2014-03-21 00:06:51 -05:00
|
|
|
(*data).push(Entry{key:k, value:v});
|
2012-03-07 17:13:31 -06:00
|
|
|
}
|
|
|
|
|
2013-07-18 19:12:46 -05:00
|
|
|
pub fn alist_get<A:Clone + 'static,
|
|
|
|
B:Clone + 'static>(
|
|
|
|
lst: &alist<A,B>,
|
|
|
|
k: A)
|
|
|
|
-> B {
|
2012-03-07 17:13:31 -06:00
|
|
|
let eq_fn = lst.eq_fn;
|
2013-12-31 17:46:27 -06:00
|
|
|
let data = lst.data.borrow();
|
2014-03-21 00:06:51 -05:00
|
|
|
for entry in (*data).iter() {
|
2013-07-02 14:47:32 -05:00
|
|
|
if eq_fn(entry.key.clone(), k.clone()) {
|
|
|
|
return entry.value.clone();
|
|
|
|
}
|
2012-03-07 17:13:31 -06:00
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
fail!();
|
2012-03-07 17:13:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-07-18 19:12:46 -05:00
|
|
|
pub fn new_int_alist<B:'static>() -> alist<int, B> {
|
2013-04-22 23:19:58 -05:00
|
|
|
fn eq_int(a: int, b: int) -> bool { a == b }
|
2013-12-31 17:46:27 -06:00
|
|
|
return alist {
|
|
|
|
eq_fn: eq_int,
|
2014-06-11 21:33:52 -05:00
|
|
|
data: box(GC) RefCell::new(Vec::new()),
|
2013-12-31 17:46:27 -06:00
|
|
|
};
|
2012-03-07 17:13:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-07-18 19:12:46 -05:00
|
|
|
pub fn new_int_alist_2<B:'static>() -> alist<int, B> {
|
2012-03-07 17:13:31 -06:00
|
|
|
#[inline]
|
2013-04-22 23:19:58 -05:00
|
|
|
fn eq_int(a: int, b: int) -> bool { a == b }
|
2013-12-31 17:46:27 -06:00
|
|
|
return alist {
|
|
|
|
eq_fn: eq_int,
|
2014-06-11 21:33:52 -05:00
|
|
|
data: box(GC) RefCell::new(Vec::new()),
|
2013-12-31 17:46:27 -06:00
|
|
|
};
|
2012-09-18 17:52:21 -05:00
|
|
|
}
|