add vector

This commit is contained in:
csyer
2024-02-22 19:53:46 +08:00
parent b648f3923d
commit d65c91fee8
24 changed files with 3974 additions and 2 deletions

516
vector/data/class-bint.hpp Normal file
View File

@ -0,0 +1,516 @@
#include <string>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <stdexcept>
namespace Util {
const size_t MIN_CAPACITY = 2048;
class Bint {
class NewSpaceFailed : public std::runtime_error {
public:
NewSpaceFailed();
};
class BadCast : public std::invalid_argument {
public:
BadCast();
};
bool isMinus = false;
size_t length;
int *data = nullptr;
size_t capacity = MIN_CAPACITY;
void _DoubleSpace();
void _SafeNewSpace(int *&p, const size_t &len);
explicit Bint(const size_t &capa);
public:
Bint();
Bint(int x);
Bint(long long x);
Bint(std::string x);
Bint(const Bint &b);
Bint(Bint &&b) noexcept;
Bint &operator=(int rhs);
Bint &operator=(long long rhs);
Bint &operator=(const Bint &rhs);
Bint &operator=(Bint &&rhs) noexcept;
friend Bint abs(const Bint &x);
friend Bint abs(Bint &&x);
friend bool operator==(const Bint &lhs, const Bint &rhs);
friend bool operator!=(const Bint &lhs, const Bint &rhs);
friend bool operator<(const Bint &lhs, const Bint &rhs);
friend bool operator>(const Bint &lhs, const Bint &rhs);
friend bool operator<=(const Bint &lhs, const Bint &rhs);
friend bool operator>=(const Bint &lhs, const Bint &rhs);
friend Bint operator+(const Bint &lhs, const Bint &rhs);
friend Bint operator-(const Bint &b);
friend Bint operator-(Bint &&b);
friend Bint operator-(const Bint &lhs, const Bint &rhs);
friend Bint operator*(const Bint &lhs, const Bint &rhs);
friend std::istream &operator>>(std::istream &is, Bint &b);
friend std::ostream &operator<<(std::ostream &os, const Bint &b);
~Bint();
};
}
#include <iomanip>
#include <algorithm>
namespace Util {
Bint::NewSpaceFailed::NewSpaceFailed() : std::runtime_error("No Enough Memory Space.") {}
Bint::BadCast::BadCast() : std::invalid_argument("Cannot convert to a Bint object") {}
void Bint::_SafeNewSpace(int *&p, const size_t &len)
{
if (p != nullptr) {
delete[] p;
p = nullptr;
}
p = new int[len];
if (p == nullptr) {
throw NewSpaceFailed();
}
memset(p, 0, len * sizeof(unsigned int));
}
void Bint::_DoubleSpace()
{
int *newMem = nullptr;
_SafeNewSpace(newMem, capacity << 1);
memcpy(newMem, data, capacity * sizeof(int));
delete[] data;
data = newMem;
capacity <<= 1;
}
Bint::Bint()
: length(1)
{
_SafeNewSpace(data, capacity);
}
Bint::Bint(int x)
: length(0)
{
_SafeNewSpace(data, capacity);
if (x < 0) {
isMinus = true;
x = -x;
}
while (x) {
data[length++] = x % 10000;
x /= 10000;
}
if (!length) {
length = 1;
}
}
Bint::Bint(long long x)
: length(0)
{
_SafeNewSpace(data, capacity);
if (x < 0) {
isMinus = true;
x = -x;
}
while (x) {
data[length++] = static_cast<unsigned int>(x % 10000);
x /= 10000;
}
if (!length) {
length = 1;
}
}
Bint::Bint(const size_t &capa)
: length(1)
{
while (capacity < capa) {
capacity <<= 1;
}
_SafeNewSpace(data, capacity);
}
Bint::Bint(std::string x)
{
while (x[0] == '-') {
isMinus = !isMinus;
x.erase(0, 1);
}
while ((capacity << 2) <= x.length()) {
capacity <<= 1;
}
_SafeNewSpace(data, capacity);
size_t mid = x.length() >> 1;
for (size_t i = 0; i < mid; ++i) {
std::swap(x[i], x[x.length() - 1 - i]);
}
const static unsigned int pow10[4] = {1, 10, 100, 1000};
for (size_t i = 0; i < capacity; ++i) {
if ((i << 2) >= x.length()) {
length = i;
break;
}
for (size_t j = 0; j < 4; ++j) {
if ((i << 2) + j >= x.length()) {
break;
}
if (x[(i << 2) + j] > '9' || x[(i << 2) + j] < '0') {
throw BadCast();
}
data[i] = data[i] + (x[(i << 2) + j] - '0') * pow10[j];
}
}
}
Bint::Bint(const Bint &b)
: isMinus(b.isMinus), length(b.length), capacity(b.capacity)
{
_SafeNewSpace(data, capacity);
memcpy(data, b.data, sizeof(unsigned int) * capacity);
}
Bint::Bint(Bint &&b) noexcept
: isMinus(b.isMinus), length(b.length), capacity(b.capacity)
{
data = b.data;
b.data = nullptr;
}
Bint &Bint::operator=(int x)
{
memset(data, 0, sizeof(unsigned int) * capacity);
length = 0;
if (x < 0) {
isMinus = true;
x = -x;
}
while (x) {
data[length++] = x % 10000;
x /= 10000;
}
if (!length) {
length = 1;
}
return *this;
}
Bint &Bint::operator=(long long x)
{
memset(data, 0, sizeof(unsigned int) * capacity);
length = 0;
if (x < 0) {
isMinus = true;
x = -x;
}
while (x) {
data[length++] = static_cast<unsigned int>(x % 10000);
x /= 10000;
}
if (!length) {
length = 1;
}
return *this;
}
Bint &Bint::operator=(const Bint &rhs)
{
if (this == &rhs) {
return *this;
}
if (rhs.capacity > capacity) {
capacity = rhs.capacity;
_SafeNewSpace(data, capacity);
}
memcpy(data, rhs.data, sizeof(unsigned int) * rhs.capacity);
length = rhs.length;
isMinus = rhs.isMinus;
return *this;
}
Bint &Bint::operator=(Bint &&rhs) noexcept
{
if (this == &rhs) {
return *this;
}
capacity = rhs.capacity;
length = rhs.length;
isMinus = rhs.isMinus;
data = rhs.data;
rhs.data = nullptr;
return *this;
}
std::istream &operator>>(std::istream &is, Bint &b)
{
std::string s;
is >> s;
b = Bint(s);
return is;
}
std::ostream &operator<<(std::ostream &os, const Bint &b)
{
if (b.data == nullptr) {
return os;
}
if (b.isMinus && (b.length > 1 || b.data[0] != 0)) {
os << "-";
}
os << b.data[b.length - 1];
for (long long i = b.length - 2LL; i >= 0; --i) {
os << std::setw(4) << std::setfill('0') << b.data[i];
}
return os;
}
Bint abs(const Bint &b)
{
Bint result(b);
result.isMinus = false;
return result;
}
Bint abs(Bint &&b)
{
b.isMinus = false;
return b;
}
bool operator==(const Bint &lhs, const Bint &rhs)
{
if (lhs.isMinus != rhs.isMinus) {
return false;
}
if (lhs.length != rhs.length) {
return false;
}
for (size_t i = 0; i < lhs.length; ++i) {
if (lhs.data[i] != rhs.data[i]) {
return false;
}
}
return true;
}
bool operator!=(const Bint &lhs, const Bint &rhs)
{
if (lhs.isMinus != rhs.isMinus) {
return true;
}
if (lhs.length != rhs.length) {
return true;
}
for (size_t i = 0; i < lhs.length; ++i) {
if (lhs.data[i] != rhs.data[i]) {
return true;
}
}
return false;
}
bool operator<(const Bint &lhs, const Bint &rhs)
{
if (lhs.isMinus != rhs.isMinus) {
return !lhs.isMinus;
}
if (lhs.isMinus) {
if (lhs.length != rhs.length) {
return lhs.length > rhs.length;
}
for (long long i = lhs.length - 1; i >= 0; --i) {
if (lhs.data[i] != rhs.data[i]) {
return lhs.data[i] > rhs.data[i];
}
}
return false;
} else {
if (lhs.length != rhs.length) {
return lhs.length < rhs.length;
}
for (long long i = lhs.length - 1; i >= 0; --i) {
if (lhs.data[i] != rhs.data[i]) {
return lhs.data[i] < rhs.data[i];
}
}
return false;
}
}
bool operator>(const Bint &lhs, const Bint &rhs)
{
return rhs < lhs;
}
bool operator<=(const Bint &lhs, const Bint &rhs)
{
if (lhs.isMinus != rhs.isMinus) {
return !lhs.isMinus;
}
if (lhs.isMinus) {
if (lhs.length != rhs.length) {
return lhs.length > rhs.length;
}
for (long long i = lhs.length - 1; i >= 0; --i) {
if (lhs.data[i] != rhs.data[i]) {
return lhs.data[i] > rhs.data[i];
}
}
return true;
} else {
if (lhs.length != rhs.length) {
return lhs.length < rhs.length;
}
for (long long i = lhs.length - 1; i >= 0; --i) {
if (lhs.data[i] != rhs.data[i]) {
return lhs.data[i] < rhs.data[i];
}
}
return true;
}
}
bool operator>=(const Bint &lhs, const Bint &rhs)
{
if (lhs.isMinus != rhs.isMinus) {
return lhs.isMinus;
}
if (lhs.isMinus) {
if (lhs.length != rhs.length) {
return lhs.length < rhs.length;
}
for (long long i = lhs.length - 1; i >= 0; --i) {
if (lhs.data[i] != rhs.data[i]) {
return lhs.data[i] < rhs.data[i];
}
}
return true;
} else {
if (lhs.length != rhs.length) {
return lhs.length > rhs.length;
}
for (long long i = lhs.length - 1; i >= 0; --i) {
if (lhs.data[i] != rhs.data[i]) {
return lhs.data[i] > rhs.data[i];
}
}
return true;
}
}
Bint operator+(const Bint &lhs, const Bint &rhs)
{
if (lhs.isMinus == rhs.isMinus) {
size_t maxLen = std::max(lhs.length, rhs.length);
size_t expectLen = maxLen + 1;
Bint result(expectLen); // special constructor
for (size_t i = 0; i < maxLen; ++i) {
result.data[i] = lhs.data[i] + rhs.data[i];
}
for (size_t i = 0; i < maxLen; ++i) {
if (result.data[i] > 10000) {
result.data[i] -= 10000;
++result.data[i + 1];
}
}
result.length = result.data[maxLen] > 0 ? maxLen + 1 : maxLen;
result.isMinus = lhs.isMinus;
return result;
} else {
if (lhs.isMinus) {
return rhs - abs(lhs);
} else {
return lhs - abs(rhs);
}
}
}
Bint operator-(const Bint &b)
{
Bint result(b);
result.isMinus = !result.isMinus;
return result;
}
Bint operator-(Bint &&b)
{
b.isMinus = !b.isMinus;
return b;
}
Bint operator-(const Bint &lhs, const Bint &rhs)
{
if (lhs.isMinus == rhs.isMinus) {
if (lhs.isMinus) {
return -(abs(lhs) - abs(rhs));
} else {
if (lhs < rhs) {
return -(rhs - lhs);
}
Bint result(std::max(lhs.length, rhs.length));
for (size_t i = 0; i < lhs.length; ++i) {
result.data[i] = lhs.data[i] - rhs.data[i];
}
for (size_t i = 0; i < lhs.length; ++i) {
if (result.data[i] < 0) {
result.data[i] += 10000;
++result.data[i + 1];
}
}
while (result.length > 1 && result.data[result.length - 1] == 0) {
--result.length;
}
return result;
}
} else {
return lhs + (-rhs);
}
}
Bint operator*(const Bint &lhs, const Bint &rhs)
{
size_t expectLen = lhs.length + rhs.length + 2;
Bint result(expectLen);
for (size_t i = 0; i < lhs.length; ++i) {
for (size_t j = 0; j < rhs.length; ++j) {
long long tmp = result.data[i + j] + static_cast<long long>(lhs.data[i]) * rhs.data[j];
if (tmp >= 10000) {
result.data[i + j] = tmp % 10000;
result.data[i + j + 1] += static_cast<int>(tmp / 10000);
} else {
result.data[i + j] = tmp;
}
}
}
result.length = lhs.length + rhs.length -1;
while (result.data[result.length] > 0) {
++result.length;
}
while (result.length > 1 && result.data[result.length - 1] == 0) {
--result.length;
}
return result;
}
Bint::~Bint()
{
if (data != nullptr) {
delete[] data;
data = nullptr;
}
}
}

