Листинг файла TStack.cpp

#include "TStack.h"

#include

template TStack::TStack() : head(nullptr) {

}

template std::shared_ptr TStack::operator[](size_t i) {

if (i > size() - 1) throw std::invalid_argument("index greater then stack size");

size_t j = 0;

for (std::shared_ptr a : * this) {

if (j == i) return a;

j++;

}

return std::shared_ptr(nullptr);

}

template void TStack::sort() {

if (size() > 1) {

std::shared_ptr middle = pop();

TStack left, right;

while (!empty()) {

std::shared_ptr item = pop();

if (*item < *middle) {

left.push(item);

} else {

right.push(item);

}

}

left.sort();

right.sort();

while (!left.empty()) push(left.pop_last());

push(middle);

while (!right.empty()) push(right Листинг файла TStack.cpp.pop_last());

}

}

template std::future TStack::sort_in_background() {

std::packaged_task task(std::bind(std::mem_fn(&TStack::sort_parallel), this));

std::future res(task.get_future());

std::thread th(std::move(task));

th.detach();

return res;

}

template void TStack::sort_parallel() {

if (size() > 1) {

std::shared_ptr middle = pop_last();

TStack left, right;

while (!empty()) {

std::shared_ptr item = pop_last();

if (*item < *middle) {

left.push(item);

} else {

right.push(item);

}

}

std::future left_res = left.sort_in_background();

std::future right_res = right.sort_in_background();

left_res.get();

while (!left.empty()) push(left.pop_last());

push(middle Листинг файла TStack.cpp);

right_res.get();

while (!right.empty()) push(right.pop_last());

}

}

template std::ostream& operator<<(std::ostream& os, const TStack& stack) {

std::shared_ptr item = stack.head;

while (item != nullptr) {

os << *item;

item = item->GetNext();

}

return os;

}

template void TStack::push(T *item) {

std::shared_ptr other(new TStackItem(item));

other->SetNext(head);

head = other;

}

template void TStack::push(std::shared_ptr item) {

std::shared_ptr other(new TStackItem(item));

other->SetNext(head);

head = other;

}

template bool TStack::empty() {

return head == nullptr;

}

template std::shared_ptr TStack::pop() {

std::shared_ptr result;

if (head != nullptr) {

result = head->GetValue();

head = head->GetNext();

}

return result Листинг файла TStack.cpp;

}

template std::shared_ptr TStack::pop_last() {

std::shared_ptr result;

if (head != nullptr) {

std::shared_ptr element = head;

std::shared_ptr prev = nullptr;

while (element->GetNext() != nullptr) {

prev = element;

element = element->GetNext();

}

if (prev != nullptr) {

prev->SetNext(nullptr);

result = element->GetValue();

} else {

result = element->GetValue();

head = nullptr;

}

}

return result;

}

template size_t TStack::size() {

int result = 0;

for (auto i : * this) result++;

return result;

}

template TIterator TStack::begin() {

return TIterator(head);

}

template TIterator TStack::end() {

return TIterator(nullptr);

}

template TStack::~TStack() {

//std::cout << "Stack deleted" << std::endl;

}

#include "Triangle.h"

template class TStack;

template std::ostream& operator<<(std::ostream& os, const TStack& stack);


documentavzjpor.html
documentavzjwyz.html
documentavzkejh.html
documentavzkltp.html
documentavzktdx.html
Документ Листинг файла TStack.cpp