-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtr_sparse.cpp
172 lines (148 loc) · 3.86 KB
/
vtr_sparse.cpp
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
#include <fstream>
#include "vtr_sparse.h"
VTR_SPARSE::VTR_SPARSE()
{
allocated = false;
value_number = 0;
}
// ==============================================================================
/**
\brief Konstruktor
\param n - delka vytvareneho vektoru
*/
VTR_SPARSE::VTR_SPARSE(const long& n){
allocated = false;
initialize(n);
}
// ==============================================================================
/**
\brief Ziska dalsi hodnotu v poradi
\param r - Vystupni hodnota
*/
void VTR_SPARSE::get_next_ac(long& r)
{
ac = index[ac];
r = ac;
}
// ==============================================================================
/**
\brief Zajisti vektor o delce l - melo by se pouzit jen jednou
\param l - pozadovana delka vektoru
*/
void VTR_SPARSE::initialize(const long &l)
{
long i;
if(!allocated){
length = l;
alloc();
}else if(length<l){
dealloc();
length = l;
alloc();
}
for(i=0; i<length; i++){//Nastav vychozi hodnoty
values[i] = 0;
index[i] = -1;
}
start = -2;
}
// ==============================================================================
/**
\brief Pretvori rikdou strukturu z v a ind do plne struktury pouzite ve VTR_SPARSE
\param v - Ridke pole hodnot
\param ind - Ridky zapis sloupcu kde jsou zapsany hodnoty
\param vn - Value number - kolik nenulovych hodnot je treba zapsat
\param l - length - plna delka radku
*/
void VTR_SPARSE::set_val_from_sparse(const double* v, const long* ind, const long& vn)
{
long i, n;
//PRIPRAV OBJEKT PRO ZAPIS
anul_val();
//ZAPIS HODNOTY DO POLI
start = ind[0];
value_number = vn;
for(i=0;i<vn;i++){
n = ind[i];
values[n] = v[i];
index[n] = ind[i+1];
}
index[n] = -2;// Posledni index oznamuje, ze je posledni
}
// ==============================================================================
/**
\brief Pricte hodnoty z ridkeho zapisu do VTR_SPARSE.
\param v - Ridke pole hodnot
\param ind - Ridky zapis sloupcu kde jsou zapsany hodnoty
\param vn - Value number - kolik nenulovych hodnot je treba zapsat
\param m - multiply - Volitelny parametr (vych. hod. = 1), roznasobi se jim hodnoty, ktere se pricitaji
*/
void VTR_SPARSE::add_val_from_sparse(const double* v, const long* ind, const long& vn, const double& m)
{
long i,l,n;
for (i=0; i<vn; i++){
n = ind[i];
if(index[n] != -1)
values[n] += v[i]*m;
else{//Hodnota jeste nebyla zadana
l = find_val_left(n);
index[n] = index[l];
index[l] = n;
values[n] = v[i]*m;
value_number++;
}
}
}
// ==============================================================================
/**
\brief Vytiskni pomoci printf.
*/
void VTR_SPARSE::print_full()
{
for(long i=0; i<length; i++){
if(index[i] == -1)
{
printf("0 \t");
}else{
printf("%1.6f \t",values[i]);
}
}
printf("\n");
}
// ==============================================================================
/**
\brief Nastavi vektor na zacatek - vsechny hodnoty val = 0 a indexy = -1
\warning Zde pada pri pruchodu z prikazove radky
*/
void VTR_SPARSE::anul_val()
{
long n = start;
long j;
while(n != -2){
j = index[n];
values[n] = 0;
index[n] = -1;
n=j;
}
value_number = 0;
}
// ==============================================================================
/**
\brief Alokuje potrebna pole
*/
void VTR_SPARSE::alloc()
{
values = new double[length];
index = new long [length];
allocated = true;
}
// ==============================================================================
/**
\brief Dealokuje potrebna pole
*/
void VTR_SPARSE::dealloc()
{
delete values;
delete index;
allocated = false;
}