-
Notifications
You must be signed in to change notification settings - Fork 520
/
main.rs
115 lines (99 loc) · 2.84 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#![feature(test)]
extern crate test;
use test::Bencher;
fn main() {
println!("Hello, world!");
}
use std::collections::HashSet;
pub fn check_hash(candidate: &str) -> bool {
let mut hs = HashSet::new();
candidate
.bytes()
.filter(|c| c.is_ascii_alphabetic())
.map(|c| c.to_ascii_lowercase())
.all(|c| hs.insert(c))
}
const A_LCASE: u8 = 97;
const Z_LCASE: u8 = 122;
const A_UCASE: u8 = 65;
const Z_UCASE: u8 = 90;
pub fn check_bits(candidate: &str) -> bool {
let mut letter_flags: u32 = 0;
for letter in candidate.bytes() {
if letter >= A_LCASE && letter <= Z_LCASE {
if letter_flags & (1 << (letter - A_LCASE)) != 0 {
return false;
} else {
letter_flags |= 1 << (letter - A_LCASE);
}
} else if letter >= A_UCASE && letter <= Z_UCASE {
if letter_flags & (1 << (letter - A_UCASE)) != 0 {
return false;
} else {
letter_flags |= 1 << (letter - A_UCASE);
}
}
}
return true;
}
pub fn check_bits_func(candidate: &str) -> bool {
candidate
.bytes()
.filter_map(|c| {
c.is_ascii_alphabetic()
.then(|| 1u32 << (c.to_ascii_lowercase() - A_LCASE))
})
.try_fold(0u32, |ltr_flags, ltr| {
(ltr_flags & ltr == 0).then(|| ltr_flags | ltr)
})
.is_some()
}
pub fn check_hash_filtermap(candidate: &str) -> bool {
let mut hs = HashSet::new();
candidate
.bytes()
.filter_map(|c| c.is_ascii_alphabetic().then(|| c.to_ascii_lowercase()))
.all(|c| hs.insert(c))
}
pub fn check_bits_func_filter_map(candidate: &str) -> bool {
candidate
.bytes()
.filter(|c| c.is_ascii_alphabetic())
.map(|c| 1u32 << (c.to_ascii_lowercase() - A_LCASE))
.try_fold(0u32, |ltr_flags, ltr| {
(ltr_flags & ltr == 0).then(|| ltr_flags | ltr)
})
.is_some()
}
pub fn check_hash_unicode(candidate: &str) -> bool {
let mut hs = std::collections::HashSet::new();
candidate
.to_lowercase()
.chars()
.filter(|c| c.is_alphabetic())
.all(|c| hs.insert(c))
}
#[bench]
fn check_hash(b: &mut Bencher) {
b.iter(|| check_hash("thumbscrew-japingly"));
}
#[bench]
fn check_bits(b: &mut Bencher) {
b.iter(|| check_bits("thumbscrew-japingly"));
}
#[bench]
fn check_bits_func(b: &mut Bencher) {
b.iter(|| check_bits_func("thumbscrew-japingly"));
}
#[bench]
fn check_hash_filtermap(b: &mut Bencher) {
b.iter(|| check_hash_filtermap("thumbscrew-japingly"));
}
#[bench]
fn check_bits_func_filter_map(b: &mut Bencher) {
b.iter(|| check_bits_func_filter_map("thumbscrew-japingly"));
}
#[bench]
fn check_hash_unicode(b: &mut Bencher) {
b.iter(|| check_hash_unicode("thumbscrew-japingly"));
}