Skip to content

Commit df83a79

Browse files
committed
In generic word count, use str instead of [u8], and use built in is_alphabetic, etc. functions.
1 parent 0c3a128 commit df83a79

File tree

1 file changed

+8
-95
lines changed

1 file changed

+8
-95
lines changed

src/test/bench/task-perf-word-count-generic.rs

Lines changed: 8 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,26 @@ fn join(t: joinable_task) {
4646
t.recv()
4747
}
4848

49-
fn map(&&filename: [u8], emit: map_reduce::putter<[u8], int>) {
50-
let f = alt io::file_reader(str::from_bytes(filename)) {
49+
fn map(&&filename: str, emit: map_reduce::putter<str, int>) {
50+
let f = alt io::file_reader(filename) {
5151
result::ok(f) { f }
5252
result::err(e) { fail #fmt("%?", e) }
5353
};
5454

5555
loop {
5656
alt read_word(f) {
57-
some(w) { emit(str::bytes(w), 1); }
57+
some(w) { emit(w, 1); }
5858
none { break; }
5959
}
6060
}
6161
}
6262

63-
fn reduce(&&word: [u8], get: map_reduce::getter<int>) {
63+
fn reduce(&&word: str, get: map_reduce::getter<int>) {
6464
let mut count = 0;
6565

6666
loop { alt get() { some(_) { count += 1; } none { break; } } }
6767

68-
io::println(#fmt("%?\t%?", word, count));
68+
io::println(#fmt("%s\t%?", word, count));
6969
}
7070

7171
mod map_reduce {
@@ -243,14 +243,9 @@ fn main(argv: [str]) {
243243
ret;
244244
}
245245

246-
let mut iargs = [];
247-
vec::iter_between(argv, 1u, vec::len(argv)) {|a|
248-
iargs += [str::bytes(a)];
249-
}
250-
251246
let start = time::precise_time_ns();
252247

253-
map_reduce::map_reduce(map, reduce, iargs);
248+
map_reduce::map_reduce(map, reduce, vec::slice(argv, 1u, argv.len()));
254249
let stop = time::precise_time_ns();
255250

256251
let elapsed = (stop - start) / 1000000u64;
@@ -271,87 +266,5 @@ fn read_word(r: io::reader) -> option<str> {
271266
}
272267
ret none;
273268
}
274-
275-
fn is_digit(c: char) -> bool {
276-
alt c {
277-
'0' { true }
278-
'1' { true }
279-
'2' { true }
280-
'3' { true }
281-
'4' { true }
282-
'5' { true }
283-
'6' { true }
284-
'7' { true }
285-
'8' { true }
286-
'9' { true }
287-
_ { false }
288-
}
289-
}
290-
291-
fn is_alpha_lower(c: char) -> bool {
292-
alt c {
293-
'a' { true }
294-
'b' { true }
295-
'c' { true }
296-
'd' { true }
297-
'e' { true }
298-
'f' { true }
299-
'g' { true }
300-
'h' { true }
301-
'i' { true }
302-
'j' { true }
303-
'k' { true }
304-
'l' { true }
305-
'm' { true }
306-
'n' { true }
307-
'o' { true }
308-
'p' { true }
309-
'q' { true }
310-
'r' { true }
311-
's' { true }
312-
't' { true }
313-
'u' { true }
314-
'v' { true }
315-
'w' { true }
316-
'x' { true }
317-
'y' { true }
318-
'z' { true }
319-
_ { false }
320-
}
321-
}
322-
323-
fn is_alpha_upper(c: char) -> bool {
324-
alt c {
325-
'A' { true }
326-
'B' { true }
327-
'C' { true }
328-
'D' { true }
329-
'E' { true }
330-
'F' { true }
331-
'G' { true }
332-
'H' { true }
333-
'I' { true }
334-
'J' { true }
335-
'K' { true }
336-
'L' { true }
337-
'M' { true }
338-
'N' { true }
339-
'O' { true }
340-
'P' { true }
341-
'Q' { true }
342-
'R' { true }
343-
'S' { true }
344-
'T' { true }
345-
'U' { true }
346-
'V' { true }
347-
'W' { true }
348-
'X' { true }
349-
'Y' { true }
350-
'Z' { true }
351-
_ { false }
352-
}
353-
}
354-
355-
fn is_alpha(c: char) -> bool { is_alpha_upper(c) || is_alpha_lower(c) }
356-
357-
fn is_word_char(c: char) -> bool { is_alpha(c) || is_digit(c) || c == '_' }
269+
fn is_word_char(c: char) -> bool {
270+
char::is_alphabetic(c) || char::is_digit(c) || c == '_' }

0 commit comments

Comments
 (0)