Skip to content

Commit 33c84a6

Browse files
committed
---
yaml --- r: 44368 b: refs/heads/master c: 48b2141 h: refs/heads/master v: v3
1 parent 57126dd commit 33c84a6

26 files changed

+1078
-190
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 4fb4a4b66d5c988d79f77b081dabd8f62b880dfe
2+
refs/heads/master: 48b2141b83bd3afe6aee3adf170ef6e985ab1353
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d9689399d091c3265f00434a69c551a61c28dc
55
refs/heads/try: ef355f6332f83371e4acf04fc4eb940ab41d78d3

trunk/src/libcore/core.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub use vec::{OwnedVector, OwnedCopyableVector};
199199
pub use iter::{BaseIter, ExtendedIter, EqIter, CopyableIter};
200200
pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times};
201201

202-
pub use num::Num;
202+
pub use num::{Num, NumCast};
203203
pub use ptr::Ptr;
204204
pub use to_str::ToStr;
205205
pub use clone::Clone;

trunk/src/libcore/hashmap.rs

Lines changed: 33 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,19 @@ pub mod linear {
108108
}
109109

110110
#[inline(always)]
111-
pure fn bucket_for_key(&self, k: &K) -> SearchResult {
111+
pure fn bucket_for_key(&self, buckets: &[Option<Bucket<K, V>>],
112+
k: &K) -> SearchResult {
112113
let hash = k.hash_keyed(self.k0, self.k1) as uint;
113-
self.bucket_for_key_with_hash(hash, k)
114+
self.bucket_for_key_with_hash(buckets, hash, k)
114115
}
115116

116117
#[inline(always)]
117118
pure fn bucket_for_key_with_hash(&self,
119+
buckets: &[Option<Bucket<K, V>>],
118120
hash: uint,
119121
k: &K) -> SearchResult {
120122
let _ = for self.bucket_sequence(hash) |i| {
121-
match self.buckets[i] {
123+
match buckets[i] {
122124
Some(ref bkt) => if bkt.hash == hash && *k == bkt.key {
123125
return FoundEntry(i);
124126
},
@@ -155,19 +157,11 @@ pub mod linear {
155157
}
156158
}
157159

158-
#[inline(always)]
159-
pure fn value_for_bucket(&self, idx: uint) -> &self/V {
160-
match self.buckets[idx] {
161-
Some(ref bkt) => &bkt.value,
162-
None => die!(~"LinearMap::find: internal logic error"),
163-
}
164-
}
165-
166160
/// Inserts the key value pair into the buckets.
167161
/// Assumes that there will be a bucket.
168162
/// True if there was no previous entry with that key
169163
fn insert_internal(&mut self, hash: uint, k: K, v: V) -> bool {
170-
match self.bucket_for_key_with_hash(hash, &k) {
164+
match self.bucket_for_key_with_hash(self.buckets, hash, &k) {
171165
TableFull => { die!(~"Internal logic error"); }
172166
FoundHole(idx) => {
173167
debug!("insert fresh (%?->%?) at idx %?, hash %?",
@@ -202,7 +196,8 @@ pub mod linear {
202196
//
203197
// I found this explanation elucidating:
204198
// http://www.maths.lse.ac.uk/Courses/MA407/del-hash.pdf
205-
let mut idx = match self.bucket_for_key_with_hash(hash, k) {
199+
let mut idx = match self.bucket_for_key_with_hash(self.buckets,
200+
hash, k) {
206201
TableFull | FoundHole(_) => return None,
207202
FoundEntry(idx) => idx
208203
};
@@ -278,7 +273,7 @@ pub mod linear {
278273
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Map<K, V> {
279274
/// Return true if the map contains a value for the specified key
280275
pure fn contains_key(&self, k: &K) -> bool {
281-
match self.bucket_for_key(k) {
276+
match self.bucket_for_key(self.buckets, k) {
282277
FoundEntry(_) => {true}
283278
TableFull | FoundHole(_) => {false}
284279
}
@@ -296,9 +291,20 @@ pub mod linear {
296291
297292
/// Return the value corresponding to the key in the map
298293
pure fn find(&self, k: &K) -> Option<&self/V> {
299-
match self.bucket_for_key(k) {
300-
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
301-
TableFull | FoundHole(_) => None,
294+
match self.bucket_for_key(self.buckets, k) {
295+
FoundEntry(idx) => {
296+
match self.buckets[idx] {
297+
Some(ref bkt) => {
298+
Some(&bkt.value)
299+
}
300+
None => {
301+
die!(~"LinearMap::find: internal logic error")
302+
}
303+
}
304+
}
305+
TableFull | FoundHole(_) => {
306+
None
307+
}
302308
}
303309
}
304310
@@ -358,63 +364,6 @@ pub mod linear {
358364
old_value
359365
}
360366
361-
/// Return the value corresponding to the key in the map, or insert
362-
/// and return the value if it doesn't exist.
363-
fn find_or_insert(&mut self, k: K, v: V) -> &self/V {
364-
if self.size >= self.resize_at {
365-
// n.b.: We could also do this after searching, so
366-
// that we do not resize if this call to insert is
367-
// simply going to update a key in place. My sense
368-
// though is that it's worse to have to search through
369-
// buckets to find the right spot twice than to just
370-
// resize in this corner case.
371-
self.expand();
372-
}
373-
374-
let hash = k.hash_keyed(self.k0, self.k1) as uint;
375-
let idx = match self.bucket_for_key_with_hash(hash, &k) {
376-
TableFull => die!(~"Internal logic error"),
377-
FoundEntry(idx) => idx,
378-
FoundHole(idx) => {
379-
self.buckets[idx] = Some(Bucket{hash: hash, key: k,
380-
value: v});
381-
self.size += 1;
382-
idx
383-
},
384-
};
385-
386-
self.value_for_bucket(idx)
387-
}
388-
389-
/// Return the value corresponding to the key in the map, or create,
390-
/// insert, and return a new value if it doesn't exist.
391-
fn find_or_insert_with(&mut self, k: K, f: fn(&K) -> V) -> &self/V {
392-
if self.size >= self.resize_at {
393-
// n.b.: We could also do this after searching, so
394-
// that we do not resize if this call to insert is
395-
// simply going to update a key in place. My sense
396-
// though is that it's worse to have to search through
397-
// buckets to find the right spot twice than to just
398-
// resize in this corner case.
399-
self.expand();
400-
}
401-
402-
let hash = k.hash_keyed(self.k0, self.k1) as uint;
403-
let idx = match self.bucket_for_key_with_hash(hash, &k) {
404-
TableFull => die!(~"Internal logic error"),
405-
FoundEntry(idx) => idx,
406-
FoundHole(idx) => {
407-
let v = f(&k);
408-
self.buckets[idx] = Some(Bucket{hash: hash, key: k,
409-
value: v});
410-
self.size += 1;
411-
idx
412-
},
413-
};
414-
415-
self.value_for_bucket(idx)
416-
}
417-
418367
fn consume(&mut self, f: fn(K, V)) {
419368
let mut buckets = ~[];
420369
self.buckets <-> buckets;
@@ -572,7 +521,7 @@ mod test_map {
572521
use uint;
573522

574523
#[test]
575-
pub fn test_insert() {
524+
pub fn inserts() {
576525
let mut m = LinearMap::new();
577526
assert m.insert(1, 2);
578527
assert m.insert(2, 4);
@@ -581,7 +530,7 @@ mod test_map {
581530
}
582531

583532
#[test]
584-
pub fn test_insert_overwrite() {
533+
pub fn overwrite() {
585534
let mut m = LinearMap::new();
586535
assert m.insert(1, 2);
587536
assert *m.get(&1) == 2;
@@ -590,7 +539,7 @@ mod test_map {
590539
}
591540

592541
#[test]
593-
pub fn test_insert_conflicts() {
542+
pub fn conflicts() {
594543
let mut m = linear::linear_map_with_capacity(4);
595544
assert m.insert(1, 2);
596545
assert m.insert(5, 3);
@@ -601,7 +550,7 @@ mod test_map {
601550
}
602551

603552
#[test]
604-
pub fn test_conflict_remove() {
553+
pub fn conflict_remove() {
605554
let mut m = linear::linear_map_with_capacity(4);
606555
assert m.insert(1, 2);
607556
assert m.insert(5, 3);
@@ -612,7 +561,7 @@ mod test_map {
612561
}
613562

614563
#[test]
615-
pub fn test_is_empty() {
564+
pub fn empty() {
616565
let mut m = linear::linear_map_with_capacity(4);
617566
assert m.insert(1, 2);
618567
assert !m.is_empty();
@@ -621,37 +570,23 @@ mod test_map {
621570
}
622571

623572
#[test]
624-
pub fn test_pop() {
573+
pub fn pops() {
625574
let mut m = LinearMap::new();
626575
m.insert(1, 2);
627576
assert m.pop(&1) == Some(2);
628577
assert m.pop(&1) == None;
629578
}
630579

631580
#[test]
632-
pub fn test_swap() {
581+
pub fn swaps() {
633582
let mut m = LinearMap::new();
634583
assert m.swap(1, 2) == None;
635584
assert m.swap(1, 3) == Some(2);
636585
assert m.swap(1, 4) == Some(3);
637586
}
638587

639588
#[test]
640-
pub fn test_find_or_insert() {
641-
let mut m = LinearMap::new::<int, int>();
642-
assert m.find_or_insert(1, 2) == &2;
643-
assert m.find_or_insert(1, 3) == &2;
644-
}
645-
646-
#[test]
647-
pub fn test_find_or_insert_with() {
648-
let mut m = LinearMap::new::<int, int>();
649-
assert m.find_or_insert_with(1, |_| 2) == &2;
650-
assert m.find_or_insert_with(1, |_| 3) == &2;
651-
}
652-
653-
#[test]
654-
pub fn test_consume() {
589+
pub fn consumes() {
655590
let mut m = LinearMap::new();
656591
assert m.insert(1, 2);
657592
assert m.insert(2, 3);
@@ -666,7 +601,7 @@ mod test_map {
666601
}
667602

668603
#[test]
669-
pub fn test_iterate() {
604+
pub fn iterate() {
670605
let mut m = linear::linear_map_with_capacity(4);
671606
for uint::range(0, 32) |i| {
672607
assert m.insert(i, i*2);
@@ -680,7 +615,7 @@ mod test_map {
680615
}
681616

682617
#[test]
683-
pub fn test_find() {
618+
pub fn find() {
684619
let mut m = LinearMap::new();
685620
assert m.find(&1).is_none();
686621
m.insert(1, 2);

trunk/src/libcore/num/f32.rs

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use cmath;
1414
use cmp;
1515
use libc::{c_float, c_int};
1616
use num;
17+
use num::NumCast;
1718
use option::Option;
1819
use from_str;
1920
use to_str;
@@ -283,11 +284,6 @@ impl f32: num::Num {
283284
pure fn modulo(&self, other: &f32) -> f32 { return *self % *other; }
284285
#[inline(always)]
285286
pure fn neg(&self) -> f32 { return -*self; }
286-
287-
#[inline(always)]
288-
pure fn to_int(&self) -> int { return *self as int; }
289-
#[inline(always)]
290-
static pure fn from_int(n: int) -> f32 { return n as f32; }
291287
}
292288

293289
impl f32: num::Zero {
@@ -300,6 +296,30 @@ impl f32: num::One {
300296
static pure fn one() -> f32 { 1.0 }
301297
}
302298

299+
pub impl f32: NumCast {
300+
/**
301+
* Cast `n` to an `f32`
302+
*/
303+
#[inline(always)]
304+
static pure fn from<N:NumCast>(n: N) -> f32 { n.to_f32() }
305+
306+
#[inline(always)] pure fn to_u8(&self) -> u8 { *self as u8 }
307+
#[inline(always)] pure fn to_u16(&self) -> u16 { *self as u16 }
308+
#[inline(always)] pure fn to_u32(&self) -> u32 { *self as u32 }
309+
#[inline(always)] pure fn to_u64(&self) -> u64 { *self as u64 }
310+
#[inline(always)] pure fn to_uint(&self) -> uint { *self as uint }
311+
312+
#[inline(always)] pure fn to_i8(&self) -> i8 { *self as i8 }
313+
#[inline(always)] pure fn to_i16(&self) -> i16 { *self as i16 }
314+
#[inline(always)] pure fn to_i32(&self) -> i32 { *self as i32 }
315+
#[inline(always)] pure fn to_i64(&self) -> i64 { *self as i64 }
316+
#[inline(always)] pure fn to_int(&self) -> int { *self as int }
317+
318+
#[inline(always)] pure fn to_f32(&self) -> f32 { *self }
319+
#[inline(always)] pure fn to_f64(&self) -> f64 { *self as f64 }
320+
#[inline(always)] pure fn to_float(&self) -> float { *self as float }
321+
}
322+
303323
#[abi="rust-intrinsic"]
304324
pub extern {
305325
fn floorf32(val: f32) -> f32;
@@ -545,6 +565,63 @@ impl f32: num::FromStrRadix {
545565
}
546566
}
547567

568+
#[test]
569+
pub fn test_num() {
570+
let ten: f32 = num::cast(10);
571+
let two: f32 = num::cast(2);
572+
573+
assert (ten.add(&two) == num::cast(12));
574+
assert (ten.sub(&two) == num::cast(8));
575+
assert (ten.mul(&two) == num::cast(20));
576+
assert (ten.div(&two) == num::cast(5));
577+
assert (ten.modulo(&two) == num::cast(0));
578+
}
579+
580+
#[test]
581+
fn test_numcast() {
582+
assert (20u == 20f32.to_uint());
583+
assert (20u8 == 20f32.to_u8());
584+
assert (20u16 == 20f32.to_u16());
585+
assert (20u32 == 20f32.to_u32());
586+
assert (20u64 == 20f32.to_u64());
587+
assert (20i == 20f32.to_int());
588+
assert (20i8 == 20f32.to_i8());
589+
assert (20i16 == 20f32.to_i16());
590+
assert (20i32 == 20f32.to_i32());
591+
assert (20i64 == 20f32.to_i64());
592+
assert (20f == 20f32.to_float());
593+
assert (20f32 == 20f32.to_f32());
594+
assert (20f64 == 20f32.to_f64());
595+
596+
assert (20f32 == NumCast::from(20u));
597+
assert (20f32 == NumCast::from(20u8));
598+
assert (20f32 == NumCast::from(20u16));
599+
assert (20f32 == NumCast::from(20u32));
600+
assert (20f32 == NumCast::from(20u64));
601+
assert (20f32 == NumCast::from(20i));
602+
assert (20f32 == NumCast::from(20i8));
603+
assert (20f32 == NumCast::from(20i16));
604+
assert (20f32 == NumCast::from(20i32));
605+
assert (20f32 == NumCast::from(20i64));
606+
assert (20f32 == NumCast::from(20f));
607+
assert (20f32 == NumCast::from(20f32));
608+
assert (20f32 == NumCast::from(20f64));
609+
610+
assert (20f32 == num::cast(20u));
611+
assert (20f32 == num::cast(20u8));
612+
assert (20f32 == num::cast(20u16));
613+
assert (20f32 == num::cast(20u32));
614+
assert (20f32 == num::cast(20u64));
615+
assert (20f32 == num::cast(20i));
616+
assert (20f32 == num::cast(20i8));
617+
assert (20f32 == num::cast(20i16));
618+
assert (20f32 == num::cast(20i32));
619+
assert (20f32 == num::cast(20i64));
620+
assert (20f32 == num::cast(20f));
621+
assert (20f32 == num::cast(20f32));
622+
assert (20f32 == num::cast(20f64));
623+
}
624+
548625
//
549626
// Local Variables:
550627
// mode: rust

0 commit comments

Comments
 (0)