#ifndef _DOUBLEVECTOR #define _DOUBLEVECTOR #include "Pair.hpp" template class DoubleVector { T* first; unsigned firstCount; unsigned firstAlloc; T* second; unsigned secondCount; unsigned secondAlloc; void free(); void copyFrom(const DoubleVector& other); void firstResize(); void secondResize(); public: DoubleVector(); ~DoubleVector(); DoubleVector(const DoubleVector& other); DoubleVector& operator=(const DoubleVector& other); DoubleVector(DoubleVector&& other); DoubleVector& operator=(DoubleVector&& other); Pair Count() const; void FirstPushBack(const T& object); void SecondPushBack(const U& object); Pair operator[](unsigned index); const Pair operator[](unsigned index) const; DoubleVector& operator+=(const DoubleVector& rhs); }; template DoubleVector operator+(const DoubleVector& lhs, const DoubleVector& rhs); template void DoubleVector::free() { delete[] first; delete[] second; } template void DoubleVector::copyFrom(const DoubleVector& other) { firstCount = other.firstCount; firstAlloc = other.firstAlloc; first = new T[firstAlloc]; for (unsigned i = 0; i < firstCount; ++i) first[i] = other.first[i]; secondCount = other.secondCount; secondAlloc = other.secondAlloc; second = new T[secondAlloc]; for (unsigned i = 0; i < secondCount; ++i) second[i] = other.second[i]; } template void DoubleVector::firstResize() { firstAlloc = firstAlloc == 0 ? 8 : firstAlloc * 2; T* firstLarger = new T[firstAlloc]; for (unsigned i = 0; i < firstCount; ++i) firstLarger[i] = first[i]; delete[] first; first = firstLarger; } template void DoubleVector::secondResize() { secondAlloc = secondAlloc == 0 ? 8 : secondAlloc * 2; U* secondLarger = new U[secondAlloc]; for (unsigned i = 0; i < secondCount; ++i) secondLarger[i] = second[i]; delete[] second; second = secondLarger; } template DoubleVector::DoubleVector() : first(), firstAlloc(), firstCount(), second(), secondAlloc(), secondCount() { } template DoubleVector::~DoubleVector() { free(); } template DoubleVector::DoubleVector(const DoubleVector& other) { copyFrom(other); } template DoubleVector& DoubleVector::operator=(const DoubleVector& other) { if (this != &other) { free(); copyFrom(other); } return *this; } template DoubleVector::DoubleVector(DoubleVector&& other) { first = other.first; firstCount = other.firstCount; firstAlloc = other.firstAlloc; second = other.second; secondCount = other.secondCount; secondAlloc = other.secondAlloc; other.first = nullptr; other.firstCount = other.firstAlloc = 0; other.second = nullptr; other.secondCount = other.secondAlloc = 0; } template DoubleVector& DoubleVector::operator=(DoubleVector&& other) { if (this != &other) { free(); first = other.first; firstCount = other.firstCount; firstAlloc = other.firstAlloc; second = other.second; secondCount = other.secondCount; secondAlloc = other.secondAlloc; other.first = nullptr; other.firstCount = other.firstAlloc = 0; other.second = nullptr; other.secondCount = other.secondAlloc = 0; } return *this; } template Pair DoubleVector::Count() const { return Pair(firstCount, secondCount); } template void DoubleVector::FirstPushBack(const T& object) { if (firstCount >= firstAlloc) firstResize(); first[firstCount++] = object; } template void DoubleVector::SecondPushBack(const U& object) { if (secondCount >= secondAlloc) secondResize(); second[secondCount++] = object; } template Pair DoubleVector::operator[](unsigned index) { return Pair(first[index], second[index]); } template const Pair DoubleVector::operator[](unsigned index) const { return Pair(first[index], second[index]); } template DoubleVector& DoubleVector::operator+=(const DoubleVector& rhs) { for (unsigned i = 0; i < rhs.firstCount; ++i) FirstPushBack(rhs.first[i]); for (unsigned i = 0; i < rhs.secondCount; ++i) SecondPushBack(rhs.second[i]); return *this; } template DoubleVector operator+(const DoubleVector& lhs, const DoubleVector& rhs) { DoubleVector val(lhs); val += rhs; return val; } #endif /* _DOUBLEVECTOR */