Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 08 / NVector / Nvector.cpp
blob: 0ef6d3aa755ab19e0203b8a1ac0d55f889c2e5df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "Nvector.h"

Nvector::Nvector(size_t n) : size(n)
{
	data = new int[n];

	for (int i = 0; i < n; i++)
		data[i] = 0;
}

Nvector::Nvector(const Nvector& other)
{
	copyFrom(other);
}

Nvector& Nvector::operator=(const Nvector& other)
{
	if (this != &other)
	{
		free();
		copyFrom(other);
	}
	return *this;
}
Nvector::~Nvector()
{
	free();
}

void Nvector::copyFrom(const Nvector& other)
{
	size = other.size;
	data = new int[size];

	for (int i = 0; i < size; i++)
		data[i] = other.data[i];
}
void Nvector::free()
{
	delete[] data;
	size = 0;
}



//v += v1;
Nvector& Nvector::operator+=(const Nvector& other)
{
	if (size != other.size)
		throw "The vectors should have the same size!";

	for (int i = 0; i < size; i++)
		data[i] += other.data[i];

	return *this;
}

Nvector& Nvector::operator-=(const Nvector& other)
{
	if (size != other.size)
		throw "The vectors should have the same size!";

	for (int i = 0; i < size; i++)
		data[i] -= other.data[i];
	
	return *this;
}

const Nvector operator+(const Nvector& lhs, const Nvector& rhs)
{
	Nvector lhsCopy(lhs);
	lhsCopy += rhs;

	return lhsCopy;
}

const Nvector operator-(const Nvector& lhs, const Nvector& rhs)
{
	Nvector lhsCopy(lhs);
	lhsCopy -= rhs;

	return lhsCopy;
}

int& Nvector::operator[](size_t index)
{
	if (index >= size)
		throw "Invalid index";

	return data[index];
}

int Nvector::operator[](size_t index) const
{
	if (index >= size)
		throw "Invalid index";

	return data[index];
}

size_t Nvector::operator~() const
{
	return size;
}

std::ostream& operator<<(std::ostream& os, const Nvector& v)
{
	os << "[ ";
	for (int i = 0; i < v.size; i++)
		os << v[i] << " ";
	os << " ]";

	return os;
}

std::istream& operator>>(std::istream& is, Nvector& v)
{
	for (int i = 0; i < v.size; i++)
		is >> v[i];

	return is;
}

bool operator||(const Nvector& lhs, const Nvector& rhs)
{
	if (~lhs != ~rhs)
		return false;

	double coef = (double)lhs[0] / rhs[0];

	double eps = 0.01;

	for (int i = 1; i < ~lhs; i++)
	{
		double currCoef = (double)lhs[i] / rhs[i];

		if (abs(currCoef - coef) > eps)
			return false;
	}
	return true;
}

Nvector& Nvector::operator*=(size_t scalar)
{
	for (int i = 0; i < size; i++)
		data[i] *= scalar;

	return *this;
}

const Nvector operator*(const Nvector& lhs, size_t scalar)
{
	Nvector copy(lhs);
	copy *= scalar;
	return copy;
}
const Nvector operator*(size_t scalar, const Nvector& lhs)
{
	return lhs * scalar;
}
size_t operator%(const Nvector& lhs, const Nvector& rhs)
{
	if (~lhs != ~rhs)
		throw "The vectors should have the same size!";

	size_t res = 0;
	for (int i = 0; ~lhs; i++)
		res += lhs[i] * rhs[i];

	return res;
}

size_t operator|=(const Nvector& lhs, const Nvector& rhs)
{
	return  lhs % rhs == 0; //the size check is done by %
}