Skip to content

Commit d967ab3

Browse files
committed
---
yaml --- r: 138740 b: refs/heads/try2 c: ce23c8c h: refs/heads/master v: v3
1 parent dcbe6b8 commit d967ab3

File tree

29 files changed

+308
-339
lines changed

29 files changed

+308
-339
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 165cc9e2c424b674a9e6fc88cc7c86ebc7d55b98
8+
refs/heads/try2: ce23c8c0cfc6aee3eae7d0287651ef7bcd9c6b9f
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcore/io.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ pub trait ReaderUtil {
144144
/// Read a big-endian i16 (2 bytes).
145145
fn read_be_i16(&self) -> i16;
146146

147+
/// Read a big-endian IEEE754 double-precision floating-point (8 bytes).
148+
fn read_be_f64(&self) -> f64;
149+
150+
/// Read a big-endian IEEE754 single-precision floating-point (4 bytes).
151+
fn read_be_f32(&self) -> f32;
152+
147153
/// Read a little-endian u64 (8 bytes).
148154
fn read_le_u64(&self) -> u64;
149155

@@ -162,6 +168,14 @@ pub trait ReaderUtil {
162168
/// Read a litle-endian i16 (2 bytes).
163169
fn read_le_i16(&self) -> i16;
164170

171+
/// Read a litten-endian IEEE754 double-precision floating-point
172+
/// (8 bytes).
173+
fn read_le_f64(&self) -> f64;
174+
175+
/// Read a litten-endian IEEE754 single-precision floating-point
176+
/// (4 bytes).
177+
fn read_le_f32(&self) -> f32;
178+
165179
/// Read a u8 (1 byte).
166180
fn read_u8(&self) -> u8;
167181

@@ -368,6 +382,18 @@ impl<T:Reader> ReaderUtil for T {
368382
self.read_be_int_n(2) as i16
369383
}
370384

385+
fn read_be_f64(&self) -> f64 {
386+
unsafe {
387+
cast::transmute::<u64, f64>(self.read_be_u64())
388+
}
389+
}
390+
391+
fn read_be_f32(&self) -> f32 {
392+
unsafe {
393+
cast::transmute::<u32, f32>(self.read_be_u32())
394+
}
395+
}
396+
371397
fn read_le_u64(&self) -> u64 {
372398
self.read_le_uint_n(8) as u64
373399
}
@@ -392,6 +418,18 @@ impl<T:Reader> ReaderUtil for T {
392418
self.read_le_int_n(2) as i16
393419
}
394420

421+
fn read_le_f64(&self) -> f64 {
422+
unsafe {
423+
cast::transmute::<u64, f64>(self.read_le_u64())
424+
}
425+
}
426+
427+
fn read_le_f32(&self) -> f32 {
428+
unsafe {
429+
cast::transmute::<u32, f32>(self.read_le_u32())
430+
}
431+
}
432+
395433
fn read_u8(&self) -> u8 {
396434
self.read_byte() as u8
397435
}
@@ -874,6 +912,12 @@ pub trait WriterUtil {
874912
/// Write a big-endian i16 (2 bytes).
875913
fn write_be_i16(&self, n: i16);
876914

915+
/// Write a big-endian IEEE754 double-precision floating-point (8 bytes).
916+
fn write_be_f64(&self, f: f64);
917+
918+
/// Write a big-endian IEEE754 single-precision floating-point (4 bytes).
919+
fn write_be_f32(&self, f: f32);
920+
877921
/// Write a little-endian u64 (8 bytes).
878922
fn write_le_u64(&self, n: u64);
879923

@@ -892,6 +936,14 @@ pub trait WriterUtil {
892936
/// Write a little-endian i16 (2 bytes).
893937
fn write_le_i16(&self, n: i16);
894938

939+
/// Write a little-endian IEEE754 double-precision floating-point
940+
/// (8 bytes).
941+
fn write_le_f64(&self, f: f64);
942+
943+
/// Write a litten-endian IEEE754 single-precision floating-point
944+
/// (4 bytes).
945+
fn write_le_f32(&self, f: f32);
946+
895947
/// Write a u8 (1 byte).
896948
fn write_u8(&self, n: u8);
897949

@@ -948,6 +1000,16 @@ impl<T:Writer> WriterUtil for T {
9481000
fn write_be_i16(&self, n: i16) {
9491001
u64_to_be_bytes(n as u64, 2u, |v| self.write(v))
9501002
}
1003+
fn write_be_f64(&self, f:f64) {
1004+
unsafe {
1005+
self.write_be_u64(cast::transmute(f))
1006+
}
1007+
}
1008+
fn write_be_f32(&self, f:f32) {
1009+
unsafe {
1010+
self.write_be_u32(cast::transmute(f))
1011+
}
1012+
}
9511013
fn write_le_u64(&self, n: u64) {
9521014
u64_to_le_bytes(n, 8u, |v| self.write(v))
9531015
}
@@ -966,9 +1028,20 @@ impl<T:Writer> WriterUtil for T {
9661028
fn write_le_i16(&self, n: i16) {
9671029
u64_to_le_bytes(n as u64, 2u, |v| self.write(v))
9681030
}
1031+
fn write_le_f64(&self, f:f64) {
1032+
unsafe {
1033+
self.write_le_u64(cast::transmute(f))
1034+
}
1035+
}
1036+
fn write_le_f32(&self, f:f32) {
1037+
unsafe {
1038+
self.write_le_u32(cast::transmute(f))
1039+
}
1040+
}
9691041

9701042
fn write_u8(&self, n: u8) { self.write([n]) }
9711043
fn write_i8(&self, n: i8) { self.write([n as u8]) }
1044+
9721045
}
9731046

9741047
#[allow(non_implicitly_copyable_typarams)]
@@ -1421,6 +1494,41 @@ mod tests {
14211494
}
14221495
}
14231496

1497+
#[test]
1498+
fn test_read_f32() {
1499+
let path = Path("tmp/lib-io-test-read-f32.tmp");
1500+
//big-endian floating-point 8.1250
1501+
let buf = ~[0x41, 0x02, 0x00, 0x00];
1502+
1503+
{
1504+
let file = io::file_writer(&path, [io::Create]).get();
1505+
file.write(buf);
1506+
}
1507+
1508+
{
1509+
let file = io::file_reader(&path).get();
1510+
let f = file.read_be_f32();
1511+
assert f == 8.1250;
1512+
}
1513+
}
1514+
1515+
#[test]
1516+
fn test_read_write_f32() {
1517+
let path = Path("tmp/lib-io-test-read-write-f32.tmp");
1518+
let f:f32 = 8.1250;
1519+
1520+
{
1521+
let file = io::file_writer(&path, [io::Create]).get();
1522+
file.write_be_f32(f);
1523+
file.write_le_f32(f);
1524+
}
1525+
1526+
{
1527+
let file = io::file_reader(&path).get();
1528+
assert file.read_be_f32() == 8.1250;
1529+
assert file.read_le_f32() == 8.1250;
1530+
}
1531+
}
14241532
}
14251533

14261534
//

branches/try2/src/libcore/trie.rs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use prelude::*;
1414

1515
// FIXME: #3469: need to manually update TrieNode when SHIFT changes
16-
// FIXME: #5244: need to manually update the TrieNode constructor
1716
const SHIFT: uint = 4;
1817
const SIZE: uint = 1 << SHIFT;
1918
const MASK: uint = SIZE - 1;
@@ -57,7 +56,7 @@ impl<T> Container for TrieMap<T> {
5756
pure fn is_empty(&self) -> bool { self.len() == 0 }
5857
}
5958

60-
impl<T> Mutable for TrieMap<T> {
59+
impl<T: Copy> Mutable for TrieMap<T> {
6160
/// Clear the map, removing all values.
6261
#[inline(always)]
6362
fn clear(&mut self) {
@@ -66,7 +65,7 @@ impl<T> Mutable for TrieMap<T> {
6665
}
6766
}
6867

69-
impl<T> Map<uint, T> for TrieMap<T> {
68+
impl<T: Copy> Map<uint, T> for TrieMap<T> {
7069
/// Return true if the map contains a value for the specified key
7170
#[inline(always)]
7271
pure fn contains_key(&self, key: &uint) -> bool {
@@ -128,7 +127,7 @@ impl<T> Map<uint, T> for TrieMap<T> {
128127
}
129128
}
130129

131-
impl<T> TrieMap<T> {
130+
impl<T: Copy> TrieMap<T> {
132131
#[inline(always)]
133132
static pure fn new() -> TrieMap<T> {
134133
TrieMap{root: TrieNode::new(), length: 0}
@@ -210,15 +209,10 @@ struct TrieNode<T> {
210209
children: [Child<T> * 16] // FIXME: #3469: can't use the SIZE constant yet
211210
}
212211

213-
impl<T> TrieNode<T> {
212+
impl<T: Copy> TrieNode<T> {
214213
#[inline(always)]
215214
static pure fn new() -> TrieNode<T> {
216-
// FIXME: #5244: [Nothing, ..SIZE] should be possible without Copy
217-
TrieNode{count: 0,
218-
children: [Nothing, Nothing, Nothing, Nothing,
219-
Nothing, Nothing, Nothing, Nothing,
220-
Nothing, Nothing, Nothing, Nothing,
221-
Nothing, Nothing, Nothing, Nothing]}
215+
TrieNode{count: 0, children: [Nothing, ..SIZE]}
222216
}
223217
}
224218

@@ -266,16 +260,12 @@ pure fn chunk(n: uint, idx: uint) -> uint {
266260
(n >> (SHIFT * real_idx)) & MASK
267261
}
268262

269-
fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint,
263+
fn insert<T: Copy>(count: &mut uint, child: &mut Child<T>, key: uint,
270264
value: T, idx: uint) -> bool {
271-
let mut tmp = Nothing;
272-
tmp <-> *child;
273-
let mut added = false;
274-
275-
*child = match tmp {
265+
match *child {
276266
External(stored_key, stored_value) => {
277267
if stored_key == key {
278-
External(stored_key, value)
268+
false // already in the trie
279269
} else {
280270
// conflict - split the node
281271
let mut new = ~TrieNode::new();
@@ -284,24 +274,20 @@ fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint,
284274
stored_key, stored_value, idx + 1);
285275
insert(&mut new.count, &mut new.children[chunk(key, idx)], key,
286276
value, idx + 1);
287-
added = true;
288-
Internal(new)
277+
*child = Internal(new);
278+
true
289279
}
290280
}
291-
Internal(x) => {
292-
let mut x = x;
293-
added = insert(&mut x.count, &mut x.children[chunk(key, idx)], key,
294-
value, idx + 1);
295-
Internal(x)
296-
281+
Internal(ref mut x) => {
282+
insert(&mut x.count, &mut x.children[chunk(key, idx)], key, value,
283+
idx + 1)
297284
}
298285
Nothing => {
299286
*count += 1;
300-
added = true;
301-
External(key, value)
287+
*child = External(key, value);
288+
true
302289
}
303-
};
304-
added
290+
}
305291
}
306292

307293
fn remove<T>(count: &mut uint, child: &mut Child<T>, key: uint,

0 commit comments

Comments
 (0)