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.
|
|
|
|
|
2012-09-18 17:52:21 -05:00
|
|
|
#[legacy_modes];
|
|
|
|
|
2012-09-05 14:32:05 -05:00
|
|
|
use dvec::DVec;
|
2012-06-04 12:44:19 -05:00
|
|
|
|
2013-01-30 16:30:22 -06:00
|
|
|
pub struct Entry<A,B> {key: A, value: B}
|
2013-01-26 00:46:32 -06:00
|
|
|
|
2013-01-30 16:30:22 -06:00
|
|
|
pub struct alist<A,B> { eq_fn: fn@(A,A) -> bool, data: DVec<Entry<A,B>> }
|
2012-03-07 17:13:31 -06:00
|
|
|
|
2013-01-30 16:30:22 -06:00
|
|
|
pub fn alist_add<A: Copy, B: Copy>(lst: alist<A,B>, k: A, v: B) {
|
2013-01-26 00:46:32 -06:00
|
|
|
lst.data.push(Entry{key:k, value:v});
|
2012-03-07 17:13:31 -06:00
|
|
|
}
|
|
|
|
|
2013-01-30 16:30:22 -06:00
|
|
|
pub fn alist_get<A: Copy, B: Copy>(lst: alist<A,B>, k: A) -> B {
|
2012-03-07 17:13:31 -06:00
|
|
|
let eq_fn = lst.eq_fn;
|
2012-06-30 18:19:07 -05:00
|
|
|
for lst.data.each |entry| {
|
2012-08-01 19:30:05 -05:00
|
|
|
if eq_fn(entry.key, k) { return entry.value; }
|
2012-03-07 17:13:31 -06:00
|
|
|
}
|
2013-01-31 19:51:01 -06:00
|
|
|
die!();
|
2012-03-07 17:13:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-01-30 16:30:22 -06:00
|
|
|
pub fn new_int_alist<B: Copy>() -> alist<int, B> {
|
2012-03-07 17:13:31 -06:00
|
|
|
fn eq_int(&&a: int, &&b: int) -> bool { a == b }
|
2013-01-08 16:00:45 -06:00
|
|
|
return alist {eq_fn: eq_int, data: DVec()};
|
2012-03-07 17:13:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-01-30 16:30:22 -06:00
|
|
|
pub fn new_int_alist_2<B: Copy>() -> alist<int, B> {
|
2012-03-07 17:13:31 -06:00
|
|
|
#[inline]
|
|
|
|
fn eq_int(&&a: int, &&b: int) -> bool { a == b }
|
2013-01-08 16:00:45 -06:00
|
|
|
return alist {eq_fn: eq_int, data: DVec()};
|
2012-09-18 17:52:21 -05:00
|
|
|
}
|