-
Notifications
You must be signed in to change notification settings - Fork 5
/
vector.hpp
106 lines (73 loc) · 2.27 KB
/
vector.hpp
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
/*
This is vector.cpp
Coxeter version 3.0 Copyright (C) 2002 Fokko du Cloux
See file main.cpp for full copyright notice
*/
/****************************************************************************
This module contains the implementation of the vector template.
Just as for lists, we define vectors only for types where copy-constructors
are simply bitwise copy; i.e., vectors are really strings. Otheerwise,
a vector is a list of elements in a class for which ring operations
are defined.
****************************************************************************/
/****************************************************************************
Chapter I --- The Vector class
This section contains the definition of the functions for the Vector
class :
- operator+=(w) : adds w to the current vector;
- operator-=(w) : subtracts w from the current vector;
- operator*=(a) : multiplies the current vector by the scalar a;
- reduceDim() : reduces the dimension;
****************************************************************************/
namespace vector {
template <class T> Vector<T>& Vector<T>::operator+= (const Vector<T>& w)
/*
Operator += for Vectors always makes sure that there is enough
space.
*/
{
if (w.dim() > dim()) /* enlarge v if necessary and extend by zero */
setDim(w.dim());
for (Ulong j = 0; j < w.dim(); j++)
d_list[j] += w[j];
return *this;
}
template <class T> Vector<T>& Vector<T>::operator-= (const Vector<T>& w)
/*
Operator -= for Vectors always makes sure that there is enough
space.
*/
{
unsigned long j;
if (w.dim() > dim()) /* enlarge v if necessary and extend by zero */
setDim(w.dim());
for (Ulong j = 0; j < w.dim(); j++)
d_list[j] -= w[j];
return *this;
}
template <class T> Vector<T>& Vector<T>::operator*= (const T& a)
/*
Scalar multiplication operator.
*/
{
for (Ulong j = 0; j < dim(); j++)
d_list[j] *= a;
return *this;
}
template <class T> void Vector<T>::reduceDim()
/*
This function reduces the dimension to the smallest value that will contain
all non-zero coefficients in the current vector.
*/
{
for (Ulong j = dim(); j;) {
j--;
if (d_list[j]) {
d_list.setSize(j+1);
return;
}
}
d_list.setSize(0);
return;
}
};