···11+# Generated by Cargo
22+# will have compiled files and executables
33+/target/
44+**/*.rs.bk
55+66+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
77+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
88+Cargo.lock
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+75
rust/parallel-letter-frequency/README.md
···11+# Parallel Letter Frequency
22+33+Welcome to Parallel Letter Frequency on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+66+## Instructions
77+88+Count the frequency of letters in texts using parallel computation.
99+1010+Parallelism is about doing things in parallel that can also be done
1111+sequentially. A common example is counting the frequency of letters.
1212+Create a function that returns the total frequency of each letter in a
1313+list of texts and that employs parallelism.
1414+1515+Learn more about concurrency in Rust here:
1616+1717+- [Concurrency](https://doc.rust-lang.org/book/ch16-00-concurrency.html)
1818+1919+## Bonus
2020+2121+This exercise also includes a benchmark, with a sequential implementation as a
2222+baseline. You can compare your solution to the benchmark. Observe the
2323+effect different size inputs have on the performance of each. Can you
2424+surpass the benchmark using concurrent programming techniques?
2525+2626+As of this writing, test::Bencher is unstable and only available on
2727+*nightly* Rust. Run the benchmarks with Cargo:
2828+2929+```
3030+cargo bench
3131+```
3232+3333+If you are using rustup.rs:
3434+3535+```
3636+rustup run nightly cargo bench
3737+```
3838+3939+- [Benchmark tests](https://doc.rust-lang.org/stable/unstable-book/library-features/test.html)
4040+4141+Learn more about nightly Rust:
4242+4343+- [Nightly Rust](https://doc.rust-lang.org/stable/book/2018-edition/appendix-06-nightly-rust.html)
4444+- [Rustup: Working with nightly](https://github.com/rust-lang-nursery/rustup.rs#working-with-nightly-rust)
4545+4646+## Source
4747+4848+### Created by
4949+5050+- @EduardoBautista
5151+5252+### Contributed to by
5353+5454+- @andrewclarkson
5555+- @ashleygwilliams
5656+- @ccouzens
5757+- @ClashTheBunny
5858+- @coriolinus
5959+- @cwhakes
6060+- @EduardoBautista
6161+- @efx
6262+- @ErikSchierboom
6363+- @etrepum
6464+- @glennpratt
6565+- @IanWhitney
6666+- @kytrinyx
6767+- @lutostag
6868+- @mkantor
6969+- @nfiles
7070+- @petertseng
7171+- @rofrol
7272+- @sjwarner-bp
7373+- @stringparser
7474+- @xakon
7575+- @ZapAnton
···11+#![feature(test)]
22+extern crate parallel_letter_frequency;
33+extern crate test;
44+55+use std::collections::HashMap;
66+use test::Bencher;
77+88+#[bench]
99+fn bench_tiny_parallel(b: &mut Bencher) {
1010+ let tiny = &["a"];
1111+ b.iter(|| parallel_letter_frequency::frequency(tiny, 3));
1212+}
1313+1414+#[bench]
1515+fn bench_tiny_sequential(b: &mut Bencher) {
1616+ let tiny = &["a"];
1717+ b.iter(|| frequency(tiny));
1818+}
1919+2020+#[bench]
2121+fn bench_small_parallel(b: &mut Bencher) {
2222+ let texts = all_texts(1);
2323+ b.iter(|| parallel_letter_frequency::frequency(&texts, 3));
2424+}
2525+2626+#[bench]
2727+fn bench_small_sequential(b: &mut Bencher) {
2828+ let texts = all_texts(1);
2929+ b.iter(|| frequency(&texts));
3030+}
3131+3232+#[bench]
3333+fn bench_large_parallel(b: &mut Bencher) {
3434+ let texts = all_texts(30);
3535+ b.iter(|| parallel_letter_frequency::frequency(&texts, 3));
3636+}
3737+3838+#[bench]
3939+fn bench_large_sequential(b: &mut Bencher) {
4040+ let texts = all_texts(30);
4141+ b.iter(|| frequency(&texts));
4242+}
4343+4444+/// Simple sequential char frequency. Can it be beat?
4545+pub fn frequency(texts: &[&str]) -> HashMap<char, usize> {
4646+ let mut map = HashMap::new();
4747+4848+ for line in texts {
4949+ for chr in line.chars().filter(|c| c.is_alphabetic()) {
5050+ if let Some(c) = chr.to_lowercase().next() {
5151+ (*map.entry(c).or_insert(0)) += 1;
5252+ }
5353+ }
5454+ }
5555+5656+ map
5757+}
5858+5959+fn all_texts(repeat: usize) -> Vec<&'static str> {
6060+ [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER]
6161+ .iter()
6262+ .cycle()
6363+ .take(3 * repeat)
6464+ .flat_map(|anthem| anthem.iter().cloned())
6565+ .collect()
6666+}
6767+6868+// Poem by Friedrich Schiller. The corresponding music is the European Anthem.
6969+pub const ODE_AN_DIE_FREUDE: [&str; 8] = [
7070+ "Freude schöner Götterfunken",
7171+ "Tochter aus Elysium,",
7272+ "Wir betreten feuertrunken,",
7373+ "Himmlische, dein Heiligtum!",
7474+ "Deine Zauber binden wieder",
7575+ "Was die Mode streng geteilt;",
7676+ "Alle Menschen werden Brüder,",
7777+ "Wo dein sanfter Flügel weilt.",
7878+];
7979+8080+// Dutch national anthem
8181+pub const WILHELMUS: [&str; 8] = [
8282+ "Wilhelmus van Nassouwe",
8383+ "ben ik, van Duitsen bloed,",
8484+ "den vaderland getrouwe",
8585+ "blijf ik tot in den dood.",
8686+ "Een Prinse van Oranje",
8787+ "ben ik, vrij, onverveerd,",
8888+ "den Koning van Hispanje",
8989+ "heb ik altijd geëerd.",
9090+];
9191+9292+// American national anthem
9393+pub const STAR_SPANGLED_BANNER: [&str; 8] = [
9494+ "O say can you see by the dawn's early light,",
9595+ "What so proudly we hailed at the twilight's last gleaming,",
9696+ "Whose broad stripes and bright stars through the perilous fight,",
9797+ "O'er the ramparts we watched, were so gallantly streaming?",
9898+ "And the rockets' red glare, the bombs bursting in air,",
9999+ "Gave proof through the night that our flag was still there;",
100100+ "O say does that star-spangled banner yet wave,",
101101+ "O'er the land of the free and the home of the brave?",
102102+];
···11+use std::collections::HashMap;
22+33+use parallel_letter_frequency as frequency;
44+55+// Poem by Friedrich Schiller. The corresponding music is the European Anthem.
66+const ODE_AN_DIE_FREUDE: [&str; 8] = [
77+ "Freude schöner Götterfunken",
88+ "Tochter aus Elysium,",
99+ "Wir betreten feuertrunken,",
1010+ "Himmlische, dein Heiligtum!",
1111+ "Deine Zauber binden wieder",
1212+ "Was die Mode streng geteilt;",
1313+ "Alle Menschen werden Brüder,",
1414+ "Wo dein sanfter Flügel weilt.",
1515+];
1616+1717+// Dutch national anthem
1818+const WILHELMUS: [&str; 8] = [
1919+ "Wilhelmus van Nassouwe",
2020+ "ben ik, van Duitsen bloed,",
2121+ "den vaderland getrouwe",
2222+ "blijf ik tot in den dood.",
2323+ "Een Prinse van Oranje",
2424+ "ben ik, vrij, onverveerd,",
2525+ "den Koning van Hispanje",
2626+ "heb ik altijd geëerd.",
2727+];
2828+2929+// American national anthem
3030+const STAR_SPANGLED_BANNER: [&str; 8] = [
3131+ "O say can you see by the dawn's early light,",
3232+ "What so proudly we hailed at the twilight's last gleaming,",
3333+ "Whose broad stripes and bright stars through the perilous fight,",
3434+ "O'er the ramparts we watched, were so gallantly streaming?",
3535+ "And the rockets' red glare, the bombs bursting in air,",
3636+ "Gave proof through the night that our flag was still there;",
3737+ "O say does that star-spangled banner yet wave,",
3838+ "O'er the land of the free and the home of the brave?",
3939+];
4040+4141+#[test]
4242+fn test_no_texts() {
4343+ assert_eq!(frequency::frequency(&[], 4), HashMap::new());
4444+}
4545+4646+#[test]
4747+#[ignore]
4848+fn test_one_letter() {
4949+ let mut hm = HashMap::new();
5050+ hm.insert('a', 1);
5151+ assert_eq!(frequency::frequency(&["a"], 4), hm);
5252+}
5353+5454+#[test]
5555+#[ignore]
5656+fn test_case_insensitivity() {
5757+ let mut hm = HashMap::new();
5858+ hm.insert('a', 2);
5959+ assert_eq!(frequency::frequency(&["aA"], 4), hm);
6060+}
6161+6262+#[test]
6363+#[ignore]
6464+fn test_many_empty_lines() {
6565+ let v = vec![""; 1000];
6666+ assert_eq!(frequency::frequency(&v[..], 4), HashMap::new());
6767+}
6868+6969+#[test]
7070+#[ignore]
7171+fn test_many_times_same_text() {
7272+ let v = vec!["abc"; 1000];
7373+ let mut hm = HashMap::new();
7474+ hm.insert('a', 1000);
7575+ hm.insert('b', 1000);
7676+ hm.insert('c', 1000);
7777+ assert_eq!(frequency::frequency(&v[..], 4), hm);
7878+}
7979+8080+#[test]
8181+fn test_punctuation_doesnt_count() {
8282+ assert!(!frequency::frequency(&WILHELMUS, 4).contains_key(&','));
8383+}
8484+8585+#[test]
8686+#[ignore]
8787+fn test_numbers_dont_count() {
8888+ assert!(!frequency::frequency(&["Testing, 1, 2, 3"], 4).contains_key(&'1'));
8989+}
9090+9191+#[test]
9292+#[ignore]
9393+fn test_all_three_anthems_1_worker() {
9494+ let mut v = Vec::new();
9595+ for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() {
9696+ for line in anthem.iter() {
9797+ v.push(*line);
9898+ }
9999+ }
100100+ let freqs = frequency::frequency(&v[..], 1);
101101+ assert_eq!(freqs.get(&'a'), Some(&49));
102102+ assert_eq!(freqs.get(&'t'), Some(&56));
103103+ assert_eq!(freqs.get(&'ü'), Some(&2));
104104+}
105105+106106+#[test]
107107+#[ignore]
108108+fn test_all_three_anthems_3_workers() {
109109+ let mut v = Vec::new();
110110+ for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() {
111111+ for line in anthem.iter() {
112112+ v.push(*line);
113113+ }
114114+ }
115115+ let freqs = frequency::frequency(&v[..], 3);
116116+ assert_eq!(freqs.get(&'a'), Some(&49));
117117+ assert_eq!(freqs.get(&'t'), Some(&56));
118118+ assert_eq!(freqs.get(&'ü'), Some(&2));
119119+}
120120+121121+#[test]
122122+#[ignore]
123123+fn test_non_integer_multiple_of_threads() {
124124+ let v = vec!["abc"; 999];
125125+ let mut hm = HashMap::new();
126126+ hm.insert('a', 999);
127127+ hm.insert('b', 999);
128128+ hm.insert('c', 999);
129129+ assert_eq!(frequency::frequency(&v[..], 4), hm);
130130+}