Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 09 / StringPool / StringPool.cpp
blob: ffe8f5c54c35ca680a91d26a44aedf7240e28224 (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
#include "StringPool.h"

void StringPool::copyFrom(const StringPool& other)
{
	data = new MyString * [other.count];
	for (size_t i = 0; i < other.count; i++)
		data[i] = new MyString(*other.data[i]);
	count = other.count;
}

StringPool::StringPool()
{
	count = 0;
	data = nullptr;
}
void StringPool::free()
{
	for (size_t i = 0; i < count; i++)
		delete data[i]; //destr of MyString
	delete[] data;
}

void StringPool::resize(size_t newCount)
{
	MyString** resizedArray = new MyString*[newCount];

	for (size_t i = 0; i < std::min(newCount,count); i++)
		resizedArray[i] = data[i];
	count = newCount;

	delete[] data;
	data = resizedArray;
}

StringPool& StringPool::operator*=(const MyString& word)
{
	if ((*this)[word])
		return *this;
	resize(count + 1);
	data[count - 1] = new MyString(word);
	size_t currentStringPosition = count - 1;
	while (currentStringPosition > 0 && *data[currentStringPosition] < *data[currentStringPosition - 1])
	{
		std::swap(data[currentStringPosition], data[currentStringPosition - 1]);
		currentStringPosition--;
	}
	return *this;
}

StringPool& StringPool::operator/=(const MyString& str)
{
	int indexOfWord = findWord(str);
	if (indexOfWord == -1)
		return *this;

	delete data[indexOfWord];

	for (int i = indexOfWord; i < count - 1; i++)
		data[i] = data[i + 1];

	resize(count - 1);
	return *this;
}


int StringPool::findWord(const MyString& str) const
{
	int l = 0;
	int r = count - 1;

	while (l <= r)
	{
		size_t mid = l + (r - l) / 2;

		if (*data[mid] == str)
			return mid;
		else if (*data[mid] < str)
			l = mid + 1;
		else
			r = mid - 1;
	}
	return -1;
}
bool StringPool::operator[](const MyString& str) const
{
	return findWord(str) != -1;
}

std::ostream& operator<<(std::ostream& stream, const StringPool& pool)
{
	for (size_t i = 0; i < pool.count; i++)
		stream << (*pool.data[i]) << " ";
	return stream;
}
void operator>>(const MyString& str, StringPool& pool)
{
	pool *= str;
}
StringPool& StringPool::operator+=(const StringPool& other)
{
	for (size_t i = 0; i < other.count; i++)
		(*this) *= (*other.data[i]);
	return *this;
}
StringPool& StringPool::operator-=(const StringPool& other)
{
	for (size_t i = 0; i < other.count; i++)
		(*this) /= (*other.data[i]);
	return *this;
}

StringPool operator+(const StringPool& lhs, const StringPool& rhs)
{
	StringPool copy(lhs);
	copy += rhs;
	return copy;
}
StringPool operator-(const StringPool& lhs, const StringPool& rhs)
{
	StringPool copy(lhs);
	copy -= rhs;
	return copy;
}

StringPool::StringPool(const StringPool& other)
{
	copyFrom(other);
}
StringPool& StringPool::operator=(const StringPool& other)
{
	if (this != &other)
	{
		free();
		copyFrom(other);
	}
	return *this;
}

StringPool::~StringPool() 
{
	free();
}