SparseArray.h
#pragma once
#include<memory>
#include<iostream>
#include<iomanip>
using std::shared_ptr;
using std::make_shared;
using std::cout;
using std::setw;
using std::endl;
template<typename T>
class SparseArrayT
{
public:
class Node {
public:
size_t index{};
T* pObject;
shared_ptr<Node> pPrevious;
shared_ptr<Node> pNext;
public:
Node(size_t index) :index{ index }, pObject{ new T }{}
Node(const Node& a) :index{ a.index }, pObject{ new T(*a.pObject) }{}
void show() { cout << *pObject << " "; }
~Node(){ delete pObject; }
};
private:
shared_ptr<Node> pFirst;
shared_ptr<Node> pLast;
public:
SparseArrayT() = default;
SparseArrayT(const SparseArrayT&);
SparseArrayT& operator=(const SparseArrayT&);
T& operator[](size_t);
bool element_exists(size_t);
void list(bool flag = true);
};
template<typename T>
SparseArrayT<T>::SparseArrayT<T>(const SparseArrayT<T>& a) {
if (a.pFirst) {
pFirst = pLast = make_shared<Node>(*a.pFirst);
shared_ptr<Node> pTemp;
shared_ptr<Node> pCurrent = a.pFirst;
while (pCurrent = pCurrent->pNext) {
pTemp = pLast;
pLast = make_shared<Node>(*pCurrent);
pTemp->pNext = pLast;
pLast->pPrevious = pTemp;
}
}
}
template<typename T>
SparseArrayT<T>& SparseArrayT<T>::operator=(const SparseArrayT<T>& a) {
if (this == &a)
return *this;
if (a.pFirst) {
pFirst = pLast = make_shared<Node>(*a.pFirst);
shared_ptr<Node> pTemp;
shared_ptr<Node> pCurrent = a.pFirst;
while (pCurrent = pCurrent->pNext) {
pTemp = pLast;
pLast = make_shared<Node>(*pCurrent);
pTemp->pNext = pLast;
pLast->pPrevious = pTemp;
}
}
return *this;
}
template<typename T>
T& SparseArrayT<T>::operator[](size_t index) {
shared_ptr<Node> pCurrent = pFirst;
while (pCurrent) {
if (pCurrent->index == index)
return *pCurrent->pObject;
if (pCurrent->index > index)
break;
pCurrent = pCurrent->pNext;
}
shared_ptr<Node> pNode = make_shared<Node>(index);
if (pCurrent) {
if (pCurrent->pPrevious) {
pCurrent->pPrevious->pNext = pNode;
pNode->pPrevious = pCurrent->pPrevious;
pNode->pNext = pCurrent;
pCurrent->pPrevious = pNode;
}
else {
pCurrent->pPrevious = pNode;
pNode->pNext = pCurrent;
pFirst = pNode;
}
}
else {
if (pLast) {
pLast->pNext = pNode;
pNode->pPrevious = pLast;
pLast = pNode;
}
else {
pFirst = pLast = pNode;
}
}
return *pNode->pObject;
}
template<typename T>
void SparseArrayT<T>::list(bool flag) {
size_t count{};
size_t perline{ 5 };
if (flag) {
shared_ptr<Node> pCurrent = pFirst;
while (pCurrent) {
pCurrent->show();
pCurrent = pCurrent->pNext;
if (++count % perline == 0) cout << endl;
}
if (count % 5) cout << endl;
}
else {
shared_ptr<Node> pCurrent = pLast;
while (pCurrent) {
pCurrent->show();
pCurrent = pCurrent->pPrevious;
if (++count % perline == 0) cout << endl;
}
if (count % 5) cout << endl;
}
}
template<typename T>
bool SparseArrayT<T>::element_exists(size_t index) {
shared_ptr<Node> pCurrent = pFirst;
while (pCurrent) {
if (pCurrent->index == index) return true;
pCurrent = pCurrent->pNext;
}
return false;
}
main.cpp
#include"SparseArrayT.h"
#include<string>
using std::string;
void extract_words(SparseArrayT<string>& words, string& text) {
string separators{ " ,;:.\"!'\n" };
size_t start{ text.find_first_not_of(separators) };
size_t end;
size_t count{};
while (start != string::npos) {
end = text.find_first_of(separators, start + 1);
if (end == string::npos)
end = text.length();
words[count++] = text.substr(start, end - start);
start = text.find_first_not_of(separators, end + 1);
}
}
int main() {
SparseArrayT<string> words;
string text;
std::getline(std::cin, text, '#');
extract_words(words, text);
words.list(false);
return 0;
}