std: Convert to rustdoc
This commit is contained in:
parent
b22556a6f8
commit
95521c4084
@ -1,9 +1,3 @@
|
||||
/*
|
||||
Module: bitv
|
||||
|
||||
Bitvectors.
|
||||
*/
|
||||
|
||||
export t;
|
||||
export create;
|
||||
export union;
|
||||
@ -28,25 +22,19 @@ export eq_vec;
|
||||
// an optimizing version of this module that produces a different obj
|
||||
// for the case where nbits <= 32.
|
||||
|
||||
/*
|
||||
Type: t
|
||||
|
||||
The bitvector type.
|
||||
*/
|
||||
#[doc = "The bitvector type"]
|
||||
type t = @{storage: [mutable uint], nbits: uint};
|
||||
|
||||
|
||||
const uint_bits: uint = 32u + (1u << 32u >> 27u);
|
||||
|
||||
/*
|
||||
Function: create
|
||||
#[doc = "
|
||||
Constructs a bitvector
|
||||
|
||||
Constructs a bitvector.
|
||||
# Arguments
|
||||
|
||||
Parameters:
|
||||
nbits - The number of bits in the bitvector
|
||||
init - If true then the bits are initialized to 1, otherwise 0
|
||||
*/
|
||||
* nbits - The number of bits in the bitvector
|
||||
* init - If true then the bits are initialized to 1, otherwise 0
|
||||
"]
|
||||
fn create(nbits: uint, init: bool) -> t {
|
||||
let elt = if init { !0u } else { 0u };
|
||||
let storage = vec::to_mut(vec::init_elt(nbits / uint_bits + 1u, elt));
|
||||
@ -74,21 +62,12 @@ fn union(v0: t, v1: t) -> bool { let sub = lor; ret process(v0, v1, sub); }
|
||||
|
||||
fn land(w0: uint, w1: uint) -> uint { ret w0 & w1; }
|
||||
|
||||
/*
|
||||
Function: intersect
|
||||
|
||||
#[doc = "
|
||||
Calculates the intersection of two bitvectors
|
||||
|
||||
Sets `v0` to the intersection of `v0` and `v1`
|
||||
|
||||
Preconditions:
|
||||
|
||||
Both bitvectors must be the same length
|
||||
|
||||
Returns:
|
||||
|
||||
True if `v0` was changed
|
||||
*/
|
||||
Sets `v0` to the intersection of `v0` and `v1`. Both bitvectors must be the
|
||||
same length. Returns 'true' if `v0` was changed.
|
||||
"]
|
||||
fn intersect(v0: t, v1: t) -> bool {
|
||||
let sub = land;
|
||||
ret process(v0, v1, sub);
|
||||
@ -96,26 +75,14 @@ fn intersect(v0: t, v1: t) -> bool {
|
||||
|
||||
fn right(_w0: uint, w1: uint) -> uint { ret w1; }
|
||||
|
||||
/*
|
||||
Function: assign
|
||||
|
||||
#[doc = "
|
||||
Assigns the value of `v1` to `v0`
|
||||
|
||||
Preconditions:
|
||||
|
||||
Both bitvectors must be the same length
|
||||
|
||||
Returns:
|
||||
|
||||
True if `v0` was changed
|
||||
*/
|
||||
Both bitvectors must be the same length. Returns `true` if `v0` was changed
|
||||
"]
|
||||
fn assign(v0: t, v1: t) -> bool { let sub = right; ret process(v0, v1, sub); }
|
||||
|
||||
/*
|
||||
Function: clone
|
||||
|
||||
Makes a copy of a bitvector
|
||||
*/
|
||||
#[doc = "Makes a copy of a bitvector"]
|
||||
fn clone(v: t) -> t {
|
||||
let storage = vec::to_mut(vec::init_elt(v.nbits / uint_bits + 1u, 0u));
|
||||
let len = vec::len(v.storage);
|
||||
@ -123,11 +90,7 @@ fn clone(v: t) -> t {
|
||||
ret @{storage: storage, nbits: v.nbits};
|
||||
}
|
||||
|
||||
/*
|
||||
Function: get
|
||||
|
||||
Retreive the value at index `i`
|
||||
*/
|
||||
#[doc = "Retreive the value at index `i`"]
|
||||
pure fn get(v: t, i: uint) -> bool {
|
||||
assert (i < v.nbits);
|
||||
let bits = uint_bits;
|
||||
@ -139,19 +102,12 @@ pure fn get(v: t, i: uint) -> bool {
|
||||
|
||||
// FIXME: This doesn't account for the actual size of the vectors,
|
||||
// so it could end up comparing garbage bits
|
||||
/*
|
||||
Function: equal
|
||||
|
||||
#[doc = "
|
||||
Compares two bitvectors
|
||||
|
||||
Preconditions:
|
||||
|
||||
Both bitvectors must be the same length
|
||||
|
||||
Returns:
|
||||
|
||||
True if both bitvectors contain identical elements
|
||||
*/
|
||||
Both bitvectors must be the same length. Returns `true` if both bitvectors
|
||||
contain identical elements.
|
||||
"]
|
||||
fn equal(v0: t, v1: t) -> bool {
|
||||
// FIXME: when we can break or return from inside an iterator loop,
|
||||
// we can eliminate this painful while-loop
|
||||
@ -165,51 +121,31 @@ fn equal(v0: t, v1: t) -> bool {
|
||||
ret true;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: clear
|
||||
|
||||
Set all bits to 0
|
||||
*/
|
||||
#[doc = "Set all bits to 0"]
|
||||
fn clear(v: t) {
|
||||
uint::range(0u, vec::len(v.storage)) {|i| v.storage[i] = 0u; };
|
||||
}
|
||||
|
||||
/*
|
||||
Function: set_all
|
||||
|
||||
Set all bits to 1
|
||||
*/
|
||||
#[doc = "Set all bits to 1"]
|
||||
fn set_all(v: t) {
|
||||
uint::range(0u, v.nbits) {|i| set(v, i, true); };
|
||||
}
|
||||
|
||||
/*
|
||||
Function: invert
|
||||
|
||||
Invert all bits
|
||||
*/
|
||||
#[doc = "Invert all bits"]
|
||||
fn invert(v: t) {
|
||||
uint::range(0u, vec::len(v.storage)) {|i|
|
||||
v.storage[i] = !v.storage[i];
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
Function: difference
|
||||
|
||||
#[doc = "
|
||||
Calculate the difference between two bitvectors
|
||||
|
||||
Sets each element of `v0` to the value of that element minus the element
|
||||
of `v1` at the same index.
|
||||
of `v1` at the same index. Both bitvectors must be the same length.
|
||||
|
||||
Preconditions:
|
||||
|
||||
Both bitvectors must be the same length
|
||||
|
||||
Returns:
|
||||
|
||||
True if `v0` was changed
|
||||
*/
|
||||
Returns `true` if `v0` was changed.
|
||||
"]
|
||||
fn difference(v0: t, v1: t) -> bool {
|
||||
invert(v1);
|
||||
let b = intersect(v0, v1);
|
||||
@ -217,15 +153,11 @@ fn difference(v0: t, v1: t) -> bool {
|
||||
ret b;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: set
|
||||
|
||||
#[doc = "
|
||||
Set the value of a bit at a given index
|
||||
|
||||
Preconditions:
|
||||
|
||||
`i` must be less than the length of the bitvector
|
||||
*/
|
||||
`i` must be less than the length of the bitvector.
|
||||
"]
|
||||
fn set(v: t, i: uint, x: bool) {
|
||||
assert (i < v.nbits);
|
||||
let bits = uint_bits;
|
||||
@ -236,22 +168,14 @@ fn set(v: t, i: uint, x: bool) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Function: is_true
|
||||
|
||||
Returns true if all bits are 1
|
||||
*/
|
||||
#[doc = "Returns true if all bits are 1"]
|
||||
fn is_true(v: t) -> bool {
|
||||
for i: uint in to_vec(v) { if i != 1u { ret false; } }
|
||||
ret true;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Function: is_false
|
||||
|
||||
Returns true if all bits are 0
|
||||
*/
|
||||
#[doc = "Returns true if all bits are 0"]
|
||||
fn is_false(v: t) -> bool {
|
||||
for i: uint in to_vec(v) { if i == 1u { ret false; } }
|
||||
ret true;
|
||||
@ -259,39 +183,35 @@ fn is_false(v: t) -> bool {
|
||||
|
||||
fn init_to_vec(v: t, i: uint) -> uint { ret if get(v, i) { 1u } else { 0u }; }
|
||||
|
||||
/*
|
||||
Function: to_vec
|
||||
#[doc = "
|
||||
Converts the bitvector to a vector of uint with the same length.
|
||||
|
||||
Converts the bitvector to a vector of uint with the same length. Each uint
|
||||
in the resulting vector has either value 0u or 1u.
|
||||
*/
|
||||
Each uint in the resulting vector has either value 0u or 1u.
|
||||
"]
|
||||
fn to_vec(v: t) -> [uint] {
|
||||
let sub = bind init_to_vec(v, _);
|
||||
ret vec::init_fn::<uint>(v.nbits, sub);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: to_str
|
||||
|
||||
Converts the bitvector to a string. The resulting string has the same
|
||||
length as the bitvector, and each character is either '0' or '1'.
|
||||
*/
|
||||
#[doc = "
|
||||
Converts the bitvector to a string.
|
||||
|
||||
The resulting string has the same length as the bitvector, and each character
|
||||
is either '0' or '1'.
|
||||
"]
|
||||
fn to_str(v: t) -> str {
|
||||
let rs = "";
|
||||
for i: uint in to_vec(v) { if i == 1u { rs += "1"; } else { rs += "0"; } }
|
||||
ret rs;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: eq_vec
|
||||
#[doc = "
|
||||
Compare a bitvector to a vector of uint
|
||||
|
||||
Compare a bitvector to a vector of uint. The uint vector is expected to
|
||||
only contain the values 0u and 1u.
|
||||
|
||||
Preconditions:
|
||||
|
||||
Both the bitvector and vector must have the same length
|
||||
*/
|
||||
The uint vector is expected to only contain the values 0u and 1u. Both the
|
||||
bitvector and vector must have the same length
|
||||
"]
|
||||
fn eq_vec(v0: t, v1: [uint]) -> bool {
|
||||
assert (v0.nbits == vec::len::<uint>(v1));
|
||||
let len = v0.nbits;
|
||||
|
@ -1,6 +1,4 @@
|
||||
/*
|
||||
Module: c_vec
|
||||
|
||||
#[doc = "
|
||||
Library to interface with chunks of memory allocated in C.
|
||||
|
||||
It is often desirable to safely interface with memory allocated from C,
|
||||
@ -25,8 +23,7 @@ itself around, the c_vec could be garbage collected, and the memory within
|
||||
could be destroyed. There are legitimate uses for the pointer elimination
|
||||
form -- for instance, to pass memory back into C -- but great care must be
|
||||
taken to ensure that a reference to the c_vec::t is still held if needed.
|
||||
|
||||
*/
|
||||
"];
|
||||
|
||||
export t;
|
||||
export create, create_with_dtor;
|
||||
@ -34,14 +31,12 @@ export get, set;
|
||||
export len;
|
||||
export ptr;
|
||||
|
||||
/*
|
||||
Type: t
|
||||
|
||||
The type representing a native chunk of memory. Wrapped in a enum for
|
||||
opacity; FIXME #818 when it is possible to have truly opaque types, this
|
||||
should be revisited.
|
||||
*/
|
||||
#[doc = "
|
||||
The type representing a native chunk of memory
|
||||
|
||||
Wrapped in a enum for opacity; FIXME #818 when it is possible to have
|
||||
truly opaque types, this should be revisited.
|
||||
"]
|
||||
enum t<T> {
|
||||
t({ base: *mutable T, len: uint, rsrc: @dtor_res})
|
||||
}
|
||||
@ -57,16 +52,14 @@ resource dtor_res(dtor: option<fn@()>) {
|
||||
Section: Introduction forms
|
||||
*/
|
||||
|
||||
/*
|
||||
Function: create
|
||||
|
||||
#[doc = "
|
||||
Create a c_vec::t from a native buffer with a given length.
|
||||
|
||||
Parameters:
|
||||
# Arguments
|
||||
|
||||
base - A native pointer to a buffer
|
||||
len - The number of elements in the buffer
|
||||
*/
|
||||
* base - A native pointer to a buffer
|
||||
* len - The number of elements in the buffer
|
||||
"]
|
||||
unsafe fn create<T>(base: *mutable T, len: uint) -> t<T> {
|
||||
ret t({base: base,
|
||||
len: len,
|
||||
@ -74,19 +67,17 @@ unsafe fn create<T>(base: *mutable T, len: uint) -> t<T> {
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
Function: create_with_dtor
|
||||
|
||||
#[doc = "
|
||||
Create a c_vec::t from a native buffer, with a given length,
|
||||
and a function to run upon destruction.
|
||||
|
||||
Parameters:
|
||||
# Arguments
|
||||
|
||||
base - A native pointer to a buffer
|
||||
len - The number of elements in the buffer
|
||||
dtor - A function to run when the value is destructed, useful
|
||||
for freeing the buffer, etc.
|
||||
*/
|
||||
* base - A native pointer to a buffer
|
||||
* len - The number of elements in the buffer
|
||||
* dtor - A function to run when the value is destructed, useful
|
||||
for freeing the buffer, etc.
|
||||
"]
|
||||
unsafe fn create_with_dtor<T>(base: *mutable T, len: uint, dtor: fn@())
|
||||
-> t<T> {
|
||||
ret t({base: base,
|
||||
@ -99,29 +90,21 @@ unsafe fn create_with_dtor<T>(base: *mutable T, len: uint, dtor: fn@())
|
||||
Section: Operations
|
||||
*/
|
||||
|
||||
/*
|
||||
Function: get
|
||||
|
||||
#[doc = "
|
||||
Retrieves an element at a given index
|
||||
|
||||
Failure:
|
||||
|
||||
If `ofs` is greater or equal to the length of the vector
|
||||
*/
|
||||
Fails if `ofs` is greater or equal to the length of the vector
|
||||
"]
|
||||
fn get<T: copy>(t: t<T>, ofs: uint) -> T {
|
||||
assert ofs < len(t);
|
||||
ret unsafe { *ptr::mut_offset((*t).base, ofs) };
|
||||
}
|
||||
|
||||
/*
|
||||
Function: set
|
||||
|
||||
#[doc = "
|
||||
Sets the value of an element at a given index
|
||||
|
||||
Failure:
|
||||
|
||||
If `ofs` is greater or equal to the length of the vector
|
||||
*/
|
||||
Fails if `ofs` is greater or equal to the length of the vector
|
||||
"]
|
||||
fn set<T: copy>(t: t<T>, ofs: uint, v: T) {
|
||||
assert ofs < len(t);
|
||||
unsafe { *ptr::mut_offset((*t).base, ofs) = v };
|
||||
@ -131,20 +114,12 @@ fn set<T: copy>(t: t<T>, ofs: uint, v: T) {
|
||||
Section: Elimination forms
|
||||
*/
|
||||
|
||||
/*
|
||||
Function: len
|
||||
|
||||
Returns the length of the vector
|
||||
*/
|
||||
#[doc = "Returns the length of the vector"]
|
||||
fn len<T>(t: t<T>) -> uint {
|
||||
ret (*t).len;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: ptr
|
||||
|
||||
Returns a pointer to the first element of the vector
|
||||
*/
|
||||
#[doc = "Returns a pointer to the first element of the vector"]
|
||||
unsafe fn ptr<T>(t: t<T>) -> *mutable T {
|
||||
ret (*t).base;
|
||||
}
|
||||
|
@ -1,12 +1,4 @@
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Unsafe debugging functions for inspecting values.
|
||||
*
|
||||
* Your RUST_LOG environment variable must contain "stdlib" for any debug
|
||||
* logging.
|
||||
*/
|
||||
#[doc = "Unsafe debugging functions for inspecting values."];
|
||||
|
||||
#[abi = "cdecl"]
|
||||
native mod rustrt {
|
||||
|
@ -1,40 +1,18 @@
|
||||
#[doc = "A deque. Untested as of yet. Likely buggy"];
|
||||
|
||||
import option::{some, none};
|
||||
|
||||
/*
|
||||
Module: deque
|
||||
|
||||
A deque. Untested as of yet. Likely buggy.
|
||||
*/
|
||||
|
||||
/*
|
||||
Iface: t
|
||||
*/
|
||||
iface t<T> {
|
||||
// Method: size
|
||||
fn size() -> uint;
|
||||
// Method: add_front
|
||||
fn add_front(T);
|
||||
// Method: add_back
|
||||
fn add_back(T);
|
||||
// Method: pop_front
|
||||
fn pop_front() -> T;
|
||||
// Method: pop_back
|
||||
fn pop_back() -> T;
|
||||
// Method: peek_front
|
||||
fn peek_front() -> T;
|
||||
// Method: peek_back
|
||||
fn peek_back() -> T;
|
||||
// Method: get
|
||||
fn get(int) -> T;
|
||||
}
|
||||
|
||||
/*
|
||||
Section: Functions
|
||||
*/
|
||||
|
||||
/*
|
||||
Function: create
|
||||
*/
|
||||
// FIXME eventually, a proper datatype plus an exported impl would be
|
||||
// preferrable
|
||||
fn create<T: copy>() -> t<T> {
|
||||
|
@ -1,8 +1,6 @@
|
||||
// -*- rust -*-
|
||||
|
||||
/*
|
||||
Module: four
|
||||
|
||||
#[doc = "
|
||||
The fourrternary Belnap relevance logic FOUR represented as ADT
|
||||
|
||||
This allows reasoning with four logic values (true, false, none, both).
|
||||
@ -10,7 +8,7 @@ This allows reasoning with four logic values (true, false, none, both).
|
||||
Implementation: Truth values are represented using a single u8 and
|
||||
all operations are done using bit operations which is fast
|
||||
on current cpus.
|
||||
*/
|
||||
"];
|
||||
|
||||
import tri;
|
||||
|
||||
@ -19,152 +17,93 @@ export not, and, or, xor, implies, implies_materially;
|
||||
export eq, ne, is_true, is_false;
|
||||
export from_str, to_str, all_values, to_trit, to_bit;
|
||||
|
||||
/*
|
||||
Type: t
|
||||
|
||||
#[doc = "
|
||||
The type of fourrternary logic values
|
||||
|
||||
It may be thought of as tuple `(y, x)` of two bools
|
||||
|
||||
*/
|
||||
"]
|
||||
type t = u8;
|
||||
|
||||
const b0: u8 = 1u8;
|
||||
const b1: u8 = 2u8;
|
||||
const b01: u8 = 3u8;
|
||||
|
||||
/*
|
||||
Constant: none
|
||||
|
||||
Logic value `(0, 0)` for bottom (neither true or false)
|
||||
*/
|
||||
#[doc = "Logic value `(0, 0)` for bottom (neither true or false)"]
|
||||
const none: t = 0u8;
|
||||
|
||||
/*
|
||||
Constant: true
|
||||
|
||||
Logic value `(0, 1)` for truth
|
||||
*/
|
||||
#[doc = "Logic value `(0, 1)` for truth"]
|
||||
const true: t = 1u8;
|
||||
|
||||
/*
|
||||
Constant: false
|
||||
|
||||
Logic value `(1, 0)` for falsehood
|
||||
*/
|
||||
#[doc = "Logic value `(1, 0)` for falsehood"]
|
||||
const false: t = 2u8;
|
||||
|
||||
/*
|
||||
Constant: both
|
||||
|
||||
Logic value `(1, 1)` for top (both true and false)
|
||||
*/
|
||||
#[doc = "Logic value `(1, 1)` for top (both true and false)"]
|
||||
const both: t = 3u8;
|
||||
|
||||
/* Function: not
|
||||
|
||||
#[doc = "
|
||||
Negation/Inverse
|
||||
|
||||
Returns:
|
||||
|
||||
`'(v.y, v.x)`
|
||||
*/
|
||||
Returns `'(v.y, v.x)`
|
||||
"]
|
||||
pure fn not(v: t) -> t { ((v << 1u8) | (v >> 1u8)) & b01 }
|
||||
|
||||
/* Function: and
|
||||
|
||||
#[doc = "
|
||||
Conjunction
|
||||
|
||||
Returns:
|
||||
|
||||
`(a.x | b.x, a.y & b.y)`
|
||||
*/
|
||||
Returns `(a.x | b.x, a.y & b.y)`
|
||||
"]
|
||||
pure fn and(a: t, b: t) -> t { ((a & b) & b0) | ((a | b) & b1) }
|
||||
|
||||
/* Function: or
|
||||
|
||||
#[doc = "
|
||||
Disjunction
|
||||
|
||||
Returns:
|
||||
|
||||
`(a.x & b.x, a.y | b.y)`
|
||||
*/
|
||||
Returns `(a.x & b.x, a.y | b.y)`
|
||||
"]
|
||||
pure fn or(a: t, b: t) -> t { ((a | b) & b0) | ((a & b) & b1) }
|
||||
|
||||
/* Function: xor
|
||||
|
||||
#[doc = "
|
||||
Classic exclusive or
|
||||
|
||||
Returns:
|
||||
|
||||
`or(and(a, not(b)), and(not(a), b))`
|
||||
*/
|
||||
Returns `or(and(a, not(b)), and(not(a), b))`
|
||||
"]
|
||||
pure fn xor(a: t, b: t) -> t { or(and(a, not(b)), and(not(a), b)) }
|
||||
|
||||
/*
|
||||
Function: implies
|
||||
|
||||
#[doc = "
|
||||
Strong implication (from `a` strongly follows `b`)
|
||||
|
||||
Returns:
|
||||
|
||||
`( x1 & y2, !x1 | x2)`
|
||||
*/
|
||||
Returns `( x1 & y2, !x1 | x2)`
|
||||
"]
|
||||
pure fn implies(a: t, b: t) -> t { ((a << 1u8) & b & b1) | (((!a) | b) & b0) }
|
||||
|
||||
/*
|
||||
Function: implies_materially
|
||||
|
||||
#[doc = "
|
||||
Classic (material) implication in the logic
|
||||
(from `a` materially follows `b`)
|
||||
|
||||
Returns:
|
||||
|
||||
`or(not(a), b)`
|
||||
*/
|
||||
Returns `or(not(a), b)`
|
||||
"]
|
||||
pure fn implies_materially(a: t, b: t) -> t { or(not(a), b) }
|
||||
|
||||
/*
|
||||
Predicate: eq
|
||||
|
||||
Returns:
|
||||
|
||||
true if truth values `a` and `b` are indistinguishable in the logic
|
||||
*/
|
||||
#[doc = "
|
||||
Returns true if truth values `a` and `b` are indistinguishable in the logic
|
||||
"]
|
||||
pure fn eq(a: t, b: t) -> bool { a == b }
|
||||
|
||||
/*
|
||||
Predicate: ne
|
||||
|
||||
Returns:
|
||||
|
||||
true if truth values `a` and `b` are distinguishable in the logic
|
||||
*/
|
||||
#[doc = "
|
||||
Returns true if truth values `a` and `b` are distinguishable in the logic
|
||||
"]
|
||||
pure fn ne(a: t, b: t) -> bool { a != b }
|
||||
|
||||
/*
|
||||
Predicate: is_true
|
||||
|
||||
Returns:
|
||||
|
||||
true if `v` represents truth in the logic (is `true` or `both`)
|
||||
*/
|
||||
#[doc = "
|
||||
Returns true if `v` represents truth in the logic (is `true` or `both`)
|
||||
"]
|
||||
pure fn is_true(v: t) -> bool { (v & b0) != 0u8 }
|
||||
|
||||
/*
|
||||
Predicate: is_false
|
||||
|
||||
Returns:
|
||||
|
||||
true if `v` represents falsehood in the logic (is `false` or `none`)
|
||||
*/
|
||||
#[doc = "
|
||||
Returns true if `v` represents falsehood in the logic (is `false` or `none`)
|
||||
"]
|
||||
pure fn is_false(v: t) -> bool { (v & b0) == 0u8 }
|
||||
|
||||
/*
|
||||
Function: from_str
|
||||
|
||||
Parse logic value from `s`
|
||||
*/
|
||||
#[doc = "Parse logic value from `s`"]
|
||||
pure fn from_str(s: str) -> t {
|
||||
alt check s {
|
||||
"none" { none }
|
||||
@ -174,11 +113,7 @@ pure fn from_str(s: str) -> t {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: to_str
|
||||
|
||||
Convert `v` into a string
|
||||
*/
|
||||
#[doc = "Convert `v` into a string"]
|
||||
pure fn to_str(v: t) -> str {
|
||||
// FIXME replace with consts as soon as that works
|
||||
alt check v {
|
||||
@ -189,12 +124,10 @@ pure fn to_str(v: t) -> str {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: all_values
|
||||
|
||||
Iterates over all truth values by passing them to `blk`
|
||||
in an unspecified order
|
||||
*/
|
||||
#[doc = "
|
||||
Iterates over all truth values by passing them to `blk` in an unspecified
|
||||
order
|
||||
"]
|
||||
fn all_values(blk: fn(v: t)) {
|
||||
blk(both);
|
||||
blk(four::true);
|
||||
@ -202,22 +135,15 @@ fn all_values(blk: fn(v: t)) {
|
||||
blk(none);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: to_bit
|
||||
|
||||
Returns:
|
||||
|
||||
An u8 whose first bit is set if `if_true(v)` holds
|
||||
*/
|
||||
#[doc = "
|
||||
Returns an `u8` whose first bit is set if `if_true(v)` holds
|
||||
"]
|
||||
fn to_bit(v: t) -> u8 { v & b0 }
|
||||
|
||||
/*
|
||||
Function: to_tri
|
||||
|
||||
Returns:
|
||||
|
||||
A trit of `v` (`both` and `none` are both coalesced into `trit::unknown`)
|
||||
*/
|
||||
#[doc = "
|
||||
Returns a trit of `v` (`both` and `none` are both coalesced into
|
||||
`trit::unknown`)
|
||||
"]
|
||||
fn to_trit(v: t) -> tri::t { v & (v ^ not(v)) }
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -1,6 +1,4 @@
|
||||
/*
|
||||
Module: fun_treemap
|
||||
|
||||
#[doc = "
|
||||
A functional key,value store that works on anything.
|
||||
|
||||
This works using a binary search tree. In the first version, it's a
|
||||
@ -9,8 +7,7 @@ red-black tree or something else.
|
||||
|
||||
This is copied and modified from treemap right now. It's missing a lot
|
||||
of features.
|
||||
|
||||
*/
|
||||
"];
|
||||
|
||||
import option::{some, none};
|
||||
import option = option;
|
||||
@ -21,35 +18,17 @@ export insert;
|
||||
export find;
|
||||
export traverse;
|
||||
|
||||
/* Section: Types */
|
||||
|
||||
/*
|
||||
Type: treemap
|
||||
*/
|
||||
type treemap<K, V> = @tree_node<K, V>;
|
||||
|
||||
/*
|
||||
Tag: tree_node
|
||||
*/
|
||||
enum tree_node<K, V> {
|
||||
empty,
|
||||
node(@K, @V, @tree_node<K, V>, @tree_node<K, V>)
|
||||
}
|
||||
|
||||
/* Section: Operations */
|
||||
|
||||
/*
|
||||
Function: init
|
||||
|
||||
Create a treemap
|
||||
*/
|
||||
#[doc = "Create a treemap"]
|
||||
fn init<K, V>() -> treemap<K, V> { @empty }
|
||||
|
||||
/*
|
||||
Function: insert
|
||||
|
||||
Insert a value into the map
|
||||
*/
|
||||
#[doc = "Insert a value into the map"]
|
||||
fn insert<K: copy, V: copy>(m: treemap<K, V>, k: K, v: V) -> treemap<K, V> {
|
||||
@alt m {
|
||||
@empty { node(@k, @v, @empty, @empty) }
|
||||
@ -63,11 +42,7 @@ fn insert<K: copy, V: copy>(m: treemap<K, V>, k: K, v: V) -> treemap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: find
|
||||
|
||||
Find a value based on the key
|
||||
*/
|
||||
#[doc = "Find a value based on the key"]
|
||||
fn find<K, V: copy>(m: treemap<K, V>, k: K) -> option<V> {
|
||||
alt *m {
|
||||
empty { none }
|
||||
@ -79,11 +54,7 @@ fn find<K, V: copy>(m: treemap<K, V>, k: K) -> option<V> {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: traverse
|
||||
|
||||
Visit all pairs in the map in order.
|
||||
*/
|
||||
#[doc = "Visit all pairs in the map in order."]
|
||||
fn traverse<K, V: copy>(m: treemap<K, V>, f: fn(K, V)) {
|
||||
alt *m {
|
||||
empty { }
|
||||
|
@ -1,49 +1,49 @@
|
||||
/*
|
||||
Module: getopts
|
||||
#[doc = "
|
||||
Simple getopt alternative.
|
||||
|
||||
Simple getopt alternative. Construct a vector of options, either by using
|
||||
reqopt, optopt, and optflag or by building them from components yourself, and
|
||||
pass them to getopts, along with a vector of actual arguments (not including
|
||||
argv[0]). You'll either get a failure code back, or a match. You'll have to
|
||||
verify whether the amount of 'free' arguments in the match is what you
|
||||
expect. Use opt_* accessors to get argument values out of the match object.
|
||||
Construct a vector of options, either by using reqopt, optopt, and optflag or
|
||||
by building them from components yourself, and pass them to getopts, along
|
||||
with a vector of actual arguments (not including argv[0]). You'll either get a
|
||||
failure code back, or a match. You'll have to verify whether the amount of
|
||||
'free' arguments in the match is what you expect. Use opt_* accessors to get
|
||||
argument values out of the match object.
|
||||
|
||||
Single-character options are expected to appear on the command line with a
|
||||
single preceeding dash; multiple-character options are expected to be
|
||||
proceeded by two dashes. Options that expect an argument accept their argument
|
||||
following either a space or an equals sign.
|
||||
|
||||
Example:
|
||||
# Example
|
||||
|
||||
The following example shows simple command line parsing for an application
|
||||
that requires an input file to be specified, accepts an optional output file
|
||||
name following -o, and accepts both -h and --help as optional flags.
|
||||
|
||||
> fn main(args: [str]) {
|
||||
> let opts = [
|
||||
> optopt("o"),
|
||||
> optflag("h"),
|
||||
> optflag("help")
|
||||
> ];
|
||||
> let match = alt getopts(vec::shift(args), opts) {
|
||||
> ok(m) { m }
|
||||
> err(f) { fail fail_str(f) }
|
||||
> };
|
||||
> if opt_present(match, "h") || opt_present(match, "help") {
|
||||
> print_usage();
|
||||
> ret;
|
||||
> }
|
||||
> let output = opt_maybe_str(match, "o");
|
||||
> let input = if !vec::is_empty(match.free) {
|
||||
> match.free[0]
|
||||
> } else {
|
||||
> print_usage();
|
||||
> ret;
|
||||
> }
|
||||
> do_work(input, output);
|
||||
> }
|
||||
fn main(args: [str]) {
|
||||
let opts = [
|
||||
optopt(\"o\"),
|
||||
optflag(\"h\"),
|
||||
optflag(\"help\")
|
||||
];
|
||||
let match = alt getopts(vec::shift(args), opts) {
|
||||
ok(m) { m }
|
||||
err(f) { fail fail_str(f) }
|
||||
};
|
||||
if opt_present(match, \"h\") || opt_present(match, \"help\") {
|
||||
print_usage();
|
||||
ret;
|
||||
}
|
||||
let output = opt_maybe_str(match, \"o\");
|
||||
let input = if !vec::is_empty(match.free) {
|
||||
match.free[0]
|
||||
} else {
|
||||
print_usage();
|
||||
ret;
|
||||
}
|
||||
do_work(input, output);
|
||||
}
|
||||
|
||||
*/
|
||||
"];
|
||||
|
||||
import core::result;
|
||||
import core::result::{err, ok};
|
||||
@ -71,11 +71,7 @@ enum hasarg { yes, no, maybe, }
|
||||
|
||||
enum occur { req, optional, multi, }
|
||||
|
||||
/*
|
||||
Type: opt
|
||||
|
||||
A description of a possible option
|
||||
*/
|
||||
#[doc = "A description of a possible option"]
|
||||
type opt = {name: name, hasarg: hasarg, occur: occur};
|
||||
|
||||
fn mkname(nm: str) -> name {
|
||||
@ -84,60 +80,40 @@ fn mkname(nm: str) -> name {
|
||||
} else { long(nm) };
|
||||
}
|
||||
|
||||
/*
|
||||
Function: reqopt
|
||||
|
||||
Create an option that is required and takes an argument
|
||||
*/
|
||||
#[doc = "Create an option that is required and takes an argument"]
|
||||
fn reqopt(name: str) -> opt {
|
||||
ret {name: mkname(name), hasarg: yes, occur: req};
|
||||
}
|
||||
|
||||
/*
|
||||
Function: optopt
|
||||
|
||||
Create an option that is optional and takes an argument
|
||||
*/
|
||||
#[doc = "Create an option that is optional and takes an argument"]
|
||||
fn optopt(name: str) -> opt {
|
||||
ret {name: mkname(name), hasarg: yes, occur: optional};
|
||||
}
|
||||
|
||||
/*
|
||||
Function: optflag
|
||||
|
||||
Create an option that is optional and does not take an argument
|
||||
*/
|
||||
#[doc = "Create an option that is optional and does not take an argument"]
|
||||
fn optflag(name: str) -> opt {
|
||||
ret {name: mkname(name), hasarg: no, occur: optional};
|
||||
}
|
||||
|
||||
/*
|
||||
Function: optflagopt
|
||||
|
||||
Create an option that is optional and takes an optional argument
|
||||
*/
|
||||
#[doc = "Create an option that is optional and takes an optional argument"]
|
||||
fn optflagopt(name: str) -> opt {
|
||||
ret {name: mkname(name), hasarg: maybe, occur: optional};
|
||||
}
|
||||
|
||||
/*
|
||||
Function: optmulti
|
||||
|
||||
#[doc = "
|
||||
Create an option that is optional, takes an argument, and may occur
|
||||
multiple times
|
||||
*/
|
||||
"]
|
||||
fn optmulti(name: str) -> opt {
|
||||
ret {name: mkname(name), hasarg: yes, occur: multi};
|
||||
}
|
||||
|
||||
enum optval { val(str), given, }
|
||||
|
||||
/*
|
||||
Type: match
|
||||
|
||||
#[doc = "
|
||||
The result of checking command line arguments. Contains a vector
|
||||
of matches and a vector of free strings.
|
||||
*/
|
||||
"]
|
||||
type match = {opts: [opt], vals: [mutable [optval]], free: [str]};
|
||||
|
||||
fn is_arg(arg: str) -> bool {
|
||||
@ -152,12 +128,10 @@ fn find_opt(opts: [opt], nm: name) -> option<uint> {
|
||||
vec::position(opts, { |opt| opt.name == nm })
|
||||
}
|
||||
|
||||
/*
|
||||
Type: fail_
|
||||
|
||||
#[doc = "
|
||||
The type returned when the command line does not conform to the
|
||||
expected format. Pass this value to <fail_str> to get an error message.
|
||||
*/
|
||||
"]
|
||||
enum fail_ {
|
||||
argument_missing(str),
|
||||
unrecognized_option(str),
|
||||
@ -166,11 +140,7 @@ enum fail_ {
|
||||
unexpected_argument(str),
|
||||
}
|
||||
|
||||
/*
|
||||
Function: fail_str
|
||||
|
||||
Convert a <fail_> enum into an error string
|
||||
*/
|
||||
#[doc = "Convert a `fail_` enum into an error string"]
|
||||
fn fail_str(f: fail_) -> str {
|
||||
ret alt f {
|
||||
argument_missing(nm) { "Argument to option '" + nm + "' missing." }
|
||||
@ -185,30 +155,19 @@ fn fail_str(f: fail_) -> str {
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
Type: result
|
||||
|
||||
#[doc = "
|
||||
The result of parsing a command line with a set of options
|
||||
(result::t<match, fail_>)
|
||||
|
||||
Variants:
|
||||
|
||||
ok(match) - Returned from getopts on success
|
||||
err(fail_) - Returned from getopts on failure
|
||||
*/
|
||||
"]
|
||||
type result = result::t<match, fail_>;
|
||||
|
||||
/*
|
||||
Function: getopts
|
||||
|
||||
#[doc = "
|
||||
Parse command line arguments according to the provided options
|
||||
|
||||
Returns:
|
||||
|
||||
ok(match) - On success. Use functions such as <opt_present>
|
||||
<opt_str>, etc. to interrogate results.
|
||||
err(fail_) - On failure. Use <fail_str> to get an error message.
|
||||
*/
|
||||
On success returns `ok(opt)`. Use functions such as `opt_present` `opt_str`,
|
||||
etc. to interrogate results. Returns `err(fail_)` on failure. Use <fail_str>
|
||||
to get an error message.
|
||||
"]
|
||||
fn getopts(args: [str], opts: [opt]) -> result unsafe {
|
||||
let n_opts = vec::len::<opt>(opts);
|
||||
fn f(_x: uint) -> [optval] { ret []; }
|
||||
@ -311,35 +270,25 @@ fn opt_vals(m: match, nm: str) -> [optval] {
|
||||
|
||||
fn opt_val(m: match, nm: str) -> optval { ret opt_vals(m, nm)[0]; }
|
||||
|
||||
/*
|
||||
Function: opt_present
|
||||
|
||||
Returns true if an option was matched
|
||||
*/
|
||||
#[doc = "Returns true if an option was matched"]
|
||||
fn opt_present(m: match, nm: str) -> bool {
|
||||
ret vec::len::<optval>(opt_vals(m, nm)) > 0u;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: opt_str
|
||||
|
||||
#[doc = "
|
||||
Returns the string argument supplied to a matching option
|
||||
|
||||
Failure:
|
||||
|
||||
- If the option was not matched
|
||||
- If the match did not take an argument
|
||||
*/
|
||||
Fails if the option was not matched or if the match did not take an argument
|
||||
"]
|
||||
fn opt_str(m: match, nm: str) -> str {
|
||||
ret alt opt_val(m, nm) { val(s) { s } _ { fail } };
|
||||
}
|
||||
|
||||
/*
|
||||
Function: opt_str
|
||||
|
||||
#[doc = "
|
||||
Returns a vector of the arguments provided to all matches of the given option.
|
||||
|
||||
Used when an option accepts multiple values.
|
||||
*/
|
||||
"]
|
||||
fn opt_strs(m: match, nm: str) -> [str] {
|
||||
let acc: [str] = [];
|
||||
for v: optval in opt_vals(m, nm) {
|
||||
@ -348,11 +297,9 @@ fn opt_strs(m: match, nm: str) -> [str] {
|
||||
ret acc;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: opt_str
|
||||
|
||||
#[doc = "
|
||||
Returns the string argument supplied to a matching option or none
|
||||
*/
|
||||
"]
|
||||
fn opt_maybe_str(m: match, nm: str) -> option<str> {
|
||||
let vals = opt_vals(m, nm);
|
||||
if vec::len::<optval>(vals) == 0u { ret none::<str>; }
|
||||
@ -360,15 +307,13 @@ fn opt_maybe_str(m: match, nm: str) -> option<str> {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Function: opt_default
|
||||
|
||||
#[doc = "
|
||||
Returns the matching string, a default, or none
|
||||
|
||||
Returns none if the option was not present, `def` if the option was
|
||||
present but no argument was provided, and the argument if the option was
|
||||
present and an argument was provided.
|
||||
*/
|
||||
"]
|
||||
fn opt_default(m: match, nm: str, def: str) -> option<str> {
|
||||
let vals = opt_vals(m, nm);
|
||||
if vec::len::<optval>(vals) == 0u { ret none::<str>; }
|
||||
|
@ -1,6 +1,8 @@
|
||||
// Rust JSON serialization library
|
||||
// Copyright (c) 2011 Google Inc.
|
||||
|
||||
#[doc = "json serialization"];
|
||||
|
||||
import result::{ok, err};
|
||||
import io;
|
||||
import io::{reader_util, writer_util};
|
||||
@ -22,23 +24,13 @@ export list;
|
||||
export dict;
|
||||
export null;
|
||||
|
||||
/*
|
||||
Tag: json
|
||||
|
||||
Represents a json value.
|
||||
*/
|
||||
#[doc = "Represents a json value"]
|
||||
enum json {
|
||||
/* Variant: num */
|
||||
num(float),
|
||||
/* Variant: string */
|
||||
string(str),
|
||||
/* Variant: boolean */
|
||||
boolean(bool),
|
||||
/* Variant: list */
|
||||
list([json]),
|
||||
/* Variant: dict */
|
||||
dict(map::hashmap<str,json>),
|
||||
/* Variant: null */
|
||||
null,
|
||||
}
|
||||
|
||||
@ -48,11 +40,7 @@ type error = {
|
||||
msg: str,
|
||||
};
|
||||
|
||||
/*
|
||||
Function: to_writer
|
||||
|
||||
Serializes a json value into a io::writer.
|
||||
*/
|
||||
#[doc = "Serializes a json value into a io::writer"]
|
||||
fn to_writer(wr: io::writer, j: json) {
|
||||
alt j {
|
||||
num(n) { wr.write_str(float::to_str(n, 6u)); }
|
||||
@ -114,11 +102,7 @@ fn to_writer(wr: io::writer, j: json) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: to_str
|
||||
|
||||
Serializes a json value into a string.
|
||||
*/
|
||||
#[doc = "Serializes a json value into a string"]
|
||||
fn to_str(j: json) -> str {
|
||||
io::with_str_writer { |wr| to_writer(wr, j) }
|
||||
}
|
||||
@ -469,12 +453,7 @@ impl parser for parser {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: from_reader
|
||||
|
||||
Deserializes a json value from an io::reader.
|
||||
*/
|
||||
|
||||
#[doc = "Deserializes a json value from an io::reader"]
|
||||
fn from_reader(rdr: io::reader) -> result::t<json, error> {
|
||||
let parser = {
|
||||
rdr: rdr,
|
||||
@ -486,20 +465,12 @@ fn from_reader(rdr: io::reader) -> result::t<json, error> {
|
||||
parser.parse()
|
||||
}
|
||||
|
||||
/*
|
||||
Function: from_str
|
||||
|
||||
Deserializes a json value from a string.
|
||||
*/
|
||||
#[doc = "Deserializes a json value from a string"]
|
||||
fn from_str(s: str) -> result::t<json, error> {
|
||||
io::with_str_reader(s, from_reader)
|
||||
}
|
||||
|
||||
/*
|
||||
Function: eq
|
||||
|
||||
Test if two json values are equal.
|
||||
*/
|
||||
#[doc = "Test if two json values are equal"]
|
||||
fn eq(value0: json, value1: json) -> bool {
|
||||
alt (value0, value1) {
|
||||
(num(f0), num(f1)) { f0 == f1 }
|
||||
|
@ -1,66 +1,45 @@
|
||||
/*
|
||||
Module: list
|
||||
|
||||
A standard linked list
|
||||
*/
|
||||
#[doc = "A standard linked list"];
|
||||
|
||||
import core::option;
|
||||
import option::*;
|
||||
import option::{some, none};
|
||||
|
||||
/* Section: Types */
|
||||
|
||||
/*
|
||||
Tag: list
|
||||
*/
|
||||
enum list<T> {
|
||||
/* Variant: cons */
|
||||
cons(T, @list<T>),
|
||||
/* Variant: nil */
|
||||
nil,
|
||||
}
|
||||
|
||||
/*Section: Operations */
|
||||
|
||||
/*
|
||||
Function: from_vec
|
||||
|
||||
Create a list from a vector
|
||||
*/
|
||||
#[doc = "Create a list from a vector"]
|
||||
fn from_vec<T: copy>(v: [const T]) -> list<T> {
|
||||
*vec::foldr(v, @nil::<T>, { |h, t| @cons(h, t) })
|
||||
}
|
||||
|
||||
/*
|
||||
Function: foldl
|
||||
|
||||
#[doc = "
|
||||
Left fold
|
||||
|
||||
Applies `f` to `u` and the first element in the list, then applies
|
||||
`f` to the result of the previous call and the second element,
|
||||
and so on, returning the accumulated result.
|
||||
Applies `f` to `u` and the first element in the list, then applies `f` to the
|
||||
result of the previous call and the second element, and so on, returning the
|
||||
accumulated result.
|
||||
|
||||
Parameters:
|
||||
# Arguments
|
||||
|
||||
ls - The list to fold
|
||||
z - The initial value
|
||||
f - The function to apply
|
||||
*/
|
||||
* ls - The list to fold
|
||||
* z - The initial value
|
||||
* f - The function to apply
|
||||
"]
|
||||
fn foldl<T: copy, U>(ls: list<U>, z: T, f: fn(T, U) -> T) -> T {
|
||||
let accum: T = z;
|
||||
iter(ls) {|elt| accum = f(accum, elt);}
|
||||
accum
|
||||
}
|
||||
|
||||
/*
|
||||
Function: find
|
||||
|
||||
#[doc = "
|
||||
Search for an element that matches a given predicate
|
||||
|
||||
Apply function `f` to each element of `v`, starting from the first.
|
||||
When function `f` returns true then an option containing the element
|
||||
is returned. If `f` matches no elements then none is returned.
|
||||
*/
|
||||
"]
|
||||
fn find<T: copy, U: copy>(ls: list<T>, f: fn(T) -> option<U>)
|
||||
-> option<U> {
|
||||
let ls = ls;
|
||||
@ -75,11 +54,7 @@ fn find<T: copy, U: copy>(ls: list<T>, f: fn(T) -> option<U>)
|
||||
ret none;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: has
|
||||
|
||||
Returns true if a list contains an element with the given value
|
||||
*/
|
||||
#[doc = "Returns true if a list contains an element with the given value"]
|
||||
fn has<T: copy>(ls: list<T>, elt: T) -> bool {
|
||||
let ls = ls;
|
||||
while true {
|
||||
@ -91,11 +66,7 @@ fn has<T: copy>(ls: list<T>, elt: T) -> bool {
|
||||
ret false;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: is_empty
|
||||
|
||||
Returns true if the list is empty.
|
||||
*/
|
||||
#[doc = "Returns true if the list is empty"]
|
||||
pure fn is_empty<T: copy>(ls: list<T>) -> bool {
|
||||
alt ls {
|
||||
nil { true }
|
||||
@ -103,31 +74,19 @@ pure fn is_empty<T: copy>(ls: list<T>) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: is_not_empty
|
||||
|
||||
Returns true if the list is not empty.
|
||||
*/
|
||||
#[doc = "Returns true if the list is not empty"]
|
||||
pure fn is_not_empty<T: copy>(ls: list<T>) -> bool {
|
||||
ret !is_empty(ls);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: len
|
||||
|
||||
Returns the length of a list
|
||||
*/
|
||||
#[doc = "Returns the length of a list"]
|
||||
fn len<T>(ls: list<T>) -> uint {
|
||||
let count = 0u;
|
||||
iter(ls) {|_e| count += 1u;}
|
||||
count
|
||||
}
|
||||
|
||||
/*
|
||||
Function: tail
|
||||
|
||||
Returns all but the first element of a list
|
||||
*/
|
||||
#[doc = "Returns all but the first element of a list"]
|
||||
pure fn tail<T: copy>(ls: list<T>) -> list<T> {
|
||||
alt ls {
|
||||
cons(_, tl) { ret *tl; }
|
||||
@ -135,20 +94,12 @@ pure fn tail<T: copy>(ls: list<T>) -> list<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: head
|
||||
|
||||
Returns the first element of a list
|
||||
*/
|
||||
#[doc = "Returns the first element of a list"]
|
||||
pure fn head<T: copy>(ls: list<T>) -> T {
|
||||
alt check ls { cons(hd, _) { hd } }
|
||||
}
|
||||
|
||||
/*
|
||||
Function: append
|
||||
|
||||
Appends one list to another
|
||||
*/
|
||||
#[doc = "Appends one list to another"]
|
||||
pure fn append<T: copy>(l: list<T>, m: list<T>) -> list<T> {
|
||||
alt l {
|
||||
nil { ret m; }
|
||||
@ -156,11 +107,7 @@ pure fn append<T: copy>(l: list<T>, m: list<T>) -> list<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: iter
|
||||
|
||||
Iterate over a list
|
||||
*/
|
||||
#[doc = "Iterate over a list"]
|
||||
fn iter<T>(l: list<T>, f: fn(T)) {
|
||||
alt l {
|
||||
cons(hd, tl) {
|
||||
|
@ -1,107 +1,65 @@
|
||||
/*
|
||||
Module: map
|
||||
|
||||
A map type
|
||||
*/
|
||||
#[doc = "A map type"];
|
||||
|
||||
import chained::hashmap;
|
||||
export hashmap, hashfn, eqfn, set, map, chained, mk_hashmap, new_str_hash;
|
||||
export new_bytes_hash, new_int_hash, new_uint_hash, set_add;
|
||||
|
||||
/* Section: Types */
|
||||
#[doc = "
|
||||
A function that returns a hash of a value
|
||||
|
||||
/*
|
||||
Type: hashfn
|
||||
|
||||
A function that returns a hash of a value.
|
||||
The hash should concentrate entropy in the
|
||||
lower bits.
|
||||
*/
|
||||
The hash should concentrate entropy in the lower bits.
|
||||
"]
|
||||
type hashfn<K> = fn@(K) -> uint;
|
||||
|
||||
/*
|
||||
Type: eqfn
|
||||
|
||||
Equality
|
||||
*/
|
||||
type eqfn<K> = fn@(K, K) -> bool;
|
||||
|
||||
/*
|
||||
Type: set
|
||||
|
||||
A convenience type to treat a hashmap as a set
|
||||
*/
|
||||
#[doc = "A convenience type to treat a hashmap as a set"]
|
||||
type set<K> = hashmap<K, ()>;
|
||||
|
||||
type hashmap<K, V> = chained::t<K, V>;
|
||||
|
||||
/*
|
||||
IFace: map
|
||||
*/
|
||||
iface map<K: copy, V: copy> {
|
||||
/*
|
||||
Method: size
|
||||
|
||||
Return the number of elements in the map
|
||||
*/
|
||||
#[doc = "Return the number of elements in the map"]
|
||||
fn size() -> uint;
|
||||
/*
|
||||
Method: insert
|
||||
|
||||
Add a value to the map. If the map already contains a value for
|
||||
the specified key then the original value is replaced.
|
||||
#[doc = "
|
||||
Add a value to the map.
|
||||
|
||||
Returns:
|
||||
If the map already contains a value for the specified key then the
|
||||
original value is replaced.
|
||||
|
||||
True if the key did not already exist in the map
|
||||
*/
|
||||
Returns true if the key did not already exist in the map
|
||||
"]
|
||||
fn insert(K, V) -> bool;
|
||||
/*
|
||||
Method: contains_key
|
||||
|
||||
Returns true if the map contains a value for the specified key
|
||||
*/
|
||||
#[doc = "Returns true if the map contains a value for the specified key"]
|
||||
fn contains_key(K) -> bool;
|
||||
/*
|
||||
Method: get
|
||||
|
||||
Get the value for the specified key
|
||||
|
||||
Failure:
|
||||
|
||||
If the key does not exist in the map
|
||||
*/
|
||||
#[doc = "
|
||||
Get the value for the specified key. Fails if the key does not exist in
|
||||
the map.
|
||||
"]
|
||||
fn get(K) -> V;
|
||||
/*
|
||||
Method: find
|
||||
|
||||
Get the value for the specified key. If the key does not exist
|
||||
in the map then returns none.
|
||||
*/
|
||||
#[doc = "
|
||||
Get the value for the specified key. If the key does not exist in
|
||||
the map then returns none.
|
||||
"]
|
||||
fn find(K) -> option<V>;
|
||||
/*
|
||||
Method: remove
|
||||
|
||||
#[doc = "
|
||||
Remove and return a value from the map. If the key does not exist
|
||||
in the map then returns none.
|
||||
*/
|
||||
"]
|
||||
fn remove(K) -> option<V>;
|
||||
/*
|
||||
Method: items
|
||||
|
||||
Iterate over all the key/value pairs in the map
|
||||
*/
|
||||
#[doc = "Iterate over all the key/value pairs in the map"]
|
||||
fn items(fn(K, V));
|
||||
/*
|
||||
Method: keys
|
||||
|
||||
Iterate over all the keys in the map
|
||||
*/
|
||||
#[doc = "Iterate over all the keys in the map"]
|
||||
fn keys(fn(K));
|
||||
|
||||
/*
|
||||
Iterate over all the values in the map
|
||||
*/
|
||||
#[doc = "Iterate over all the values in the map"]
|
||||
fn values(fn(V));
|
||||
}
|
||||
|
||||
@ -345,51 +303,33 @@ fn mk_hashmap<K: copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>)
|
||||
chained::mk(hasher, eqer)
|
||||
}
|
||||
|
||||
/*
|
||||
Function: new_str_hash
|
||||
|
||||
Construct a hashmap for string keys
|
||||
*/
|
||||
#[doc = "Construct a hashmap for string keys"]
|
||||
fn new_str_hash<V: copy>() -> hashmap<str, V> {
|
||||
ret mk_hashmap(str::hash, str::eq);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: new_bytes_hash
|
||||
|
||||
Construct a hashmap for byte string keys
|
||||
*/
|
||||
#[doc = "Construct a hashmap for byte string keys"]
|
||||
fn new_bytes_hash<V: copy>() -> hashmap<[u8], V> {
|
||||
ret mk_hashmap(vec::u8::hash, vec::u8::eq);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: new_int_hash
|
||||
|
||||
Construct a hashmap for int keys
|
||||
*/
|
||||
#[doc = "Construct a hashmap for int keys"]
|
||||
fn new_int_hash<V: copy>() -> hashmap<int, V> {
|
||||
fn hash_int(&&x: int) -> uint { int::hash(x) }
|
||||
fn eq_int(&&a: int, &&b: int) -> bool { ret a == b; }
|
||||
ret mk_hashmap(hash_int, eq_int);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: new_uint_hash
|
||||
|
||||
Construct a hashmap for uint keys
|
||||
*/
|
||||
#[doc = "Construct a hashmap for uint keys"]
|
||||
fn new_uint_hash<V: copy>() -> hashmap<uint, V> {
|
||||
fn hash_uint(&&x: uint) -> uint { uint::hash(x) }
|
||||
fn eq_uint(&&a: uint, &&b: uint) -> bool { ret a == b; }
|
||||
ret mk_hashmap(hash_uint, eq_uint);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: set_add
|
||||
|
||||
#[doc = "
|
||||
Convenience function for adding keys to a hashmap with nil type keys
|
||||
*/
|
||||
"]
|
||||
fn set_add<K>(set: set<K>, key: K) -> bool { ret set.insert(key, ()); }
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -1,17 +1,7 @@
|
||||
/*
|
||||
Module: net
|
||||
*/
|
||||
|
||||
import vec;
|
||||
import uint;
|
||||
|
||||
/* Section: Types */
|
||||
|
||||
/*
|
||||
Tag: ip_addr
|
||||
|
||||
An IP address
|
||||
*/
|
||||
#[doc = "An IP address"]
|
||||
enum ip_addr {
|
||||
/*
|
||||
Variant: ipv4
|
||||
@ -21,13 +11,7 @@ enum ip_addr {
|
||||
ipv4(u8, u8, u8, u8),
|
||||
}
|
||||
|
||||
/* Section: Operations */
|
||||
|
||||
/*
|
||||
Function: format_addr
|
||||
|
||||
Convert an <ip_addr> to a str
|
||||
*/
|
||||
#[doc = "Convert an `ip_addr` to a str"]
|
||||
fn format_addr(ip: ip_addr) -> str {
|
||||
alt ip {
|
||||
ipv4(a, b, c, d) {
|
||||
@ -37,17 +21,13 @@ fn format_addr(ip: ip_addr) -> str {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: parse_addr
|
||||
#[doc = "
|
||||
Convert a str to `ip_addr`
|
||||
|
||||
Convert a str to <ip_addr>
|
||||
Converts a string of the format `x.x.x.x` into an ip_addr enum.
|
||||
|
||||
Converts a string of the format "x.x.x.x" into an ip_addr enum.
|
||||
|
||||
Failure:
|
||||
|
||||
String must be a valid IPv4 address
|
||||
*/
|
||||
Fails if the string is not a valid IPv4 address
|
||||
"]
|
||||
fn parse_addr(ip: str) -> ip_addr {
|
||||
let parts = vec::map(str::split_char(ip, '.'), {|s|
|
||||
alt uint::from_str(s) {
|
||||
|
@ -1,8 +1,4 @@
|
||||
/*
|
||||
Module: rand
|
||||
|
||||
Random number generation
|
||||
*/
|
||||
#[doc = "Random number generation"]
|
||||
|
||||
enum rctx {}
|
||||
|
||||
@ -13,52 +9,24 @@ native mod rustrt {
|
||||
fn rand_free(c: *rctx);
|
||||
}
|
||||
|
||||
/* Section: Types */
|
||||
|
||||
/*
|
||||
Obj: rng
|
||||
|
||||
A random number generator
|
||||
*/
|
||||
#[doc = "A random number generator"]
|
||||
iface rng {
|
||||
/*
|
||||
Method: next
|
||||
|
||||
Return the next random integer
|
||||
*/
|
||||
#[doc = "Return the next random integer"]
|
||||
fn next() -> u32;
|
||||
|
||||
/*
|
||||
Method: next_float
|
||||
|
||||
Return the next random float
|
||||
*/
|
||||
#[doc = "Return the next random float"]
|
||||
fn next_float() -> float;
|
||||
|
||||
/*
|
||||
Method: gen_str
|
||||
|
||||
Return a random string composed of A-Z, a-z, 0-9.
|
||||
*/
|
||||
#[doc = "Return a random string composed of A-Z, a-z, 0-9."]
|
||||
fn gen_str(len: uint) -> str;
|
||||
|
||||
/*
|
||||
Method: gen_bytes
|
||||
|
||||
Return a random byte string.
|
||||
*/
|
||||
#[doc = "Return a random byte string."]
|
||||
fn gen_bytes(len: uint) -> [u8];
|
||||
}
|
||||
|
||||
resource rand_res(c: *rctx) { rustrt::rand_free(c); }
|
||||
|
||||
/* Section: Operations */
|
||||
|
||||
/*
|
||||
Function: mk_rng
|
||||
|
||||
Create a random number generator
|
||||
*/
|
||||
#[doc = "Create a random number generator"]
|
||||
fn mk_rng() -> rng {
|
||||
impl of rng for @rand_res {
|
||||
fn next() -> u32 { ret rustrt::rand_next(**self); }
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,8 +1,4 @@
|
||||
/*
|
||||
Module: run
|
||||
|
||||
Process spawning
|
||||
*/
|
||||
#[doc ="Process spawning"];
|
||||
import option::{some, none};
|
||||
import str::sbuf;
|
||||
import ctypes::{fd_t, pid_t, void};
|
||||
@ -21,87 +17,51 @@ native mod rustrt {
|
||||
-> pid_t;
|
||||
}
|
||||
|
||||
/* Section: Types */
|
||||
|
||||
/*
|
||||
Iface: program
|
||||
|
||||
A value representing a child process
|
||||
*/
|
||||
#[doc ="A value representing a child process"]
|
||||
iface program {
|
||||
/*
|
||||
Method: get_id
|
||||
|
||||
Returns the process id of the program
|
||||
*/
|
||||
#[doc ="Returns the process id of the program"]
|
||||
fn get_id() -> pid_t;
|
||||
|
||||
/*
|
||||
Method: input
|
||||
|
||||
Returns an io::writer that can be used to write to stdin
|
||||
*/
|
||||
#[doc ="Returns an io::writer that can be used to write to stdin"]
|
||||
fn input() -> io::writer;
|
||||
|
||||
/*
|
||||
Method: output
|
||||
|
||||
Returns an io::reader that can be used to read from stdout
|
||||
*/
|
||||
#[doc ="Returns an io::reader that can be used to read from stdout"]
|
||||
fn output() -> io::reader;
|
||||
|
||||
/*
|
||||
Method: err
|
||||
|
||||
Returns an io::reader that can be used to read from stderr
|
||||
*/
|
||||
#[doc ="Returns an io::reader that can be used to read from stderr"]
|
||||
fn err() -> io::reader;
|
||||
|
||||
/*
|
||||
Method: close_input
|
||||
|
||||
Closes the handle to the child processes standard input
|
||||
*/
|
||||
#[doc = "Closes the handle to the child processes standard input"]
|
||||
fn close_input();
|
||||
|
||||
/*
|
||||
Method: finish
|
||||
|
||||
#[doc = "
|
||||
Waits for the child process to terminate. Closes the handle
|
||||
to stdin if necessary.
|
||||
*/
|
||||
"]
|
||||
fn finish() -> int;
|
||||
|
||||
/*
|
||||
Method: destroy
|
||||
|
||||
Closes open handles
|
||||
*/
|
||||
#[doc ="Closes open handles"]
|
||||
fn destroy();
|
||||
}
|
||||
|
||||
|
||||
/* Section: Operations */
|
||||
|
||||
/*
|
||||
Function: spawn_process
|
||||
|
||||
#[doc = "
|
||||
Run a program, providing stdin, stdout and stderr handles
|
||||
|
||||
Parameters:
|
||||
# Arguments
|
||||
|
||||
prog - The path to an executable
|
||||
args - Vector of arguments to pass to the child process
|
||||
env - optional env-modification for child
|
||||
dir - optional dir to run child in (default current dir)
|
||||
in_fd - A file descriptor for the child to use as std input
|
||||
out_fd - A file descriptor for the child to use as std output
|
||||
err_fd - A file descriptor for the child to use as std error
|
||||
* prog - The path to an executable
|
||||
* args - Vector of arguments to pass to the child process
|
||||
* env - optional env-modification for child
|
||||
* dir - optional dir to run child in (default current dir)
|
||||
* in_fd - A file descriptor for the child to use as std input
|
||||
* out_fd - A file descriptor for the child to use as std output
|
||||
* err_fd - A file descriptor for the child to use as std error
|
||||
|
||||
Returns:
|
||||
# Return value
|
||||
|
||||
The process id of the spawned process
|
||||
*/
|
||||
"]
|
||||
fn spawn_process(prog: str, args: [str],
|
||||
env: option<[(str,str)]>,
|
||||
dir: option<str>,
|
||||
@ -188,43 +148,39 @@ fn with_dirp<T>(d: option<str>,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: run_program
|
||||
|
||||
#[doc ="
|
||||
Spawns a process and waits for it to terminate
|
||||
|
||||
Parameters:
|
||||
# Arguments
|
||||
|
||||
prog - The path to an executable
|
||||
args - Vector of arguments to pass to the child process
|
||||
* prog - The path to an executable
|
||||
* args - Vector of arguments to pass to the child process
|
||||
|
||||
Returns:
|
||||
# Return value
|
||||
|
||||
The process id
|
||||
*/
|
||||
"]
|
||||
fn run_program(prog: str, args: [str]) -> int {
|
||||
ret waitpid(spawn_process(prog, args, none, none,
|
||||
0i32, 0i32, 0i32));
|
||||
}
|
||||
|
||||
/*
|
||||
Function: start_program
|
||||
|
||||
#[doc ="
|
||||
Spawns a process and returns a program
|
||||
|
||||
The returned value is a boxed resource containing a <program> object that can
|
||||
be used for sending and recieving data over the standard file descriptors.
|
||||
The resource will ensure that file descriptors are closed properly.
|
||||
|
||||
Parameters:
|
||||
# Arguments
|
||||
|
||||
prog - The path to an executable
|
||||
args - Vector of arguments to pass to the child process
|
||||
* prog - The path to an executable
|
||||
* args - Vector of arguments to pass to the child process
|
||||
|
||||
Returns:
|
||||
# Return value
|
||||
|
||||
A boxed resource of <program>
|
||||
*/
|
||||
"]
|
||||
fn start_program(prog: str, args: [str]) -> program {
|
||||
let pipe_input = os::pipe();
|
||||
let pipe_output = os::pipe();
|
||||
@ -291,22 +247,20 @@ fn read_all(rd: io::reader) -> str {
|
||||
ret buf;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: program_output
|
||||
|
||||
#[doc ="
|
||||
Spawns a process, waits for it to exit, and returns the exit code, and
|
||||
contents of stdout and stderr.
|
||||
|
||||
Parameters:
|
||||
# Arguments
|
||||
|
||||
prog - The path to an executable
|
||||
args - Vector of arguments to pass to the child process
|
||||
* prog - The path to an executable
|
||||
* args - Vector of arguments to pass to the child process
|
||||
|
||||
Returns:
|
||||
# Return value
|
||||
|
||||
A record, {status: int, out: str, err: str} containing the exit code,
|
||||
the contents of stdout and the contents of stderr.
|
||||
*/
|
||||
"]
|
||||
fn program_output(prog: str, args: [str]) ->
|
||||
{status: int, out: str, err: str} {
|
||||
let pr = start_program(prog, args);
|
||||
@ -316,11 +270,7 @@ fn program_output(prog: str, args: [str]) ->
|
||||
ret {status: pr.finish(), out: out, err: err};
|
||||
}
|
||||
|
||||
/*
|
||||
Function: waitpid
|
||||
|
||||
Waits for a process to exit and returns the exit code
|
||||
*/
|
||||
#[doc ="Waits for a process to exit and returns the exit code"]
|
||||
fn waitpid(pid: pid_t) -> int {
|
||||
ret waitpid_os(pid);
|
||||
|
||||
|
@ -1,8 +1,4 @@
|
||||
/*
|
||||
Module: serialization
|
||||
|
||||
Support code for serialization.
|
||||
*/
|
||||
#[doc = "Support code for serialization."];
|
||||
|
||||
import list::list;
|
||||
import ebml::writer;
|
||||
|
@ -1,18 +1,16 @@
|
||||
/*
|
||||
Module: sha1
|
||||
|
||||
#[doc ="
|
||||
An implementation of the SHA-1 cryptographic hash.
|
||||
|
||||
First create a <sha1> object using the <mk_sha1> constructor, then
|
||||
feed it input using the <input> or <input_str> methods, which may be
|
||||
First create a `sha1` object using the `mk_sha1` constructor, then
|
||||
feed it input using the `input` or `input_str` methods, which may be
|
||||
called any number of times.
|
||||
|
||||
After the entire input has been fed to the hash read the result using
|
||||
the <result> or <result_str> methods.
|
||||
the `result` or `result_str` methods.
|
||||
|
||||
The <sha1> object may be reused to create multiple hashes by calling
|
||||
the <reset> method.
|
||||
*/
|
||||
The `sha1` object may be reused to create multiple hashes by calling
|
||||
the `reset` method.
|
||||
"];
|
||||
|
||||
/*
|
||||
* A SHA-1 implementation derived from Paul E. Jones's reference
|
||||
@ -22,50 +20,26 @@ the <reset> method.
|
||||
export sha1;
|
||||
export mk_sha1;
|
||||
|
||||
/* Section: Types */
|
||||
|
||||
/*
|
||||
Iface: sha1
|
||||
|
||||
The SHA-1 interface
|
||||
*/
|
||||
#[doc = "The SHA-1 interface"]
|
||||
iface sha1 {
|
||||
/*
|
||||
Method: input
|
||||
|
||||
Provide message input as bytes
|
||||
*/
|
||||
#[doc = "Provide message input as bytes"]
|
||||
fn input([u8]);
|
||||
/*
|
||||
Method: input_str
|
||||
|
||||
Provide message input as string
|
||||
*/
|
||||
#[doc = "Provide message input as string"]
|
||||
fn input_str(str);
|
||||
/*
|
||||
Method: result
|
||||
|
||||
#[doc = "
|
||||
Read the digest as a vector of 20 bytes. After calling this no further
|
||||
input may be provided until reset is called.
|
||||
*/
|
||||
"]
|
||||
fn result() -> [u8];
|
||||
/*
|
||||
Method: result_str
|
||||
|
||||
#[doc = "
|
||||
Read the digest as a hex string. After calling this no further
|
||||
input may be provided until reset is called.
|
||||
*/
|
||||
"]
|
||||
fn result_str() -> str;
|
||||
/*
|
||||
Method: reset
|
||||
|
||||
Reset the SHA-1 state for reuse
|
||||
*/
|
||||
#[doc = "Reset the SHA-1 state for reuse"]
|
||||
fn reset();
|
||||
}
|
||||
|
||||
/* Section: Operations */
|
||||
|
||||
// Some unexported constants
|
||||
const digest_buf_len: uint = 5u;
|
||||
const msg_block_len: uint = 64u;
|
||||
@ -76,11 +50,7 @@ const k2: u32 = 0x8F1BBCDCu32;
|
||||
const k3: u32 = 0xCA62C1D6u32;
|
||||
|
||||
|
||||
/*
|
||||
Function: mk_sha1
|
||||
|
||||
Construct a <sha1> object
|
||||
*/
|
||||
#[doc = "Construct a `sha` object"]
|
||||
fn mk_sha1() -> sha1 {
|
||||
type sha1state =
|
||||
{h: [mutable u32],
|
||||
|
@ -1,59 +1,44 @@
|
||||
/*
|
||||
Module: smallintmap
|
||||
|
||||
#[doc = "
|
||||
A simple map based on a vector for small integer keys. Space requirements
|
||||
are O(highest integer key).
|
||||
*/
|
||||
"];
|
||||
import core::option;
|
||||
import core::option::{some, none};
|
||||
|
||||
// FIXME: Should not be @; there's a bug somewhere in rustc that requires this
|
||||
// to be.
|
||||
/*
|
||||
Type: smallintmap
|
||||
*/
|
||||
type smallintmap<T> = @{mutable v: [mutable option<T>]};
|
||||
|
||||
/*
|
||||
Function: mk
|
||||
|
||||
Create a smallintmap
|
||||
*/
|
||||
#[doc = "Create a smallintmap"]
|
||||
fn mk<T>() -> smallintmap<T> {
|
||||
let v: [mutable option<T>] = [mutable];
|
||||
ret @{mutable v: v};
|
||||
}
|
||||
|
||||
/*
|
||||
Function: insert
|
||||
|
||||
#[doc = "
|
||||
Add a value to the map. If the map already contains a value for
|
||||
the specified key then the original value is replaced.
|
||||
*/
|
||||
"]
|
||||
fn insert<T: copy>(m: smallintmap<T>, key: uint, val: T) {
|
||||
vec::grow_set::<option<T>>(m.v, key, none::<T>, some::<T>(val));
|
||||
}
|
||||
|
||||
/*
|
||||
Function: find
|
||||
|
||||
#[doc = "
|
||||
Get the value for the specified key. If the key does not exist
|
||||
in the map then returns none
|
||||
*/
|
||||
"]
|
||||
fn find<T: copy>(m: smallintmap<T>, key: uint) -> option<T> {
|
||||
if key < vec::len::<option<T>>(m.v) { ret m.v[key]; }
|
||||
ret none::<T>;
|
||||
}
|
||||
|
||||
/*
|
||||
Method: get
|
||||
|
||||
#[doc = "
|
||||
Get the value for the specified key
|
||||
|
||||
Failure:
|
||||
# Failure
|
||||
|
||||
If the key does not exist in the map
|
||||
*/
|
||||
"]
|
||||
fn get<T: copy>(m: smallintmap<T>, key: uint) -> T {
|
||||
alt find(m, key) {
|
||||
none { #error("smallintmap::get(): key not present"); fail; }
|
||||
@ -61,11 +46,9 @@ fn get<T: copy>(m: smallintmap<T>, key: uint) -> T {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Method: contains_key
|
||||
|
||||
#[doc = "
|
||||
Returns true if the map contains a value for the specified key
|
||||
*/
|
||||
"]
|
||||
fn contains_key<T: copy>(m: smallintmap<T>, key: uint) -> bool {
|
||||
ret !option::is_none(find::<T>(m, key));
|
||||
}
|
||||
@ -80,11 +63,7 @@ fn max_key<T>(m: smallintmap<T>) -> uint {
|
||||
ret vec::len::<option<T>>(m.v);
|
||||
}
|
||||
|
||||
/*
|
||||
Impl: map
|
||||
|
||||
Implements the map::map interface for smallintmap
|
||||
*/
|
||||
#[doc = "Implements the map::map interface for smallintmap"]
|
||||
impl <V: copy> of map::map<uint, V> for smallintmap<V> {
|
||||
fn size() -> uint {
|
||||
let sz = 0u;
|
||||
@ -136,11 +115,7 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Funtion: as_map
|
||||
|
||||
Cast the given smallintmap to a map::map
|
||||
*/
|
||||
#[doc = "Cast the given smallintmap to a map::map"]
|
||||
fn as_map<V>(s: smallintmap<V>) -> map::map<uint, V> {
|
||||
s as map::map::<uint, V>
|
||||
}
|
||||
|
@ -1,25 +1,18 @@
|
||||
/*
|
||||
Module: sort
|
||||
|
||||
Sorting methods
|
||||
*/
|
||||
#[doc = "Sorting methods"];
|
||||
import vec::len;
|
||||
|
||||
export merge_sort;
|
||||
export quick_sort;
|
||||
export quick_sort3;
|
||||
|
||||
/* Type: le */
|
||||
type le<T> = fn(T, T) -> bool;
|
||||
|
||||
/*
|
||||
Function: merge_sort
|
||||
|
||||
#[doc = "
|
||||
Merge sort. Returns a new vector containing the sorted list.
|
||||
|
||||
Has worst case O(n log n) performance, best case O(n), but
|
||||
is not space efficient. This is a stable sort.
|
||||
*/
|
||||
"]
|
||||
fn merge_sort<T: copy>(le: le<T>, v: [const T]) -> [T] {
|
||||
type slice = (uint, uint);
|
||||
|
||||
@ -88,14 +81,12 @@ fn qsort<T: copy>(compare_func: le<T>, arr: [mutable T], left: uint,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: quick_sort
|
||||
|
||||
#[doc = "
|
||||
Quicksort. Sorts a mutable vector in place.
|
||||
|
||||
Has worst case O(n^2) performance, average case O(n log n).
|
||||
This is an unstable sort.
|
||||
*/
|
||||
"]
|
||||
fn quick_sort<T: copy>(compare_func: le<T>, arr: [mutable T]) {
|
||||
if len::<T>(arr) == 0u { ret; }
|
||||
qsort::<T>(compare_func, arr, 0u, len::<T>(arr) - 1u);
|
||||
@ -150,18 +141,16 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
|
||||
}
|
||||
|
||||
// FIXME: This should take lt and eq types
|
||||
/*
|
||||
Function: quick_sort3
|
||||
|
||||
#[doc = "
|
||||
Fancy quicksort. Sorts a mutable vector in place.
|
||||
|
||||
Based on algorithm presented by Sedgewick and Bentley
|
||||
<http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf>.
|
||||
Based on algorithm presented by [Sedgewick and Bentley]
|
||||
(http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf).
|
||||
According to these slides this is the algorithm of choice for
|
||||
'randomly ordered keys, abstract compare' & 'small number of key values'.
|
||||
|
||||
This is an unstable sort.
|
||||
*/
|
||||
"]
|
||||
fn quick_sort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
|
||||
arr: [mutable T]) {
|
||||
if len::<T>(arr) == 0u { ret; }
|
||||
|
@ -7,6 +7,8 @@
|
||||
#[license = "MIT"];
|
||||
#[crate_type = "lib"];
|
||||
|
||||
#[doc = "The Rust standard library"];
|
||||
|
||||
export fs, io, net, run, uv;
|
||||
export c_vec, four, tri, util;
|
||||
export bitv, deque, fun_treemap, list, map, smallintmap, sort, treemap, ufind;
|
||||
|
@ -1,17 +1,10 @@
|
||||
/*
|
||||
Module: tempfile
|
||||
|
||||
Temporary files and directories
|
||||
*/
|
||||
#[doc = "Temporary files and directories"];
|
||||
|
||||
import core::option;
|
||||
import fs;
|
||||
import option::{none, some};
|
||||
import rand;
|
||||
|
||||
/*
|
||||
Function: mkdtemp
|
||||
*/
|
||||
fn mkdtemp(prefix: str, suffix: str) -> option<str> {
|
||||
let r = rand::mk_rng();
|
||||
let i = 0u;
|
||||
|
@ -1,67 +1,37 @@
|
||||
/*
|
||||
Module: term
|
||||
|
||||
Simple ANSI color library
|
||||
*/
|
||||
#[doc = "Simple ANSI color library"];
|
||||
|
||||
import core::option;
|
||||
|
||||
// TODO: Windows support.
|
||||
|
||||
/* Const: color_black */
|
||||
const color_black: u8 = 0u8;
|
||||
/* Const: color_red */
|
||||
const color_red: u8 = 1u8;
|
||||
/* Const: color_green */
|
||||
const color_green: u8 = 2u8;
|
||||
/* Const: color_yellow */
|
||||
const color_yellow: u8 = 3u8;
|
||||
/* Const: color_blue */
|
||||
const color_blue: u8 = 4u8;
|
||||
/* Const: color_magenta */
|
||||
const color_magenta: u8 = 5u8;
|
||||
/* Const: color_cyan */
|
||||
const color_cyan: u8 = 6u8;
|
||||
/* Const: color_light_gray */
|
||||
const color_light_gray: u8 = 7u8;
|
||||
/* Const: color_light_grey */
|
||||
const color_light_grey: u8 = 7u8;
|
||||
/* Const: color_dark_gray */
|
||||
const color_dark_gray: u8 = 8u8;
|
||||
/* Const: color_dark_grey */
|
||||
const color_dark_grey: u8 = 8u8;
|
||||
/* Const: color_bright_red */
|
||||
const color_bright_red: u8 = 9u8;
|
||||
/* Const: color_bright_green */
|
||||
const color_bright_green: u8 = 10u8;
|
||||
/* Const: color_bright_yellow */
|
||||
const color_bright_yellow: u8 = 11u8;
|
||||
/* Const: color_bright_blue */
|
||||
const color_bright_blue: u8 = 12u8;
|
||||
/* Const: color_bright_magenta */
|
||||
const color_bright_magenta: u8 = 13u8;
|
||||
/* Const: color_bright_cyan */
|
||||
const color_bright_cyan: u8 = 14u8;
|
||||
/* Const: color_bright_white */
|
||||
const color_bright_white: u8 = 15u8;
|
||||
|
||||
fn esc(writer: io::writer) { writer.write([0x1bu8, '[' as u8]); }
|
||||
|
||||
/*
|
||||
Function: reset
|
||||
|
||||
Reset the foreground and background colors to default
|
||||
*/
|
||||
#[doc = "Reset the foreground and background colors to default"]
|
||||
fn reset(writer: io::writer) {
|
||||
esc(writer);
|
||||
writer.write(['0' as u8, 'm' as u8]);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: color_supported
|
||||
|
||||
Returns true if the terminal supports color
|
||||
*/
|
||||
#[doc = "Returns true if the terminal supports color"]
|
||||
fn color_supported() -> bool {
|
||||
let supported_terms = ["xterm-color", "xterm",
|
||||
"screen-bce", "xterm-256color"];
|
||||
@ -84,20 +54,12 @@ fn set_color(writer: io::writer, first_char: u8, color: u8) {
|
||||
writer.write([first_char, ('0' as u8) + color, 'm' as u8]);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: fg
|
||||
|
||||
Set the foreground color
|
||||
*/
|
||||
#[doc = "Set the foreground color"]
|
||||
fn fg(writer: io::writer, color: u8) {
|
||||
ret set_color(writer, '3' as u8, color);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: fg
|
||||
|
||||
Set the background color
|
||||
*/
|
||||
#[doc = "Set the background color"]
|
||||
fn bg(writer: io::writer, color: u8) {
|
||||
ret set_color(writer, '4' as u8, color);
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
#[doc(hidden)];
|
||||
|
||||
// Support code for rustc's built in test runner generator. Currently,
|
||||
// none of this is meant for users. It is intended to support the
|
||||
// simplest interface possible for representing and running tests
|
||||
|
@ -1,26 +1,16 @@
|
||||
/*
|
||||
Module: time
|
||||
*/
|
||||
|
||||
#[abi = "cdecl"]
|
||||
native mod rustrt {
|
||||
fn get_time(&sec: u32, &usec: u32);
|
||||
fn precise_time_ns(&ns: u64);
|
||||
}
|
||||
|
||||
/*
|
||||
Type: timeval
|
||||
|
||||
A record specifying a time value in seconds and microseconds.
|
||||
*/
|
||||
#[doc = "A record specifying a time value in seconds and microseconds."]
|
||||
type timeval = {sec: u32, usec: u32};
|
||||
|
||||
/*
|
||||
Function: get_time
|
||||
|
||||
#[doc = "
|
||||
Returns the current time as a `timeval` containing the seconds and
|
||||
microseconds since 1970-01-01T00:00:00Z.
|
||||
*/
|
||||
"]
|
||||
fn get_time() -> timeval {
|
||||
let sec = 0u32;
|
||||
let usec = 0u32;
|
||||
@ -28,20 +18,16 @@ fn get_time() -> timeval {
|
||||
ret {sec: sec, usec: usec};
|
||||
}
|
||||
|
||||
/*
|
||||
Function: precise_time_ns
|
||||
|
||||
#[doc = "
|
||||
Returns the current value of a high-resolution performance counter
|
||||
in nanoseconds since an unspecified epoch.
|
||||
*/
|
||||
"]
|
||||
fn precise_time_ns() -> u64 { let ns = 0u64; rustrt::precise_time_ns(ns); ns }
|
||||
|
||||
/*
|
||||
Function: precise_time_s
|
||||
|
||||
#[doc = "
|
||||
Returns the current value of a high-resolution performance counter
|
||||
in seconds since an unspecified epoch.
|
||||
*/
|
||||
"]
|
||||
fn precise_time_s() -> float {
|
||||
ret (precise_time_ns() as float) / 1000000000.;
|
||||
}
|
||||
|
@ -1,13 +1,10 @@
|
||||
/*
|
||||
Module: treemap
|
||||
|
||||
#[doc = "
|
||||
A key,value store that works on anything.
|
||||
|
||||
This works using a binary search tree. In the first version, it's a
|
||||
very naive algorithm, but it will probably be updated to be a
|
||||
red-black tree or something else.
|
||||
|
||||
*/
|
||||
"];
|
||||
|
||||
import core::option::{some, none};
|
||||
import option = core::option;
|
||||
@ -18,32 +15,14 @@ export insert;
|
||||
export find;
|
||||
export traverse;
|
||||
|
||||
/* Section: Types */
|
||||
|
||||
/*
|
||||
Type: treemap
|
||||
*/
|
||||
type treemap<K, V> = @mutable tree_node<K, V>;
|
||||
|
||||
/*
|
||||
Tag: tree_node
|
||||
*/
|
||||
enum tree_node<K, V> { empty, node(@K, @V, treemap<K, V>, treemap<K, V>) }
|
||||
|
||||
/* Section: Operations */
|
||||
|
||||
/*
|
||||
Function: init
|
||||
|
||||
Create a treemap
|
||||
*/
|
||||
#[doc = "Create a treemap"]
|
||||
fn init<K, V>() -> treemap<K, V> { @mutable empty }
|
||||
|
||||
/*
|
||||
Function: insert
|
||||
|
||||
Insert a value into the map
|
||||
*/
|
||||
#[doc = "Insert a value into the map"]
|
||||
fn insert<K: copy, V: copy>(m: treemap<K, V>, k: K, v: V) {
|
||||
alt m {
|
||||
@empty { *m = node(@k, @v, @mutable empty, @mutable empty); }
|
||||
@ -62,11 +41,7 @@ fn insert<K: copy, V: copy>(m: treemap<K, V>, k: K, v: V) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: find
|
||||
|
||||
Find a value based on the key
|
||||
*/
|
||||
#[doc = "Find a value based on the key"]
|
||||
fn find<K: copy, V: copy>(m: treemap<K, V>, k: K) -> option<V> {
|
||||
alt *m {
|
||||
empty { none }
|
||||
@ -81,11 +56,7 @@ fn find<K: copy, V: copy>(m: treemap<K, V>, k: K) -> option<V> {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: traverse
|
||||
|
||||
Visit all pairs in the map in order.
|
||||
*/
|
||||
#[doc = "Visit all pairs in the map in order."]
|
||||
fn traverse<K, V>(m: treemap<K, V>, f: fn(K, V)) {
|
||||
alt *m {
|
||||
empty { }
|
||||
|
@ -1,8 +1,6 @@
|
||||
// -*- rust -*-
|
||||
|
||||
/*
|
||||
Module: tri
|
||||
|
||||
#[doc = "
|
||||
ADT for the ternary Kleene logic K3
|
||||
|
||||
This allows reasoning with three logic values (true, false, unknown).
|
||||
@ -10,132 +8,85 @@ This allows reasoning with three logic values (true, false, unknown).
|
||||
Implementation: Truth values are represented using a single u8 and
|
||||
all operations are done using bit operations which is fast
|
||||
on current cpus.
|
||||
*/
|
||||
"];
|
||||
|
||||
export t, true, false, unknown;
|
||||
export not, and, or, xor, implies, eq, ne, is_true, is_false;
|
||||
export from_str, to_str, all_values, to_bit;
|
||||
|
||||
/*
|
||||
Type: t
|
||||
|
||||
The type of ternary logic values
|
||||
*/
|
||||
#[doc = "The type of ternary logic values"]
|
||||
type t = u8;
|
||||
|
||||
const b0: u8 = 1u8;
|
||||
const b1: u8 = 2u8;
|
||||
const b01: u8 = 3u8;
|
||||
|
||||
/*
|
||||
Constant: unknown
|
||||
|
||||
Logic value for unknown (maybe true xor maybe false)
|
||||
*/
|
||||
#[doc = "Logic value for unknown (maybe true xor maybe false)"]
|
||||
const unknown: t = 0u8;
|
||||
|
||||
/*
|
||||
Constant: true
|
||||
|
||||
Logic value for truth
|
||||
*/
|
||||
#[doc = "Logic value for truth"]
|
||||
const true: t = 1u8;
|
||||
|
||||
/*
|
||||
Constant: false
|
||||
|
||||
Logic value for falsehood
|
||||
*/
|
||||
#[doc = "Logic value for falsehood"]
|
||||
const false: t = 2u8;
|
||||
|
||||
/* Function: not
|
||||
|
||||
Negation/Inverse
|
||||
*/
|
||||
#[doc = "Negation/Inverse"]
|
||||
pure fn not(v: t) -> t { ((v << 1u8) | (v >> 1u8)) & b01 }
|
||||
|
||||
/* Function: and
|
||||
|
||||
Conjunction
|
||||
*/
|
||||
#[doc = "Conjunction"]
|
||||
pure fn and(a: t, b: t) -> t { ((a | b) & b1) | ((a & b) & b0) }
|
||||
|
||||
/* Function: or
|
||||
|
||||
Disjunction
|
||||
*/
|
||||
#[doc = "Disjunction"]
|
||||
pure fn or(a: t, b: t) -> t { ((a & b) & b1) | ((a | b) & b0) }
|
||||
|
||||
/*
|
||||
Function: xor
|
||||
|
||||
Exclusive or
|
||||
*/
|
||||
#[doc = "Exclusive or"]
|
||||
pure fn xor(a: t, b: t) -> t {
|
||||
let anb = a & b;
|
||||
let aob = a & not(b);
|
||||
ret ((anb & b1) | (anb << 1u8) | (aob >> 1u8) | (aob & b0)) & b01;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: implies
|
||||
|
||||
Classic implication, i.e. from `a` follows `b`
|
||||
*/
|
||||
#[doc = "Classic implication, i.e. from `a` follows `b`"]
|
||||
pure fn implies(a: t, b: t) -> t {
|
||||
ret ((a & b1) >> 1u8) | (b & b0) | ((a << 1u8) & b & b1);
|
||||
}
|
||||
|
||||
/*
|
||||
Predicate: eq
|
||||
|
||||
Returns:
|
||||
#[doc = "
|
||||
# Return value
|
||||
|
||||
true if truth values `a` and `b` are indistinguishable in the logic
|
||||
*/
|
||||
"]
|
||||
pure fn eq(a: t, b: t) -> bool { a == b }
|
||||
|
||||
/*
|
||||
Predicate: ne
|
||||
|
||||
Returns:
|
||||
#[doc = "
|
||||
# Return value
|
||||
|
||||
true if truth values `a` and `b` are distinguishable in the logic
|
||||
*/
|
||||
"]
|
||||
pure fn ne(a: t, b: t) -> bool { a != b }
|
||||
|
||||
/*
|
||||
Predicate: is_true
|
||||
|
||||
Returns:
|
||||
#[doc = "
|
||||
# Return value
|
||||
|
||||
true if `v` represents truth in the logic
|
||||
*/
|
||||
"]
|
||||
pure fn is_true(v: t) -> bool { v == tri::true }
|
||||
|
||||
/*
|
||||
Predicate: is_false
|
||||
|
||||
Returns:
|
||||
#[doc = "
|
||||
# Return value
|
||||
|
||||
true if `v` represents false in the logic
|
||||
*/
|
||||
"]
|
||||
pure fn is_false(v: t) -> bool { v == tri::false }
|
||||
|
||||
/*
|
||||
Predicate: is_unknown
|
||||
|
||||
Returns:
|
||||
#[doc = "
|
||||
# Return value
|
||||
|
||||
true if `v` represents the unknown state in the logic
|
||||
*/
|
||||
"]
|
||||
pure fn is_unknown(v: t) -> bool { v == unknown }
|
||||
|
||||
/*
|
||||
Function: from_str
|
||||
|
||||
Parse logic value from `s`
|
||||
*/
|
||||
#[doc = "Parse logic value from `s`"]
|
||||
pure fn from_str(s: str) -> t {
|
||||
alt check s {
|
||||
"unknown" { unknown }
|
||||
@ -144,11 +95,7 @@ pure fn from_str(s: str) -> t {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: to_str
|
||||
|
||||
Convert `v` into a string
|
||||
*/
|
||||
#[doc = "Convert `v` into a string"]
|
||||
pure fn to_str(v: t) -> str {
|
||||
// FIXME replace with consts as soon as that works
|
||||
alt check v {
|
||||
@ -158,25 +105,21 @@ pure fn to_str(v: t) -> str {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Function: all_values
|
||||
|
||||
#[doc = "
|
||||
Iterates over all truth values by passing them to `blk`
|
||||
in an unspecified order
|
||||
*/
|
||||
"]
|
||||
fn all_values(blk: fn(v: t)) {
|
||||
blk(tri::false);
|
||||
blk(unknown);
|
||||
blk(tri::true);
|
||||
}
|
||||
|
||||
/*
|
||||
Function: to_bit
|
||||
|
||||
Returns:
|
||||
#[doc = "
|
||||
# Return value
|
||||
|
||||
An u8 whose first bit is set if `if_true(v)` holds
|
||||
*/
|
||||
"]
|
||||
fn to_bit(v: t) -> u8 { v & b0 }
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -1,26 +1,11 @@
|
||||
/*
|
||||
Module: util
|
||||
*/
|
||||
|
||||
/*
|
||||
Function: id
|
||||
|
||||
The identity function
|
||||
*/
|
||||
#[doc = "The identity function"]
|
||||
pure fn id<T: copy>(x: T) -> T { x }
|
||||
|
||||
/* FIXME (issue #141): See test/run-pass/constrained-type.rs. Uncomment
|
||||
* the constraint once fixed. */
|
||||
/*
|
||||
Function: rational
|
||||
|
||||
A rational number
|
||||
*/
|
||||
#[doc = "A rational number"]
|
||||
type rational = {num: int, den: int}; // : int::positive(*.den);
|
||||
|
||||
/*
|
||||
Function: rational_leq
|
||||
*/
|
||||
pure fn rational_leq(x: rational, y: rational) -> bool {
|
||||
// NB: Uses the fact that rationals have positive denominators WLOG:
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user