2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-19 15:59:44 -05:00
|
|
|
// xfail-fast
|
2012-09-18 17:52:21 -05:00
|
|
|
#[legacy_modes];
|
|
|
|
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2012-09-05 14:32:05 -05:00
|
|
|
use std::map::*;
|
2012-05-07 16:42:28 -05:00
|
|
|
|
|
|
|
enum cat_type { tuxedo, tabby, tortoiseshell }
|
|
|
|
|
2012-08-27 18:26:35 -05:00
|
|
|
impl cat_type : cmp::Eq {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &cat_type) -> bool {
|
|
|
|
((*self) as uint) == ((*other) as uint)
|
2012-08-27 18:26:35 -05:00
|
|
|
}
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn ne(&self, other: &cat_type) -> bool { !(*self).eq(other) }
|
2012-08-27 18:26:35 -05:00
|
|
|
}
|
|
|
|
|
2012-05-07 16:42:28 -05:00
|
|
|
// 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-09-07 21:04:40 -05:00
|
|
|
struct cat<T: Copy> {
|
2012-09-10 20:56:07 -05:00
|
|
|
// Yes, you can have negative meows
|
|
|
|
priv mut meows : int,
|
2012-05-07 16:42:28 -05:00
|
|
|
|
2012-09-06 21:40:15 -05:00
|
|
|
mut how_hungry : int,
|
|
|
|
name : T,
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
2012-05-07 16:42:28 -05:00
|
|
|
|
2012-09-07 21:04:40 -05:00
|
|
|
impl<T: Copy> cat<T> {
|
2012-05-07 16:42:28 -05:00
|
|
|
fn speak() { self.meow(); }
|
|
|
|
|
|
|
|
fn eat() -> bool {
|
|
|
|
if self.how_hungry > 0 {
|
2012-08-22 19:24:52 -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-08-22 19:24:52 -05:00
|
|
|
error!("Not hungry!");
|
2012-08-01 19:30:05 -05:00
|
|
|
return false;
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
2012-05-07 16:42:28 -05:00
|
|
|
|
2012-09-10 17:38:28 -05:00
|
|
|
impl<T: Copy> cat<T> : Map<int, T> {
|
2012-08-27 18:26:35 -05:00
|
|
|
pure 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-11-17 10:41:47 -06:00
|
|
|
pure fn contains_key(+k: int) -> bool { k <= self.meows }
|
|
|
|
pure fn contains_key_ref(k: &int) -> bool { self.contains_key(*k) }
|
2012-08-02 17:42:56 -05:00
|
|
|
|
2012-11-17 10:41:47 -06:00
|
|
|
pure fn get(+k:int) -> T { match self.find(k) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(v) => { v }
|
|
|
|
None => { fail ~"epic fail"; }
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
pure fn find(+k:int) -> Option<T> { if k <= self.meows {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(self.name)
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
else { None }
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
2012-06-04 13:21:11 -05:00
|
|
|
|
2012-11-25 16:04:45 -06:00
|
|
|
fn update_with_key(+key: int, +val: T, ff: fn(+k: int, +v0: T, +v1: T) -> T) -> bool {
|
2012-11-11 03:01:37 -06:00
|
|
|
match self.find(key) {
|
|
|
|
None => return self.insert(key, val),
|
|
|
|
Some(copy orig) => return self.insert(key, ff(key, orig, val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-25 16:04:45 -06:00
|
|
|
fn update(+key: int, +val: T, ff: fn(+v0: T, +v1: T) -> T) -> bool {
|
2012-11-21 01:33:31 -06:00
|
|
|
match self.find(key) {
|
|
|
|
None => return self.insert(key, val),
|
|
|
|
Some(copy orig) => return self.insert(key, ff(orig, val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-11 03:01:37 -06:00
|
|
|
|
2012-08-21 19:02:40 -05:00
|
|
|
fn remove(+k:int) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.find(k) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(x) => {
|
2012-08-21 19:02:40 -05:00
|
|
|
self.meows -= k; true
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => { false }
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-23 06:39:27 -05:00
|
|
|
pure fn each(f: fn(+v: int, +v: 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-09-23 06:39:27 -05:00
|
|
|
pure fn each_key(&&f: fn(+v: int) -> bool) {
|
2012-09-07 17:32:04 -05:00
|
|
|
for self.each |k, _v| { if !f(k) { break; } loop;};
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
2012-09-23 06:39:27 -05:00
|
|
|
pure fn each_value(&&f: fn(+v: T) -> bool) {
|
2012-09-07 17:32:04 -05:00
|
|
|
for self.each |_k, v| { if !f(v) { break; } loop;};
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
2012-08-02 17:42:56 -05:00
|
|
|
|
2012-08-27 18:26:35 -05:00
|
|
|
pure fn each_ref(f: fn(k: &int, v: &T) -> bool) {}
|
|
|
|
pure fn each_key_ref(f: fn(k: &int) -> bool) {}
|
|
|
|
pure fn each_value_ref(f: fn(k: &T) -> bool) {}
|
2012-08-02 17:42:56 -05:00
|
|
|
|
2012-07-09 16:06:29 -05:00
|
|
|
fn clear() { }
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
|
2012-09-07 21:04:40 -05:00
|
|
|
priv impl<T: Copy> cat<T> {
|
|
|
|
fn meow() {
|
|
|
|
self.meows += 1;
|
|
|
|
error!("Meow %d", self.meows);
|
|
|
|
if self.meows % 5 == 0 {
|
|
|
|
self.how_hungry += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
fn cat<T: Copy>(in_x : int, in_y : int, in_name: T) -> cat<T> {
|
2012-09-05 17:58:43 -05:00
|
|
|
cat {
|
|
|
|
meows: in_x,
|
|
|
|
how_hungry: in_y,
|
|
|
|
name: in_name
|
|
|
|
}
|
|
|
|
}
|
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-08-20 14:23:37 -05:00
|
|
|
assert(nyan.find(1) == Some(~"nyan"));
|
|
|
|
assert(nyan.find(10) == None);
|
2012-05-07 16:42:28 -05:00
|
|
|
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);
|
|
|
|
}
|