From 086675f2e8b421f811ffc0ea140dc1b4f2b85e0d Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Tue, 19 Apr 2022 20:25:32 +0300 Subject: Add files via upload --- Sem. 09/StringPool/MyString/MyString.cpp | 134 +++++++++++++++++++++++++++++ Sem. 09/StringPool/MyString/MyString.h | 44 ++++++++++ Sem. 09/StringPool/Source.cpp | 29 ++++++- Sem. 09/StringPool/StringPool.cpp | 142 +++++++++++++++++++++++++++++++ Sem. 09/StringPool/StringPool.h | 37 ++++++++ 5 files changed, 385 insertions(+), 1 deletion(-) create mode 100644 Sem. 09/StringPool/MyString/MyString.cpp create mode 100644 Sem. 09/StringPool/MyString/MyString.h create mode 100644 Sem. 09/StringPool/StringPool.cpp create mode 100644 Sem. 09/StringPool/StringPool.h diff --git a/Sem. 09/StringPool/MyString/MyString.cpp b/Sem. 09/StringPool/MyString/MyString.cpp new file mode 100644 index 0000000..a2cee29 --- /dev/null +++ b/Sem. 09/StringPool/MyString/MyString.cpp @@ -0,0 +1,134 @@ +#include "MyString.h" +#include +#include + +void MyString::copyFrom(const MyString& other) +{ + str = new char[strlen(other.str) + 1]; + strcpy(str, other.str); + size = other.size; +} +void MyString::free() +{ + delete[] str; +} + +MyString::MyString() +{ + str = new char[1]; + str[0] = '\0'; + size = 0; +} + +MyString::MyString(const char* data) +{ + size = strlen(data); + str = new char[size + 1]; + strcpy(str, data); +} + +MyString::MyString(const MyString& other) +{ + copyFrom(other); +} +MyString& MyString::operator=(const MyString& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +MyString::~MyString() +{ + free(); +} + + +size_t MyString::getSize() const +{ + return size; +} +void MyString::concat(const MyString& other) +{ + char* temp = new char[getSize() + other.getSize() + 1]; + strcpy(temp, str); + strcat(temp, other.str); + + delete[] str; + str = temp; + size = size + other.getSize(); +} + +const char* MyString::c_str() const +{ + return str; +} + +MyString& MyString::operator+=(const MyString& other) +{ + concat(other); + return *this; +} + +MyString operator+(const MyString& lhs, const MyString& rhs) +{ + MyString copyOfLeft(lhs); + copyOfLeft += rhs; + return copyOfLeft; +} + +std::ostream& operator<<(std::ostream& stream, const MyString& str) +{ + stream << str.str; + return stream; +} + +std::istream& operator>>(std::istream& stream, MyString& str) +{ + delete[] str.str; + char buff[1024]; + stream >> buff; + + str.size = strlen(buff); + str.str = new char[str.size + 1]; + strcpy(str.str, buff); + + return stream; +} + +bool operator==(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) == 0; +} +bool operator<=(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) <= 0; + +} +bool operator<(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) < 0; + +} + +MyString::MyString(MyString&& otherString) +{ + str = otherString.str; + size = otherString.size; + otherString.str = nullptr; + std::cout << "move constr" << std::endl; +} + +MyString& MyString::operator=(MyString&& otherString) +{ + if (this != &otherString) + { + free(); + str = otherString.str; + otherString.str = nullptr; + size = otherString.size; + } + return *this; +} diff --git a/Sem. 09/StringPool/MyString/MyString.h b/Sem. 09/StringPool/MyString/MyString.h new file mode 100644 index 0000000..cabc311 --- /dev/null +++ b/Sem. 09/StringPool/MyString/MyString.h @@ -0,0 +1,44 @@ +#pragma once + +#include +class MyString +{ + char* str; + size_t size; + + void copyFrom(const MyString& other); + void free(); + +public: + MyString(); + MyString(const char*); + + MyString(const MyString&); + + MyString(MyString&&); //move constructor that accepts rvalue; + + MyString& operator=(const MyString& other); + MyString& operator=(MyString&& other); //move op= that accepts rvalue; + + ~MyString(); + + size_t getSize() const; + void concat(const MyString& other); + + const char* c_str() const; + + MyString& operator+=(const MyString& other); + + friend std::ostream& operator<<(std::ostream& stream, const MyString& str); + friend std::istream& operator>>(std::istream& stream, MyString& str); + +}; + +MyString operator+(const MyString& lhs, const MyString& rhs); + +bool operator==(const MyString& lhs, const MyString& rhs); +bool operator<=(const MyString& lhs, const MyString& rhs); +bool operator<(const MyString& lhs, const MyString& rhs); + + +//MyString name = "sdfsdf" \ No newline at end of file diff --git a/Sem. 09/StringPool/Source.cpp b/Sem. 09/StringPool/Source.cpp index b478595..3f54a50 100644 --- a/Sem. 09/StringPool/Source.cpp +++ b/Sem. 09/StringPool/Source.cpp @@ -1 +1,28 @@ -s +#include + +#include "StringPool.h" + +int main() +{ + StringPool dict; + ((dict *= "Ivan") *= "Petur") *= "Alex"; + std::cout << dict << std::endl; // {Alex, Ivan, Petur} + std::cout << dict["Alex"] << " " << dict["abc"] << std::endl; // 1 0 + + StringPool dict2; + ((dict2 *= "Zebra") *= "Alex"); + + StringPool result = dict + dict2; + std::cout << result << std::endl; // {Alex, Ivan, Petur, Zebra} + + StringPool result2 = dict - dict2; + std::cout << result2 << std::endl; // {Ivan, Petur} + + "NewWOrd" >> result2; + result2 /= "NonExistingWord"; + std::cout << result2 << std::endl; //{Ivan, NewWOrd, Petur} + "Job" >> result2; + std::cout << result2 << std::endl; //{Ivan, Job, NewWOrd, Petur} + + +} \ No newline at end of file diff --git a/Sem. 09/StringPool/StringPool.cpp b/Sem. 09/StringPool/StringPool.cpp new file mode 100644 index 0000000..ffe8f5c --- /dev/null +++ b/Sem. 09/StringPool/StringPool.cpp @@ -0,0 +1,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(); +} \ No newline at end of file diff --git a/Sem. 09/StringPool/StringPool.h b/Sem. 09/StringPool/StringPool.h new file mode 100644 index 0000000..6be1715 --- /dev/null +++ b/Sem. 09/StringPool/StringPool.h @@ -0,0 +1,37 @@ +#pragma once +#include "MyString/MyString.h" + + +class StringPool +{ + MyString** data; + size_t count; + + void copyFrom(const StringPool& other); + void free(); + void resize(size_t newCount); + + int findWord(const MyString& str) const; + +public: + StringPool(); + + StringPool(const StringPool& other); + StringPool& operator=(const StringPool& other); + + StringPool& operator*=(const MyString& word); + StringPool& operator/=(const MyString& other); + + bool operator[](const MyString& str) const; + + friend std::ostream& operator<<(std::ostream&, const StringPool&); + friend void operator>>(const MyString&, StringPool&); + + StringPool& operator+=(const StringPool& other); + StringPool& operator-=(const StringPool& other); + + ~StringPool(); +}; + +StringPool operator+(const StringPool& lhs, const StringPool& rhs); +StringPool operator-(const StringPool& lhs, const StringPool& rhs); -- cgit v1.3.1-23-gd82d