blob: 4cfdbb536793999a2dd988bcee4333bf27f98178 (
plain) (
blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include "WordCollection.h"
#include "cstring"
#include <iostream>
/* Private */
void WordCollection::free() {
delete[] words;
delete[] sortedPos;
}
void WordCollection::copyFrom(const WordCollection& other) {
count = other.count;
storedWordsCount = other.storedWordsCount;
length = other.length;
words = new DynStr[length];
sortedPos = new int[length];
for (int i = 0; i < length; i++) { // Must be < length, since storedWordsCount could be > count
words[i] = other.words[i];
sortedPos[i] = other.sortedPos[i];
}
}
void WordCollection::resize() {
int oldLength = length;
if (length == 0) length = 8;
else length <<= 1;
DynStr* newWords = new DynStr[length];
int* newSortMap = new int[length];
for (int i = 0; i < oldLength; i++) {
newWords[i] = words[i];
newSortMap[i] = sortedPos[i];
}
delete[] words;
delete[] sortedPos;
words = newWords;
sortedPos = newSortMap;
}
void WordCollection::insertSortedPos(int index, int value) {
if (index == -1)
index = count;
for (int i = count; i > index; i--)
sortedPos[i] = sortedPos[i - 1];
sortedPos[index] = value;
}
void WordCollection::removeSortedPos(int index) {
for (int i = index; i <= count - 2; i++)
sortedPos[i] = sortedPos[i + 1];
sortedPos[count - 1] = 0;
}
int WordCollection::findSortedPosIndexFirstBigger(const char* str) const {
for (int i = 0; i < count; i++) {
if (words[sortedPos[i]].compare(str) > 0)
return i;
}
return -1;
}
int WordCollection::findSortedPosIndex(const char* str) const {
int l = 0, r = count - 1;
while (l <= r) {
int m = (l + r) / 2;
int cmp = words[sortedPos[m]].compare(str);
if (cmp > 0) r = m - 1;
else if (cmp < 0) l = m + 1;
else return m;
}
return -1;
}
/* Public */
WordCollection::WordCollection() {
words = nullptr;
sortedPos = nullptr;
count = storedWordsCount = length = 0;
}
WordCollection::WordCollection(const WordCollection& other) {
copyFrom(other);
}
WordCollection& WordCollection::operator=(const WordCollection& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
WordCollection::~WordCollection() {
free();
}
WordCollection& WordCollection::operator+=(const WordCollection& other) {
if (this != &other) {
for (int i = 0; i < other.count; i++) {
(*this) *= other.words[other.sortedPos[i]].get_str();
}
}
return *this;
}
WordCollection& WordCollection::operator-=(const WordCollection& other) {
if (this != &other) {
for (int i = 0; i < other.count; i++) {
(*this) /= other.words[other.sortedPos[i]].get_str();
}
}
else *this = WordCollection();
return *this;
}
WordCollection& WordCollection::operator*=(const char* str) {
if (!(*this)[str]) {
if (count == length || storedWordsCount == length) resize();
words[storedWordsCount] = DynStr(str);
insertSortedPos(findSortedPosIndexFirstBigger(str), storedWordsCount);
count++;
storedWordsCount++;
}
return *this;
}
WordCollection& WordCollection::operator/=(const char* str) {
int ind = findSortedPosIndex(str);
if (ind > -1) {
words[sortedPos[ind]] = DynStr();
removeSortedPos(ind);
count--;
}
return *this;
}
std::ostream& operator<<(std::ostream& ostrm, const WordCollection& collection) {
ostrm << "{";
if (collection.count > 0) {
for (int i = 0; i <= collection.count - 2; i++)
ostrm << collection.words[collection.sortedPos[i]].get_str() << ", ";
ostrm << collection.words[collection.sortedPos[collection.count - 1]].get_str();
}
ostrm << "}";
return ostrm;
}
std::istream& operator>>(std::istream& istrm, WordCollection& collection) {
char buffer[1024];
istrm.getline(buffer, 1024);
collection *= buffer;
return istrm;
}
bool WordCollection::operator[](const char* str) const {
return findSortedPosIndex(str) > -1;
}
const WordCollection operator+(const WordCollection& lhs, const WordCollection& rhs) {
WordCollection res(lhs);
res += rhs;
return res;
}
const WordCollection operator-(const WordCollection& lhs, const WordCollection& rhs) {
WordCollection res(lhs);
res -= rhs;
return res;
}
|