2012-05-07 16:42:28 -05:00
|
|
|
use std;
|
|
|
|
import std::map::*;
|
|
|
|
|
|
|
|
enum cat_type { tuxedo, tabby, tortoiseshell }
|
|
|
|
|
|
|
|
// Very silly -- this just returns the value of the name field
|
|
|
|
// for any int value that's less than the meows field
|
|
|
|
|
2012-07-31 12:27:51 -05:00
|
|
|
// ok: T should be in scope when resolving the trait ref for map
|
2012-08-15 20:46:55 -05:00
|
|
|
struct cat<T: copy> : map<int, T> {
|
2012-05-07 16:42:28 -05:00
|
|
|
priv {
|
|
|
|
// Yes, you can have negative meows
|
|
|
|
let mut meows : int;
|
|
|
|
fn meow() {
|
|
|
|
self.meows += 1;
|
2012-07-30 18:01:07 -05:00
|
|
|
error!{"Meow %d", self.meows};
|
2012-05-07 16:42:28 -05:00
|
|
|
if self.meows % 5 == 0 {
|
|
|
|
self.how_hungry += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut how_hungry : int;
|
|
|
|
let name : T;
|
|
|
|
|
|
|
|
new(in_x : int, in_y : int, in_name: T)
|
|
|
|
{ self.meows = in_x; self.how_hungry = in_y; self.name = in_name; }
|
|
|
|
|
|
|
|
fn speak() { self.meow(); }
|
|
|
|
|
|
|
|
fn eat() -> bool {
|
|
|
|
if self.how_hungry > 0 {
|
2012-07-30 18:01:07 -05:00
|
|
|
error!{"OM NOM NOM"};
|
2012-05-07 16:42:28 -05:00
|
|
|
self.how_hungry -= 2;
|
2012-08-01 19:30:05 -05:00
|
|
|
return true;
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-07-30 18:01:07 -05:00
|
|
|
error!{"Not hungry!"};
|
2012-08-01 19:30:05 -05:00
|
|
|
return false;
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size() -> uint { self.meows as uint }
|
2012-06-08 16:35:11 -05:00
|
|
|
fn insert(+k: int, +_v: T) -> bool {
|
2012-05-07 16:42:28 -05:00
|
|
|
self.meows += k;
|
|
|
|
true
|
|
|
|
}
|
2012-08-02 17:42:56 -05:00
|
|
|
fn contains_key(+k: int) -> bool { k <= self.meows }
|
|
|
|
fn contains_key_ref(k: &int) -> bool { self.contains_key(*k) }
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
fn get(+k:int) -> T { match self.find(k) {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(v) => { v }
|
|
|
|
none => { fail ~"epic fail"; }
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-02 17:42:56 -05:00
|
|
|
fn find(+k:int) -> option<T> { if k <= self.meows {
|
2012-05-07 16:42:28 -05:00
|
|
|
some(self.name)
|
|
|
|
}
|
|
|
|
else { none }
|
|
|
|
}
|
2012-06-04 13:21:11 -05:00
|
|
|
|
2012-08-02 17:42:56 -05:00
|
|
|
fn remove(+k:int) -> option<T> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.find(k) {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(x) => {
|
2012-05-07 16:42:28 -05:00
|
|
|
self.meows -= k; some(x)
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
none => { none }
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 17:42:56 -05:00
|
|
|
fn each(f: fn(+int, +T) -> bool) {
|
2012-05-07 16:42:28 -05:00
|
|
|
let mut n = int::abs(self.meows);
|
|
|
|
while n > 0 {
|
|
|
|
if !f(n, self.name) { break; }
|
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 13:21:11 -05:00
|
|
|
|
2012-08-02 17:42:56 -05:00
|
|
|
fn each_key(&&f: fn(+int) -> bool) {
|
2012-07-09 16:37:48 -05:00
|
|
|
for self.each |k, _v| { if !f(k) { break; } again;};
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
2012-08-02 17:42:56 -05:00
|
|
|
fn each_value(&&f: fn(+T) -> bool) {
|
2012-07-09 16:37:48 -05:00
|
|
|
for self.each |_k, v| { if !f(v) { break; } again;};
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
2012-08-02 17:42:56 -05:00
|
|
|
|
|
|
|
fn each_ref(f: fn(k: &int, v: &T) -> bool) {}
|
|
|
|
fn each_key_ref(f: fn(k: &int) -> bool) {}
|
|
|
|
fn each_value_ref(f: fn(k: &T) -> bool) {}
|
|
|
|
|
2012-07-09 16:06:29 -05:00
|
|
|
fn clear() { }
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let nyan : cat<~str> = cat(0, 2, ~"nyan");
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(1u, 5u) |_i| { nyan.speak(); }
|
2012-07-14 00:57:48 -05:00
|
|
|
assert(nyan.find(1) == some(~"nyan"));
|
2012-05-07 16:42:28 -05:00
|
|
|
assert(nyan.find(10) == none);
|
|
|
|
let spotty : cat<cat_type> = cat(2, 57, tuxedo);
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, 6u) |_i| { spotty.speak(); }
|
2012-05-07 16:42:28 -05:00
|
|
|
assert(spotty.size() == 8u);
|
|
|
|
assert(spotty.contains_key(2));
|
|
|
|
assert(spotty.get(3) == tuxedo);
|
|
|
|
}
|