View File

@ -0,0 +1,11 @@
class Integer {
private:
int data;
public:
Integer(const int &value) : data(value) {}
Integer(const Integer &other) : data(other.data) {}
bool operator==(const Integer &t)
{
return data == t.data;
}
};

View File

@ -0,0 +1,266 @@
#ifndef DIAMOND_MATRIX_HPP
#define DIAMOND_MATRIX_HPP
#include <iostream>
#include <iomanip>
#include <vector>
#include <stdexcept>
namespace Diamond {
template<typename _Td>
class Matrix {
protected:
size_t n_rows = 0;
size_t n_cols = 0;
std::vector<std::vector<_Td>> data;
class RowProxy {
std::vector<_Td> &row;
public:
RowProxy(std::vector<_Td> & _row) : row(_row) {}
_Td & operator[](const size_t &pos)
{
return row[pos];
}
};
class ConstRowProxy {
const std::vector<_Td> &row;
public:
ConstRowProxy(const std::vector<_Td> &_row) : row(_row) {}
const _Td & operator[](const size_t &pos) const
{
return row[pos];
}
};
public:
Matrix() {};
Matrix(const size_t &_n_rows, const size_t &_n_cols)
: n_rows(_n_rows), n_cols(_n_cols), data(std::vector<std::vector<_Td>>(n_rows, std::vector<_Td>(n_cols))) {}
Matrix(const size_t &_n_rows, const size_t &_n_cols, const _Td &fillValue)
: n_rows(_n_rows), n_cols(_n_cols), data(std::vector<std::vector<_Td>>(n_rows, std::vector<_Td>(n_cols, fillValue))) {}
Matrix(const Matrix<_Td> &mat)
: n_rows(mat.n_rows), n_cols(mat.n_cols), data(mat.data) {}
Matrix(Matrix<_Td> &&mat) noexcept
: n_rows(mat.n_rows), n_cols(mat.n_cols), data(mat.data) {}
Matrix<_Td> & operator=(const Matrix<_Td> &rhs)
{
this->n_rows = rhs.n_rows;
this->n_cols = rhs.n_cols;
this->data = rhs.data;
return *this;
}
Matrix<_Td> & operator=(Matrix<_Td> &&rhs)
{
this->n_rows = rhs.n_rows;
this->n_cols = rhs.n_cols;
this->data = rhs.data;
return *this;
}
inline const size_t & RowSize() const
{
return n_rows;
}
inline const size_t & ColSize() const
{
return n_cols;
}
RowProxy operator[](const size_t &Kth)
{
return RowProxy(this->data[Kth]);
}
const ConstRowProxy operator[](const size_t &Kth) const
{
return ConstRowProxy(this->data[Kth]);
}
~Matrix() = default;
};
/**
* Sum of two matrics.
*/
template<typename _Td>
Matrix<_Td> operator+(const Matrix<_Td> &a, const Matrix<_Td> &b)
{
if (a.RowSize() != b.RowSize() || a.ColSize() != b.ColSize()) {
throw std::invalid_argument("different matrics\'s sizes");
}
Matrix<_Td> c(a.RowSize(), a.ColSize());
for (size_t i = 0; i < a.RowSize(); ++i) {
for (size_t j = 0; j < a.ColSize(); ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
return c;
}
template<typename _Td>
Matrix<_Td> operator-(const Matrix<_Td> &a, const Matrix<_Td> &b)
{
if (a.RowSize() != b.RowSize() || a.ColSize() != b.ColSize()) {
throw std::invalid_argument("different matrics\'s sizes");
}
Matrix<_Td> c(a.RowSize(), a.ColSize());
for (size_t i = 0; i < a.RowSize(); ++i) {
for (size_t j = 0; j < a.ColSize(); ++j) {
c[i][j] = a[i][j] - b[i][j];
}
}
return c;
}
template<typename _Td>
bool operator==(const Matrix<_Td> &a, const Matrix<_Td> &b)
{
if (a.RowSize() != b.RowSize() || a.ColSize() != b.ColSize()) {
return false;
}
for (size_t i = 0; i < a.RowSize(); ++i) {
for (size_t j = 0; j < a.ColSize(); ++j) {
if (a[i][j] != b[i][j])
return false;
}
}
return true;
}
template<typename _Td>
Matrix<_Td> operator-(const Matrix<_Td> &mat)
{
Matrix<_Td> result(mat.RowSize(), mat.ColSize());
for (size_t i = 0; i < mat.RowSize(); ++i) {
for (size_t j = 0; j < mat.ColSize(); ++j) {
result[i][j] = -mat[i][j];
}
}
return result;
}
template<typename _Td>
Matrix<_Td> operator-(Matrix<_Td> &&mat)
{
for (size_t i = 0; i < mat.RowSize(); ++i) {
for (size_t j = 0; j < mat.ColSize(); ++j) {
mat[i][j] = -mat[i][j];
}
}
return mat;
}
/**
* Multiplication of two matrics.
*/
template<typename _Td>
Matrix<_Td> operator*(const Matrix<_Td> &a, const Matrix<_Td> &b)
{
if (a.ColSize() != b.RowSize()) {
throw std::invalid_argument("different matrics\'s sizes");
}
Matrix<_Td> c(a.RowSize(), b.ColSize(), 0);
for (size_t i = 0; i < a.RowSize(); ++i) {
for (size_t j = 0; j < b.ColSize(); ++j) {
for (size_t k = 0; k < a.ColSize(); ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
/**
* Operations between a number and a matrix;
*/
template<typename _Td>
Matrix<_Td> operator*(const Matrix<_Td> &a, const _Td &b)
{
Matrix<_Td> c(a.RowSize(), a.ColSize());
for (size_t i = 0; i < a.RowSize(); ++i) {
for (size_t j = 0; j < a.ColSize(); ++j) {
c[i][j] = a[i][j] * b;
}
}
return c;
}
template<typename _Td>
Matrix<_Td> operator*(const _Td &b, const Matrix<_Td> &a)
{
Matrix<_Td> c(a.RowSize(), a.ColSize());
for (size_t i = 0; i < a.RowSize(); ++i) {
for (size_t j = 0; j < a.ColSize(); ++j) {
c[i][j] = a[i][j] * b;
}
}
return c;
}
template<typename _Td>
Matrix<_Td> operator/(const Matrix<_Td> &a, const double &b)
{
Matrix<_Td> c(a.RowSize(), a.ColSize());
for (size_t i = 0; i < a.RowSize(); ++i) {
for (size_t j = 0; j < a.ColSize(); ++j) {
c[i][j] = a[i][j] / b;
}
}
return c;
}
template<typename _Td>
Matrix<_Td> Transpose(const Matrix<_Td> &a)
{
Matrix<_Td> res(a.ColSize(), a.RowSize());
for (size_t i = 0; i < a.ColSize(); ++i) {
for (size_t j = 0; j < a.RowSize(); ++j) {
res[i][j] = a[j][i];
}
}
return res;
}
template<typename _Td>
std::ostream & operator<<(std::ostream &stream, const Matrix<_Td> &mat)
{
std::ostream::fmtflags oldFlags = stream.flags();
stream.precision(8);
stream.setf(std::ios::fixed | std::ios::right);
stream << '\n';
for (size_t i = 0; i < mat.RowSize(); ++i) {
for (size_t j = 0; j < mat.ColSize(); ++j) {
stream << std::setw(15) << mat[i][j];
}
stream << '\n';
}
stream.flags(oldFlags);
return stream;
}
template<typename _Td>
Matrix<_Td> I(const size_t &n)
{
Matrix<_Td> res(n, n, 0);
for (size_t i = 0; i < n; ++i) {
res[i][i] = static_cast<_Td>(1);
}
return res;
}
template<typename _Td>
Matrix<_Td> Pow(Matrix<_Td> A, size_t &b)
{
if (A.RowSize() != A.ColSize()) {
throw std::invalid_argument("The row size and column size are different.");
}
Matrix<_Td> result = I<_Td>(A.ColSize());
while (b > 0) {
if (b & static_cast<size_t>(1)) {
result = result * A;
}
A = A * A;
b = b >> static_cast<size_t>(1);
}
return result;
}
}
#endif

View File

@ -0,0 +1,22 @@
Supplementary test for large amounts of data ...
550658
15523
552292
13889
553926
12255
555560
10621
557194
8987
558828
7353
560462
5719
562096
4085
563730
2451
565364
817
Finished!

View File

@ -0,0 +1,32 @@
//provided by ivy
#include "vector.hpp"
#include "class-matrix.hpp"
#include "class-bint.hpp"
#include <iostream>
void complex_test()
{
std::cout << "Supplementary test for large amounts of data ..." << std::endl;
sjtu::vector<Diamond::Matrix<Util::Bint>> myVec;
for (int i = 1; i <= 1926; ++i)
myVec.push_back(Diamond::Matrix<Util::Bint>(i % 8 + 1, i % 17 + 1, Util::Bint(i * 817)));
int o = 1234;
while (o--)
myVec.pop_back();
myVec = myVec;
int _ = 20, __ = myVec.size();
while (_--)
{
if (_ % 2 == 0)
std::cout << myVec[_][0][0] << std::endl;
else
std::cout << myVec[__ - _][0][0] << std::endl;
}
std::cout << "Finished!" << std::endl;
}
int main()
{
complex_test();
}

View File

@ -0,0 +1,22 @@
Supplementary test for large amounts of data ...
550658
15523
552292
13889
553926
12255
555560
10621
557194
8987
558828
7353
560462
5719
562096
4085
563730
2451
565364
817
Finished!

32
vector/data/four/code.cpp Normal file
View File

@ -0,0 +1,32 @@
//provided by ivy
#include "vector.hpp"
#include "class-matrix.hpp"
#include "class-bint.hpp"
#include <iostream>
void complex_test()
{
std::cout << "Supplementary test for large amounts of data ..." << std::endl;
sjtu::vector<Diamond::Matrix<Util::Bint>> myVec;
for (int i = 1; i <= 1926; ++i)
myVec.push_back(Diamond::Matrix<Util::Bint>(i % 8 + 1, i % 17 + 1, Util::Bint(i * 817)));
int o = 1234;
while (o--)
myVec.pop_back();
myVec = myVec;
int _ = 20, __ = myVec.size();
while (_--)
{
if (_ % 2 == 0)
std::cout << myVec[_][0][0] << std::endl;
else
std::cout << myVec[__ - _][0][0] << std::endl;
}
std::cout << "Finished!" << std::endl;
}
int main()
{
complex_test();
}

View File

@ -0,0 +1,69 @@
Testing constructors and assignment operator...
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
Testing iterators...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Testing accessing methods...
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
4294967296
8589934592
17179869184
34359738368
68719476736
137438953472
274877906944
549755813888
1099511627776
2199023255552
4398046511104
8796093022208
17592186044416
35184372088832
70368744177664
140737488355328
281474976710656
562949953421312
exceptions thrown correctly.
Testing push_back and pop_back...
9
22
21
20
5
Testing insert functions
0 1 2 100 3 200 4 5 6 7 8 9
Testing erase functions
0 1 2 3 4 5 6 7 8 9

View File

@ -0,0 +1,132 @@
#include "vector.hpp"
#include <iostream>
#include <iomanip>
#include <vector>
void TestConstructor()
{
std::cout << "Testing constructors and assignment operator..." << std::endl;
sjtu::vector<int> v;
for (int i = 1; i <= 10; ++i) {
v.push_back(i);
}
const sjtu::vector<int> vc(v);
for (size_t i = 0; i < vc.size(); ++i) {
std::cout << vc[i] << " ";
}
std::cout << std::endl;
sjtu::vector<int> vv;
for (int i = 0; i < 10; ++i) {
vv.push_back(i);
}
for (size_t i = 0; i < vv.size(); ++i) {
std::cout << vv[i] << " ";
}
std::cout << std::endl;
vv = v;
for (size_t i = 0; i < vv.size(); ++i) {
std::cout << vv[i] << " ";
}
std::cout << std::endl;
}
void TestIterators()
{
std::cout << "Testing iterators..." << std::endl;
sjtu::vector<int> v;
for (int i = 1; i <= 20; ++i) {
v.push_back(i);
}
for (sjtu::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
const sjtu::vector<int> vc(v);
for (sjtu::vector<int>::const_iterator it = vc.cbegin(); it != vc.cend(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
void TestAccessingMethod()
{
std::cout << "Testing accessing methods..." << std::endl;
sjtu::vector<long long> vd;
for (long long i = 0; i < 50; ++i) {
vd.push_back(1LL << i);
}
for (size_t i = 0; i < vd.size(); ++i) {
std::cout << vd[i] << std::endl;
}
try {
std::cout << vd.at(100) << std::endl;
} catch(...) {
std::cout << "exceptions thrown correctly." << std::endl;
}
}
void TestPush_Pop()
{
std::cout << "Testing push_back and pop_back..." << std::endl;
sjtu::vector<double> vd;
for (double i = 0.0; i < 10.0; i += 1.0) {
vd.push_back(i);
}
std::cout << vd.back() << std::endl;
for (double i = 20.0; i < 23.0; i += 1.0) {
vd.push_back(i);
}
std::cout << vd.back() << std::endl;
vd.pop_back();
std::cout << vd.back() << std::endl;
vd.pop_back();
std::cout << vd.back() << std::endl;
for (int i = 0; i < 5; ++i) {
vd.pop_back();
}
std::cout << vd.back() << std::endl;
}
void TestInsert()
{
std::cout << "Testing insert functions" << std::endl;
sjtu::vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
v.insert(v.begin() + 3, 100);
v.insert(v.begin() + 5, 200);
for (sjtu::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
void TestErase()
{
std::cout << "Testing erase functions" << std::endl;
sjtu::vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
v.insert(v.begin() + 3, 100);
v.insert(v.begin() + 5, 200);
v.erase(v.begin() + 5);
v.erase(v.begin() + 3);
for (sjtu::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
int main(int argc, char const *argv[])
{
TestConstructor();
TestIterators();
TestAccessingMethod();
TestPush_Pop();
TestInsert();
TestErase();
return 0;
}

View File

@ -0,0 +1,69 @@
Testing constructors and assignment operator...
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
Testing iterators...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Testing accessing methods...
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
4294967296
8589934592
17179869184
34359738368
68719476736
137438953472
274877906944
549755813888
1099511627776
2199023255552
4398046511104
8796093022208
17592186044416
35184372088832
70368744177664
140737488355328
281474976710656
562949953421312
exceptions thrown correctly.
Testing push_back and pop_back...
9
22
21
20
5
Testing insert functions
0 1 2 100 3 200 4 5 6 7 8 9
Testing erase functions
0 1 2 3 4 5 6 7 8 9

132
vector/data/one/code.cpp Normal file
View File

@ -0,0 +1,132 @@
#include "vector.hpp"
#include <iostream>
#include <iomanip>
#include <vector>
void TestConstructor()
{
std::cout << "Testing constructors and assignment operator..." << std::endl;
sjtu::vector<int> v;
for (int i = 1; i <= 10; ++i) {
v.push_back(i);
}
const sjtu::vector<int> vc(v);
for (size_t i = 0; i < vc.size(); ++i) {
std::cout << vc[i] << " ";
}
std::cout << std::endl;
sjtu::vector<int> vv;
for (int i = 0; i < 10; ++i) {
vv.push_back(i);
}
for (size_t i = 0; i < vv.size(); ++i) {
std::cout << vv[i] << " ";
}
std::cout << std::endl;
vv = v;
for (size_t i = 0; i < vv.size(); ++i) {
std::cout << vv[i] << " ";
}
std::cout << std::endl;
}
void TestIterators()
{
std::cout << "Testing iterators..." << std::endl;
sjtu::vector<int> v;
for (int i = 1; i <= 20; ++i) {
v.push_back(i);
}
for (sjtu::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
const sjtu::vector<int> vc(v);
for (sjtu::vector<int>::const_iterator it = vc.cbegin(); it != vc.cend(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
void TestAccessingMethod()
{
std::cout << "Testing accessing methods..." << std::endl;
sjtu::vector<long long> vd;
for (long long i = 0; i < 50; ++i) {
vd.push_back(1LL << i);
}
for (size_t i = 0; i < vd.size(); ++i) {
std::cout << vd[i] << std::endl;
}
try {
std::cout << vd.at(100) << std::endl;
} catch(...) {
std::cout << "exceptions thrown correctly." << std::endl;
}
}
void TestPush_Pop()
{
std::cout << "Testing push_back and pop_back..." << std::endl;
sjtu::vector<double> vd;
for (double i = 0.0; i < 10.0; i += 1.0) {
vd.push_back(i);
}
std::cout << vd.back() << std::endl;
for (double i = 20.0; i < 23.0; i += 1.0) {
vd.push_back(i);
}
std::cout << vd.back() << std::endl;
vd.pop_back();
std::cout << vd.back() << std::endl;
vd.pop_back();
std::cout << vd.back() << std::endl;
for (int i = 0; i < 5; ++i) {
vd.pop_back();
}
std::cout << vd.back() << std::endl;
}
void TestInsert()
{
std::cout << "Testing insert functions" << std::endl;
sjtu::vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
v.insert(v.begin() + 3, 100);
v.insert(v.begin() + 5, 200);
for (sjtu::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
void TestErase()
{
std::cout << "Testing erase functions" << std::endl;
sjtu::vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
v.insert(v.begin() + 3, 100);
v.insert(v.begin() + 5, 200);
v.erase(v.begin() + 5);
v.erase(v.begin() + 3);
for (sjtu::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
int main(int argc, char const *argv[])
{
TestConstructor();
TestIterators();
TestAccessingMethod();
TestPush_Pop();
TestInsert();
TestErase();
return 0;
}

View File

@ -0,0 +1,80 @@
Test for classes without default constructor...
Test OK...
Test for my Matrix...
1.00000000
2.00000000 2.00000000
2.00000000 2.00000000
3.00000000 3.00000000 3.00000000
3.00000000 3.00000000 3.00000000
3.00000000 3.00000000 3.00000000
4.00000000 4.00000000 4.00000000 4.00000000
4.00000000 4.00000000 4.00000000 4.00000000
4.00000000 4.00000000 4.00000000 4.00000000
4.00000000 4.00000000 4.00000000 4.00000000
5.00000000 5.00000000 5.00000000 5.00000000 5.00000000
5.00000000 5.00000000 5.00000000 5.00000000 5.00000000
5.00000000 5.00000000 5.00000000 5.00000000 5.00000000
5.00000000 5.00000000 5.00000000 5.00000000 5.00000000
5.00000000 5.00000000 5.00000000 5.00000000 5.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
Test for big integer
1267650600228229401496703205376 1267650600228231653296516890625 1267650600228233905096330575876 1267650600228236156896144261129 1267650600228238408695957946384 1267650600228240660495771631641 1267650600228242912295585316900 1267650600228245164095399002161 1267650600228247415895212687424 1267650600228249667695026372689

View File

@ -0,0 +1,51 @@
#include "vector.hpp"
#include "class-integer.hpp"
#include "class-matrix.hpp"
#include "class-bint.hpp"
#include <iostream>
#include <fstream>
#include <string>
void TestInteger()
{
std::cout << "Test for classes without default constructor..." << std::endl;
sjtu::vector<Integer> vInt;
for (int i = 1; i <= 100; ++i) {
vInt.push_back(Integer(i));
}
std::cout << "Test OK..." << std::endl;
}
void TestMatrix()
{
std::cout << "Test for my Matrix..." << std::endl;
sjtu::vector<Diamond::Matrix<double>> vM;
for (int i = 1; i <= 10; ++i) {
vM.push_back(Diamond::Matrix<double>(i, i, i));
}
for (size_t i = 0; i < vM.size(); ++i) {
std::cout << vM[i] << std::endl;
}
}
void TestBint()
{
std::cout << "Test for big integer" << std::endl;
sjtu::vector<Util::Bint> vBint;
for (long long i = 1LL << 50; i < (1LL << 50) + 10; ++i) {
vBint.push_back(Util::Bint(i) * i);
}
for (sjtu::vector<Util::Bint>::iterator it = vBint.begin(); it != vBint.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
int main()
{
TestInteger();
TestMatrix();
TestBint();
}

View File

@ -0,0 +1,80 @@
Test for classes without default constructor...
Test OK...
Test for my Matrix...
1.00000000
2.00000000 2.00000000
2.00000000 2.00000000
3.00000000 3.00000000 3.00000000
3.00000000 3.00000000 3.00000000
3.00000000 3.00000000 3.00000000
4.00000000 4.00000000 4.00000000 4.00000000
4.00000000 4.00000000 4.00000000 4.00000000
4.00000000 4.00000000 4.00000000 4.00000000
4.00000000 4.00000000 4.00000000 4.00000000
5.00000000 5.00000000 5.00000000 5.00000000 5.00000000
5.00000000 5.00000000 5.00000000 5.00000000 5.00000000
5.00000000 5.00000000 5.00000000 5.00000000 5.00000000
5.00000000 5.00000000 5.00000000 5.00000000 5.00000000
5.00000000 5.00000000 5.00000000 5.00000000 5.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
6.00000000 6.00000000 6.00000000 6.00000000 6.00000000 6.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000 7.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000 8.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000 9.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000 10.00000000
Test for big integer
1267650600228229401496703205376 1267650600228231653296516890625 1267650600228233905096330575876 1267650600228236156896144261129 1267650600228238408695957946384 1267650600228240660495771631641 1267650600228242912295585316900 1267650600228245164095399002161 1267650600228247415895212687424 1267650600228249667695026372689

View File

@ -0,0 +1,51 @@
#include "vector.hpp"
#include "class-integer.hpp"
#include "class-matrix.hpp"
#include "class-bint.hpp"
#include <iostream>
#include <fstream>
#include <string>
void TestInteger()
{
std::cout << "Test for classes without default constructor..." << std::endl;
sjtu::vector<Integer> vInt;
for (int i = 1; i <= 100; ++i) {
vInt.push_back(Integer(i));
}
std::cout << "Test OK..." << std::endl;
}
void TestMatrix()
{
std::cout << "Test for my Matrix..." << std::endl;
sjtu::vector<Diamond::Matrix<double>> vM;
for (int i = 1; i <= 10; ++i) {
vM.push_back(Diamond::Matrix<double>(i, i, i));
}
for (size_t i = 0; i < vM.size(); ++i) {
std::cout << vM[i] << std::endl;
}
}
void TestBint()
{
std::cout << "Test for big integer" << std::endl;
sjtu::vector<Util::Bint> vBint;
for (long long i = 1LL << 50; i < (1LL << 50) + 10; ++i) {
vBint.push_back(Util::Bint(i) * i);
}
for (sjtu::vector<Util::Bint>::iterator it = vBint.begin(); it != vBint.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
int main()
{
TestInteger();
TestMatrix();
TestBint();
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,22 @@
#include "vector.hpp"
#include <iostream>
#include <vector>
int main()
{
sjtu::vector<long long> v;
for (long long i = 0; i < 1LL << 10; ++i) {
v.push_back(i);
}
std::cout << v.back() << std::endl;
for (long long i = 0; i < 1LL << 10; ++i) {
v.insert(v.begin(), i);
}
for (size_t i = 0; i < 1LL << 10; ++i) {
std::cout << v.front() << std::endl;
v.erase(v.begin());
}
return 0;
}

1025
vector/data/two/answer.txt Normal file

File diff suppressed because it is too large Load Diff

22
vector/data/two/code.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "vector.hpp"
#include <iostream>
#include <vector>
int main()
{
sjtu::vector<long long> v;
for (long long i = 0; i < 1LL << 20; ++i) {
v.push_back(i);
}
std::cout << v.back() << std::endl;
for (long long i = 0; i < 1LL << 11; ++i) {
v.insert(v.begin(), i);
}
for (size_t i = 0; i < 1LL << 10; ++i) {
std::cout << v.front() << std::endl;
v.erase(v.begin());
}
return 0;
}