Menu

[r147]: / trunk / include / dssmatrix- / dssmatrix-io.hpp  Maximize  Restore  History

Download this file

301 lines (263 with data), 9.6 kB

  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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//=============================================================================
/*! operator() for const object */
inline double dssmatrix::operator()(const long& i, const long& j) const
{VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( i<0 || j<0 || n<=i || n<=j ){
ERROR_REPORT;
std::cerr << "The required component is out of the matrix size." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
//// search (i,j) component ////
const uint32_t ii(std::max(i,j)), jj(std::min(i,j));
for(std::vector<uint32_t>::const_iterator p=line[ii].begin(); p!=line[ii].end(); p++){
if(data[*p].i==ii && data[*p].j==jj){ return data[*p].v; }
}
//// (i,j) component was not found ////
return 0.0;
}
//=============================================================================
/*! operator() for const object */
inline double& dssmatrix::operator()(const long& i, const long& j)
{VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( i<0 || j<0 || n<=i || n<=j ){
ERROR_REPORT;
std::cerr << "The required component is out of the matrix size." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
//////// search (i,j) component ////////
const uint32_t ii(std::max(i,j)), jj(std::min(i,j));
for(std::vector<uint32_t>::iterator p=line[ii].begin(); p!=line[ii].end(); p++){
if(data[*p].i==ii && data[*p].j==jj){ return data[*p].v; }
}
//////// (i,j) component not found ////////
line[ii].push_back(data.size());
if(i!=j){//off-diagonal
line[jj].push_back(data.size());
}
data.push_back(dcomponent(ii,jj,0.));
return data.back().v;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! put value with volume cheack without isListed check */
inline dssmatrix& dssmatrix::put(const long& i, const long& j, const double& v)
{VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( i<0 || j<0 || n<=i || n<=j ){
ERROR_REPORT;
std::cerr << "The required component is out of the matrix size." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
if( isListed(i,j) ){
ERROR_REPORT;
std::cerr << "The required component is already listed." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
//// push ////
const long ii(std::max(i,j)), jj(std::min(i,j));
line[ii].push_back(data.size());
if(i!=j){//off-diagonal
line[jj].push_back(data.size());
}
data.push_back(dcomponent(ii,jj,v));
return *this;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! delete the entry of a component */
inline dssmatrix& dssmatrix::del(const long i, const long j)
{VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( i<0 || j<0 || n<=i || n<=j ){
ERROR_REPORT;
std::cerr << "The required component is out of the matrix size." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
const long ii(std::max(i,j)), jj(std::min(i,j));
//////// search (i,j) component ////////
for(std::vector<uint32_t>::iterator p=line[ii].begin(); p!=line[ii].end(); p++){
if(long(data[*p].i)==ii && long(data[*p].j)==jj){//exists
//// save position ////
uint32_t c(*p);
uint32_t C(data.size()-1);
//// data translation ////
data[c]=data.back();
data.pop_back();
//// remove from List ////
line[ii].erase(p);
if(i!=j){//off-diagonal
for(std::vector<uint32_t>::iterator pj=line[jj].begin(); pj!=line[jj].end(); pj++){
if(*pj==c){ line[jj].erase(pj); break; }
}
}
//// modify the entry of translated component ////
uint32_t I(data[c].i), J(data[c].j);
for(std::vector<uint32_t>::iterator q=line[I].begin(); q!=line[I].end(); q++){
if(*q==C){ *q=c; break; }
}
if(I!=J){//off-diagonal
for(std::vector<uint32_t>::iterator q=line[J].begin(); q!=line[J].end(); q++){
if(*q==C){ *q=c; break; }
}
}
return *this;
}
}
#ifdef CPPL_DEBUG
std::cerr << "# [NOTE]@dssmatrix::del(long&, long&): The required component was not listed. Your input was (" << i << "," << j << ")." << std::endl;
#endif//CPPL_DEBUG
return *this;
}
//=============================================================================
/*! delete the entry of an element */
inline dssmatrix& dssmatrix::del(const long c)
{VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if( c<0 || c>=long(data.size()) ){
ERROR_REPORT;
std::cerr << "The required element is out of the matrix volume." << std::endl
<< "Your input was (" << c << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
if( c==long(data.size()-1) ){//if c is the last element
long i(data[c].i), j(data[c].j);
for(std::vector<uint32_t>::iterator q=line[i].begin(); q!=line[i].end(); q++){
if(long(*q)==c){ line[i].erase(q); break; }
}
if(i!=j){//off-diagonal
for(std::vector<uint32_t>::iterator q=line[j].begin(); q!=line[j].end(); q++){
if(long(*q)==c){ line[j].erase(q); break; }
}
}
data.pop_back();
}
else{//c is NOT the last element
//// data translation ////
uint32_t C(data.size()-1);
long i(data[c].i), j(data[c].j), I(data.back().i), J(data.back().j);
data[c]=data.back();
//std::cerr << "c=" << c << " i=" << i << " j=" << j << " C=" << vol << " I=" << I << " J=" << J << std::endl;
//// remove entry of component ////
for(std::vector<uint32_t>::iterator q=line[i].begin(); q!=line[i].end(); q++){
if(long(*q)==c){ line[i].erase(q); break; }
}
if(i!=j){//off-diagonal
for(std::vector<uint32_t>::iterator q=line[j].begin(); q!=line[j].end(); q++){
if(long(*q)==c){ line[j].erase(q); break; }
}
}
//// modify the entry of translated component ////
for(std::vector<uint32_t>::iterator q=line[I].begin(); q!=line[I].end(); q++){
if(*q==C){ *q=c; break; }
}
if(I!=J){//off-diagonal
for(std::vector<uint32_t>::iterator q=line[J].begin(); q!=line[J].end(); q++){
if(*q==C){ *q=c; break; }
}
}
//// pop_back ////
data.pop_back();
}
return *this;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
inline std::ostream& operator<<(std::ostream& s, const dssmatrix& mat)
{VERBOSE_REPORT;
for(long i=0; i<mat.n; i++){
for(long j=0; j<mat.n; j++){
if( i >= j ){
long c =mat.number(i,j);
if(c<0){
s << " x ";
}
else{
s << " " << mat.data[c].v << " ";
}
}
else{//i<j
long c =mat.number(i,j);
if(c<0){
s << "{x}";
}
else{
s << "{" << mat.data[c].v << "}";
}
}
}
s << std::endl;
}
return s;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
inline void dssmatrix::write(const char* filename) const
{VERBOSE_REPORT;
std::ofstream ofs(filename, std::ios::trunc);
ofs.setf(std::cout.flags());
ofs.precision(std::cout.precision());
ofs.width(std::cout.width());
ofs.fill(std::cout.fill());
ofs << "#dssmatrix" << " " << n << std::endl;
for(std::vector<dcomponent>::const_iterator it=data.begin(); it!=data.end(); it++){
ofs << it->i << " " << it->j << " " << it->v << std::endl;
}
ofs.close();
}
//=============================================================================
inline void dssmatrix::read(const char* filename)
{VERBOSE_REPORT;
std::ifstream s( filename );
if(!s){
ERROR_REPORT;
std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
exit(1);
}
std::string id;
s >> id;
if( id != "dssmatrix" && id != "#dssmatrix" ){
ERROR_REPORT;
std::cerr << "The type name of the file \"" << filename << "\" is not dssmatrix." << std::endl
<< "Its type name was " << id << " ." << std::endl;
exit(1);
}
s >> n;
resize(n);
double val;
long i, j, tmp;
std::streamoff pos;
while(!s.eof()){
s >> i >> j >> val;
put(i,j, val);
pos =s.tellg();
s >> tmp;
s.seekg(pos);
}
if(!s.eof()){
ERROR_REPORT;
std::cerr << "There is something is wrong with the file \"" << filename << " ." << std::endl
<< "Most likely, there are too many data components over the context." << std::endl;
exit(1);
}
s.close();
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.