-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwords_lib.h
120 lines (107 loc) · 3.21 KB
/
words_lib.h
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
116
117
118
119
120
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
class IWordsParser
{
public:
virtual void parse(const std::string& s) = 0;
virtual const std::vector<std::string>& get_words() const = 0;
virtual ~IWordsParser() = default;
};
class WordsParser : public IWordsParser
{
public:
WordsParser(const std::string& delim) : m_delim(delim) {}
void parse(const std::string& s) override;
const std::vector<std::string>& get_words() const override { return m_words; }
private:
const std::string m_delim;
std::vector<std::string> m_words;
};
class ITask
{
public:
virtual void process() = 0;
virtual void print(std::ostream& out) const = 0;
virtual ~ITask() = default;
};
class WordCount : public ITask
{
public:
WordCount(IWordsParser& p) : m_parser(p) {}
void process() override;
void print(std::ostream& out) const override;
size_t get_count() const { return m_occurrences.size(); }
size_t get_count_of(const std::string &s) const { return m_occurrences.count(s) ? m_occurrences.at(s) : 0; }
private:
IWordsParser& m_parser;
std::unordered_map<std::string, size_t> m_occurrences;
};
class LongestWord: public ITask
{
public:
LongestWord(IWordsParser& p) : m_parser(p) {}
void process() override;
void print(std::ostream& out) const override;
std::string get_longest() const { return m_res; }
private:
IWordsParser& m_parser;
std::string m_res;
};
class LongestRepeatableSequenceWord : public ITask
{
public:
LongestRepeatableSequenceWord(IWordsParser& p) : m_parser(p) {}
void process() override;
void print(std::ostream& out) const override;
std::string get_sequence() const { return m_sequence; }
std::string get_word() const { return m_word; }
private:
IWordsParser& m_parser;
std::string m_sequence, m_word;
std::string longest_repeated_substring(const std::string& str);
};
class ReverseWordsOrder : public ITask
{
public:
ReverseWordsOrder(IWordsParser& p) : m_parser(p) {}
void process() override;
void print(std::ostream& out) const override;
std::vector<std::string> get_words() const { return m_reversed_words; }
private:
IWordsParser& m_parser;
std::vector<std::string> m_reversed_words;
};
class Sort : public ITask
{
public:
Sort(IWordsParser& p) : m_parser(p) {}
void process() override;
void print(std::ostream& out) const override;
std::vector<std::string> get_words() const { return m_sorted_words; }
private:
IWordsParser& m_parser;
std::vector<std::string> m_sorted_words;
virtual bool comparator(const std::string& s1, const std::string& s2) const { return s1 > s2; }
};
class SortDesc : public Sort
{
public:
SortDesc(IWordsParser& p) : Sort(p) {}
void print(std::ostream& out) const override;
private:
bool comparator(const std::string& s1, const std::string& s2) const override { return s1 < s2; }
};
class ReverseLetters : public ITask
{
public:
ReverseLetters(IWordsParser& p) : m_parser(p) {}
void process() override;
void print(std::ostream& out) const override;
std::vector<std::string> get_words() const { return m_words; }
private:
IWordsParser& m_parser;
std::vector<std::string> m_words;
};