Created
December 28, 2021 19:32
-
-
Save RealNeGate/fdddf7006c136d4b573f42a61c2d0212 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdlib> | |
| #include <string> | |
| #include <numeric> | |
| #include <execution> | |
| // enable modern c++ if you're a cool enterprise person | |
| #if MODERN_CPP | |
| bool is_word_beginning(char l, char r) { return std::isspace(l) && !std::isspace(r); } | |
| std::size_t word_count(std::string_view s) { | |
| if (s.empty()) return 0; | |
| return std::transform_reduce(std::execution::par_unseq, | |
| s.begin(), s.end() - 1, s.begin() + 1, | |
| std::size_t(!std::isspace(s.front()) ? 1 : 0), | |
| std::plus(), | |
| is_word_beginning | |
| ); | |
| } | |
| int main() { | |
| std::string_view frost = "Whose woods these are I think I know.\n" | |
| "His house is in the village though; \n" | |
| "He will not see me stopping here \n" | |
| "To watch his woods fill up with snow.\n"; | |
| return word_count(frost); | |
| } | |
| #else | |
| size_t word_count(const char* s) { | |
| size_t count = 0; | |
| for (; *s; s++) { | |
| count += isspace(s[0]) && !isspace(s[1]); | |
| } | |
| return count; | |
| } | |
| int main() { | |
| const char* frost = "Whose woods these are I think I know.\n" | |
| "His house is in the village though; \n" | |
| "He will not see me stopping here \n" | |
| "To watch his woods fill up with snow.\n"; | |
| return word_count(frost); | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment