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