Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming
diff options
context:
space:
mode:
Commit6db8e09d53f9e108fb4ce00c81cbfb67ae0465c7 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-15 09:37:35 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-15 09:37:35 +0300
Files475fd303b912bbfc50c5e83d32ac4067f699ff39 /Year 1/Object-oriented Programming
uni-projects-6db8e09d53f9e108fb4ce00c81cbfb67ae0465c7.tar
uni-projects-6db8e09d53f9e108fb4ce00c81cbfb67ae0465c7.tar.gz
uni-projects-6db8e09d53f9e108fb4ce00c81cbfb67ae0465c7.zip
Parentd5bf0fd7d55dbe6c2c8d5228e7d531128298a8c0 (diff)
Separated wordCollection count into two variable and fixed functions
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp
index aa24c3a..4a375e5 100644
--- a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.cpp
@@ -5,35 +5,37 @@
/* Private */
void WordCollection::free() {
- if(words != nullptr) delete[] words;
- if(sortMap != nullptr) delete[] sortMap;
+ delete[] words;
+ delete[] sortMap;
}
void WordCollection::copyFrom(const WordCollection& other) {
- count = other.count;
+ sortMapCount = other.sortMapCount;
+ wordsCount = other.wordsCount;
length = other.length;
words = new DynStr[length];
sortMap = new int[length];
- for (int i = 0; i < count; i++) {
+ for (int i = 0; i < length; i++) { // Must be < length, since we can have valid items outside the count range in words
words[i] = other.words[i];
sortMap[i] = other.sortMap[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 < count; i++) {
+ for (int i = 0; i < oldLength; i++) {
newWords[i] = words[i];
newSortMap[i] = sortMap[i];
}
-
- if (words != nullptr) delete[] words;
- if (sortMap != nullptr) delete[] sortMap;
+ delete[] words;
+ delete[] sortMap;
words = newWords;
sortMap = newSortMap;
@@ -41,17 +43,17 @@ void WordCollection::resize() {
void WordCollection::insertSortMapIndex(int position, int value) {
if (position == -1)
- position = count;
+ position = sortMapCount;
- for (int i = count; i > position; i--)
+ for (int i = sortMapCount; i > position; i--)
sortMap[i] = sortMap[i - 1];
sortMap[position] = value;
}
void WordCollection::removeSortMapIndex(int position) {
- for (int i = position; i <= count - 2; i++)
+ for (int i = position; i <= sortMapCount - 2; i++)
sortMap[i] = sortMap[i + 1];
- sortMap[count - 1] = 0;
+ sortMap[sortMapCount - 1] = 0;
}
int WordCollection::findSortMapIndexWhen(int condition, const char* str) {
@@ -59,7 +61,7 @@ int WordCollection::findSortMapIndexWhen(int condition, const char* str) {
else if (condition > 1) condition = 1;
// TODO: replace with binary search
- for (int i = 0; i < count; i++) {
+ for (int i = 0; i < sortMapCount; i++) {
int cmp = strcmp(words[sortMap[i]].get_str(), str);
if (cmp > 1) cmp = 1;
else if (cmp < -1) cmp = -1;
@@ -75,7 +77,7 @@ int WordCollection::findSortMapIndexWhen(int condition, const char* str) {
WordCollection::WordCollection() {
words = nullptr;
sortMap = nullptr;
- count = length = 0;
+ sortMapCount = wordsCount = length = 0;
}
WordCollection::WordCollection(const WordCollection& other) {
@@ -95,26 +97,32 @@ WordCollection::~WordCollection() {
}
WordCollection& WordCollection::operator+=(const WordCollection& other) {
- for (int i = 0; i < other.count; i++) {
- (*this) *= other.words[i].get_str();
+ for (int i = 0; i < other.sortMapCount; i++) {
+ (*this) *= other.words[other.sortMap[i]].get_str();
}
return *this;
}
WordCollection& WordCollection::operator-=(const WordCollection& other) {
- for (int i = 0; i < other.count; i++) {
- (*this) /= other.words[i].get_str();
+ if (this != &other) {
+ for (int i = 0; i < other.sortMapCount; i++) {
+ (*this) /= other.words[other.sortMap[i]].get_str();
+ }
+ }
+ else {
+ *this = WordCollection();
}
return *this;
}
WordCollection& WordCollection::operator*=(const char* str) {
if (!(*this)[str]) {
- if (count == length) resize();
+ if (sortMapCount == length || wordsCount == length) resize();
- words[count] = DynStr(str);
- insertSortMapIndex(findSortMapIndexWhen(1, str), count);
- count++;
+ words[wordsCount] = DynStr(str);
+ insertSortMapIndex(findSortMapIndexWhen(1, str), wordsCount);
+ sortMapCount++;
+ wordsCount++;
}
return *this;
}
@@ -124,16 +132,19 @@ WordCollection& WordCollection::operator/=(const char* str) {
if (ind > -1) {
words[sortMap[ind]] = DynStr();
removeSortMapIndex(ind);
- count--;
+ sortMapCount--;
}
return *this;
}
std::ostream& operator<<(std::ostream& ostrm, const WordCollection& collection) {
ostrm << "{";
- for (int i = 0; i <= collection.count - 2; i++)
- ostrm << collection.words[collection.sortMap[i]].get_str() << ", ";
- ostrm << collection.words[collection.sortMap[collection.count - 1]].get_str() << "}";
+ if (collection.sortMapCount > 0) {
+ for (int i = 0; i <= collection.sortMapCount - 2; i++)
+ ostrm << collection.words[collection.sortMap[i]].get_str() << ", ";
+ ostrm << collection.words[collection.sortMap[collection.sortMapCount - 1]].get_str();
+ }
+ ostrm << "}";
return ostrm;
}
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.h b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.h
index f7a7142..fdd3363 100644
--- a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.h
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/WordCollection.h
@@ -5,7 +5,8 @@
class WordCollection {
DynStr* words;
int* sortMap;
- int count;
+ int sortMapCount;
+ int wordsCount;
int length;
void free();