add vector
This commit is contained in:
@ -15,7 +15,7 @@
|
||||
|
||||
## 评测方法
|
||||
|
||||
测试数据将全部下发(见本仓库),供调试使用。最终测试将会在OJ上进行,并进行code review。
|
||||
测试数据将全部下发(见本仓库),供调试使用。最终测试将会在 OJ 上进行,并进行 code review。
|
||||
|
||||
## 分数构成
|
||||
|
||||
@ -23,7 +23,9 @@
|
||||
|
||||
## 截止日期
|
||||
|
||||
待定
|
||||
`vector`:3 月 3 日(第二周周日)18:30 前
|
||||
|
||||
未完待续
|
||||
|
||||
## 迟交惩罚
|
||||
|
||||
|
19
vector/README.md
Normal file
19
vector/README.md
Normal file
@ -0,0 +1,19 @@
|
||||
# STLite-Vector
|
||||
|
||||
## 实现细节
|
||||
|
||||
Vector 的基本实现要求在总项目文档和`vector.hpp`中已经给出,最终提交仅需要提交`vector.hpp`的内容。
|
||||
|
||||
可能需要注意的细节:
|
||||
|
||||
- 在测试点中,有一些类并不具有默认构造函数,所以直接使用`T* p=new T[...];`可能会出现问题。
|
||||
- 你的程序将会受到一定程度的鲁棒性检测
|
||||
|
||||
## 分数构成
|
||||
|
||||
OJ 测试部分占比:80%,code review 部分占比:20%
|
||||
|
||||
## 截止日期
|
||||
|
||||
3 月 3 日(第二周周日)18:30 前
|
||||
|
516
vector/data/class-bint.hpp
Normal file
516
vector/data/class-bint.hpp
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
11
vector/data/class-integer.hpp
Normal file
11
vector/data/class-integer.hpp
Normal 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;
|
||||
}
|
||||
};
|
266
vector/data/class-matrix.hpp
Normal file
266
vector/data/class-matrix.hpp
Normal 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
|
22
vector/data/four.memcheck/answer.txt
Normal file
22
vector/data/four.memcheck/answer.txt
Normal 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.memcheck/code.cpp
Normal file
32
vector/data/four.memcheck/code.cpp
Normal 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();
|
||||
}
|
22
vector/data/four/answer.txt
Normal file
22
vector/data/four/answer.txt
Normal 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
32
vector/data/four/code.cpp
Normal 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();
|
||||
}
|
69
vector/data/one.memcheck/answer.txt
Normal file
69
vector/data/one.memcheck/answer.txt
Normal 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.memcheck/code.cpp
Normal file
132
vector/data/one.memcheck/code.cpp
Normal 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;
|
||||
}
|
69
vector/data/one/answer.txt
Normal file
69
vector/data/one/answer.txt
Normal 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
132
vector/data/one/code.cpp
Normal 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;
|
||||
}
|
80
vector/data/three.memcheck/answer.txt
Normal file
80
vector/data/three.memcheck/answer.txt
Normal 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
|
51
vector/data/three.memcheck/code.cpp
Normal file
51
vector/data/three.memcheck/code.cpp
Normal 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();
|
||||
}
|
80
vector/data/three/answer.txt
Normal file
80
vector/data/three/answer.txt
Normal 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
|
51
vector/data/three/code.cpp
Normal file
51
vector/data/three/code.cpp
Normal 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();
|
||||
}
|
1025
vector/data/two.memcheck/answer.txt
Normal file
1025
vector/data/two.memcheck/answer.txt
Normal file
File diff suppressed because it is too large
Load Diff
22
vector/data/two.memcheck/code.cpp
Normal file
22
vector/data/two.memcheck/code.cpp
Normal 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
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
22
vector/data/two/code.cpp
Normal 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;
|
||||
}
|
39
vector/src/exceptions.hpp
Normal file
39
vector/src/exceptions.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef SJTU_EXCEPTIONS_HPP
|
||||
#define SJTU_EXCEPTIONS_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace sjtu {
|
||||
|
||||
class exception {
|
||||
protected:
|
||||
const std::string variant = "";
|
||||
std::string detail = "";
|
||||
public:
|
||||
exception() {}
|
||||
exception(const exception &ec) : variant(ec.variant), detail(ec.detail) {}
|
||||
virtual std::string what() {
|
||||
return variant + " " + detail;
|
||||
}
|
||||
};
|
||||
|
||||
class index_out_of_bound : public exception {
|
||||
/* __________________________ */
|
||||
};
|
||||
|
||||
class runtime_error : public exception {
|
||||
/* __________________________ */
|
||||
};
|
||||
|
||||
class invalid_iterator : public exception {
|
||||
/* __________________________ */
|
||||
};
|
||||
|
||||
class container_is_empty : public exception {
|
||||
/* __________________________ */
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
27
vector/src/utility.hpp
Normal file
27
vector/src/utility.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef SJTU_UTILITY_HPP
|
||||
#define SJTU_UTILITY_HPP
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace sjtu {
|
||||
|
||||
template<class T1, class T2>
|
||||
class pair {
|
||||
public:
|
||||
T1 first;
|
||||
T2 second;
|
||||
constexpr pair() : first(), second() {}
|
||||
pair(const pair &other) = default;
|
||||
pair(pair &&other) = default;
|
||||
pair(const T1 &x, const T2 &y) : first(x), second(y) {}
|
||||
template<class U1, class U2>
|
||||
pair(U1 &&x, U2 &&y) : first(x), second(y) {}
|
||||
template<class U1, class U2>
|
||||
pair(const pair<U1, U2> &other) : first(other.first), second(other.second) {}
|
||||
template<class U1, class U2>
|
||||
pair(pair<U1, U2> &&other) : first(other.first), second(other.second) {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
226
vector/src/vector.hpp
Normal file
226
vector/src/vector.hpp
Normal file
@ -0,0 +1,226 @@
|
||||
#ifndef SJTU_VECTOR_HPP
|
||||
#define SJTU_VECTOR_HPP
|
||||
|
||||
#include "exceptions.hpp"
|
||||
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
|
||||
namespace sjtu
|
||||
{
|
||||
/**
|
||||
* a data container like std::vector
|
||||
* store data in a successive memory and support random access.
|
||||
*/
|
||||
template<typename T>
|
||||
class vector
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* TODO
|
||||
* a type for actions of the elements of a vector, and you should write
|
||||
* a class named const_iterator with same interfaces.
|
||||
*/
|
||||
/**
|
||||
* you can see RandomAccessIterator at CppReference for help.
|
||||
*/
|
||||
class const_iterator;
|
||||
class iterator
|
||||
{
|
||||
// The following code is written for the C++ type_traits library.
|
||||
// Type traits is a C++ feature for describing certain properties of a type.
|
||||
// For instance, for an iterator, iterator::value_type is the type that the
|
||||
// iterator points to.
|
||||
// STL algorithms and containers may use these type_traits (e.g. the following
|
||||
// typedef) to work properly. In particular, without the following code,
|
||||
// @code{std::sort(iter, iter1);} would not compile.
|
||||
// See these websites for more information:
|
||||
// https://en.cppreference.com/w/cpp/header/type_traits
|
||||
// About value_type: https://blog.csdn.net/u014299153/article/details/72419713
|
||||
// About iterator_category: https://en.cppreference.com/w/cpp/iterator
|
||||
public:
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using value_type = T;
|
||||
using pointer = T*;
|
||||
using reference = T&;
|
||||
using iterator_category = std::output_iterator_tag;
|
||||
|
||||
private:
|
||||
/**
|
||||
* TODO add data members
|
||||
* just add whatever you want.
|
||||
*/
|
||||
public:
|
||||
/**
|
||||
* return a new iterator which pointer n-next elements
|
||||
* as well as operator-
|
||||
*/
|
||||
iterator operator+(const int &n) const
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
iterator operator-(const int &n) const
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
// return the distance between two iterators,
|
||||
// if these two iterators point to different vectors, throw invaild_iterator.
|
||||
int operator-(const iterator &rhs) const
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
iterator& operator+=(const int &n)
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
iterator& operator-=(const int &n)
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
/**
|
||||
* TODO iter++
|
||||
*/
|
||||
iterator operator++(int) {}
|
||||
/**
|
||||
* TODO ++iter
|
||||
*/
|
||||
iterator& operator++() {}
|
||||
/**
|
||||
* TODO iter--
|
||||
*/
|
||||
iterator operator--(int) {}
|
||||
/**
|
||||
* TODO --iter
|
||||
*/
|
||||
iterator& operator--() {}
|
||||
/**
|
||||
* TODO *it
|
||||
*/
|
||||
T& operator*() const{}
|
||||
/**
|
||||
* a operator to check whether two iterators are same (pointing to the same memory address).
|
||||
*/
|
||||
bool operator==(const iterator &rhs) const {}
|
||||
bool operator==(const const_iterator &rhs) const {}
|
||||
/**
|
||||
* some other operator for iterator.
|
||||
*/
|
||||
bool operator!=(const iterator &rhs) const {}
|
||||
bool operator!=(const const_iterator &rhs) const {}
|
||||
};
|
||||
/**
|
||||
* TODO
|
||||
* has same function as iterator, just for a const object.
|
||||
*/
|
||||
class const_iterator
|
||||
{
|
||||
public:
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using value_type = T;
|
||||
using pointer = T*;
|
||||
using reference = T&;
|
||||
using iterator_category = std::output_iterator_tag;
|
||||
|
||||
private:
|
||||
/*TODO*/
|
||||
|
||||
};
|
||||
/**
|
||||
* TODO Constructs
|
||||
* At least two: default constructor, copy constructor
|
||||
*/
|
||||
vector() {}
|
||||
vector(const vector &other) {}
|
||||
/**
|
||||
* TODO Destructor
|
||||
*/
|
||||
~vector() {}
|
||||
/**
|
||||
* TODO Assignment operator
|
||||
*/
|
||||
vector &operator=(const vector &other) {}
|
||||
/**
|
||||
* assigns specified element with bounds checking
|
||||
* throw index_out_of_bound if pos is not in [0, size)
|
||||
*/
|
||||
T & at(const size_t &pos) {}
|
||||
const T & at(const size_t &pos) const {}
|
||||
/**
|
||||
* assigns specified element with bounds checking
|
||||
* throw index_out_of_bound if pos is not in [0, size)
|
||||
* !!! Pay attentions
|
||||
* In STL this operator does not check the boundary but I want you to do.
|
||||
*/
|
||||
T & operator[](const size_t &pos) {}
|
||||
const T & operator[](const size_t &pos) const {}
|
||||
/**
|
||||
* access the first element.
|
||||
* throw container_is_empty if size == 0
|
||||
*/
|
||||
const T & front() const {}
|
||||
/**
|
||||
* access the last element.
|
||||
* throw container_is_empty if size == 0
|
||||
*/
|
||||
const T & back() const {}
|
||||
/**
|
||||
* returns an iterator to the beginning.
|
||||
*/
|
||||
iterator begin() {}
|
||||
const_iterator cbegin() const {}
|
||||
/**
|
||||
* returns an iterator to the end.
|
||||
*/
|
||||
iterator end() {}
|
||||
const_iterator cend() const {}
|
||||
/**
|
||||
* checks whether the container is empty
|
||||
*/
|
||||
bool empty() const {}
|
||||
/**
|
||||
* returns the number of elements
|
||||
*/
|
||||
size_t size() const {}
|
||||
/**
|
||||
* clears the contents
|
||||
*/
|
||||
void clear() {}
|
||||
/**
|
||||
* inserts value before pos
|
||||
* returns an iterator pointing to the inserted value.
|
||||
*/
|
||||
iterator insert(iterator pos, const T &value) {}
|
||||
/**
|
||||
* inserts value at index ind.
|
||||
* after inserting, this->at(ind) == value
|
||||
* returns an iterator pointing to the inserted value.
|
||||
* throw index_out_of_bound if ind > size (in this situation ind can be size because after inserting the size will increase 1.)
|
||||
*/
|
||||
iterator insert(const size_t &ind, const T &value) {}
|
||||
/**
|
||||
* removes the element at pos.
|
||||
* return an iterator pointing to the following element.
|
||||
* If the iterator pos refers the last element, the end() iterator is returned.
|
||||
*/
|
||||
iterator erase(iterator pos) {}
|
||||
/**
|
||||
* removes the element with index ind.
|
||||
* return an iterator pointing to the following element.
|
||||
* throw index_out_of_bound if ind >= size
|
||||
*/
|
||||
iterator erase(const size_t &ind) {}
|
||||
/**
|
||||
* adds an element to the end.
|
||||
*/
|
||||
void push_back(const T &value) {}
|
||||
/**
|
||||
* remove the last element from the end.
|
||||
* throw container_is_empty if size() == 0
|
||||
*/
|
||||
void pop_back() {}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user