shushus

Спринт 8. Move-итераторы. Задание 2.

Feb 2nd, 2023
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. template <typename Token>
  9. using Sentence = vector<Token>;
  10.  
  11. template <typename TokenForwardIt>
  12. TokenForwardIt FindSentenceEnd(TokenForwardIt tokens_begin, TokenForwardIt tokens_end) {
  13.     const TokenForwardIt before_sentence_end
  14.         = adjacent_find(tokens_begin, tokens_end, [](const auto& left_token, const auto& right_token) {
  15.               return left_token.IsEndSentencePunctuation() && !right_token.IsEndSentencePunctuation();
  16.           });
  17.     return before_sentence_end == tokens_end ? tokens_end : next(before_sentence_end);
  18. }
  19.  
  20. template <typename Token>
  21. vector<Sentence<Token>> SplitIntoSentences(vector<Token> tokens) {
  22.     vector<Sentence<Token>> helper;
  23.     auto iter_begin = tokens.begin(), iter_end = tokens.end(), iter = tokens.begin();
  24.     while (iter_begin != iter_end) {
  25.     auto token = FindSentenceEnd(iter_begin, iter_end);
  26.         if (iter == token) {
  27.             helper.push_back({make_move_iterator(iter_begin), make_move_iterator(token)});
  28.             iter_begin = iter;
  29.         }
  30.         ++iter;
  31.     }
  32.     return helper;
  33. }
  34.  
  35. struct TestToken {
  36.     string data;
  37.     bool is_end_sentence_punctuation = false;
  38.  
  39.     bool IsEndSentencePunctuation() const {
  40.         return is_end_sentence_punctuation;
  41.     }
  42.     bool operator==(const TestToken& other) const {
  43.         return data == other.data && is_end_sentence_punctuation == other.is_end_sentence_punctuation;
  44.     }
  45. };
  46.  
  47. ostream& operator<<(ostream& stream, const TestToken& token) {
  48.     return stream << token.data;
  49. }
  50.  
  51. void TestSplitting() {
  52.     assert(SplitIntoSentences(vector<TestToken>({{"Split"s}, {"into"s}, {"sentences"s}, {"!"s}}))
  53.            == vector<Sentence<TestToken>>({{{"Split"s}, {"into"s}, {"sentences"s}, {"!"s}}}));
  54.  
  55.     assert(SplitIntoSentences(vector<TestToken>({{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}}))
  56.            == vector<Sentence<TestToken>>({{{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}}}));
  57.  
  58.     assert(SplitIntoSentences(vector<TestToken>(
  59.                {{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}, {"!"s, true}, {"Without"s}, {"copies"s}, {"."s, true}}))
  60.            == vector<Sentence<TestToken>>({
  61.                {{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}, {"!"s, true}},
  62.                {{"Without"s}, {"copies"s}, {"."s, true}},
  63.            }));
  64. }
  65.  
  66. int main() {
  67.     TestSplitting();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment