-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSet.cpp
More file actions
117 lines (91 loc) · 2.57 KB
/
WordSet.cpp
File metadata and controls
117 lines (91 loc) · 2.57 KB
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
#include "WordSet.h"
#include <fstream>
#include <iostream>
#include <locale>
#include <codecvt>
void WordSet::addWord(const Word& word) {
words.push_back(word);
}
void WordSet::readFromFile(const std::string& filename, Alphabet alphabet) {
std::wifstream file(filename);
file.imbue(std::locale(std::locale(), new std::codecvt_utf8<wchar_t>));
if (!file.is_open()) {
std::wcerr << L"Error: Could not open file " << filename.c_str() << std::endl;
return;
}
std::wstring line;
while (std::getline(file, line)) {
if (!line.empty()) {
words.emplace_back(line, alphabet);
}
}
file.close();
}
size_t WordSet::size() const {
return words.size();
}
// Iterator implementation
WordSet::iterator::iterator(std::vector<Word>::iterator iter) : it(iter) {}
Word& WordSet::iterator::operator*() {
return *it;
}
Word* WordSet::iterator::operator->() {
return &(*it);
}
WordSet::iterator& WordSet::iterator::operator++() {
++it;
return *this;
}
WordSet::iterator WordSet::iterator::operator++(int) {
iterator temp = *this;
++it;
return temp;
}
bool WordSet::iterator::operator==(const iterator& other) const {
return it == other.it;
}
bool WordSet::iterator::operator!=(const iterator& other) const {
return it != other.it;
}
// Const iterator implementation
WordSet::const_iterator::const_iterator(std::vector<Word>::const_iterator iter) : it(iter) {}
const Word& WordSet::const_iterator::operator*() const {
return *it;
}
const Word* WordSet::const_iterator::operator->() const {
return &(*it);
}
WordSet::const_iterator& WordSet::const_iterator::operator++() {
++it;
return *this;
}
WordSet::const_iterator WordSet::const_iterator::operator++(int) {
const_iterator temp = *this;
++it;
return temp;
}
bool WordSet::const_iterator::operator==(const const_iterator& other) const {
return it == other.it;
}
bool WordSet::const_iterator::operator!=(const const_iterator& other) const {
return it != other.it;
}
// Iterator access methods
WordSet::iterator WordSet::begin() {
return iterator(words.begin());
}
WordSet::iterator WordSet::end() {
return iterator(words.end());
}
WordSet::const_iterator WordSet::begin() const {
return const_iterator(words.begin());
}
WordSet::const_iterator WordSet::end() const {
return const_iterator(words.end());
}
WordSet::const_iterator WordSet::cbegin() const {
return const_iterator(words.cbegin());
}
WordSet::const_iterator WordSet::cend() const {
return const_iterator(words.cend());
}