Menu

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

Download this file

381 lines (336 with data), 13.0 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//=============================================================================
/*! operator() for const object */
inline double dssmatrix::operator()(const long& i, const long& j) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::operator()(const long&, const long&) const" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] dssmatrix::operator()(long, long)" << std::endl
<< "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 ////
for(std::vector< std::pair<long,long> >::iterator p=Line[i].begin(); p!=Line[i].end(); p++){
if(p->first==j){ return Array[p->second]; }
}
//// (i,j) component was not found ////
return 0.0;
}
//=============================================================================
/*! operator() for const object */
inline double& dssmatrix::operator()(const long& i, const long& j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::operator()(const long&, const long&) const"
<< std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] dssmatrix::operator()(long, long)" << std::endl
<< "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 ////////
for(std::vector< std::pair<long,long> >::iterator p=Line[i].begin(); p!=Line[i].end(); p++){
if(p->first==j){ return Array[p->second]; }
}
//////// (i,j) component not found ////////
//// vol check ////
if(VOL==CAP){
expand(CPPL_SECTOR_SIZE);
#ifdef CPPL_VERBOSE
std::cerr << "# [NOTE] dssmatrix::put(long&, long&, double&) " << "The matrix volume was automatically expanded." << std::endl;
#endif//CPPL_VERBOSE
}
//// push_back ////
const long ii(max(i,j)), jj(min(i,j));
Array[VOL]=0.0;
Indx[VOL]=ii;
Jndx[VOL]=jj;
Line[ii].push_back(std::pair<long,long>(jj,VOL));
if(i!=j){
Line[jj].push_back(std::pair<long,long>(ii,VOL));
}
VOL++;
return Array[VOL-1];
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! put value with volume cheack without isListed check */
inline void dssmatrix::put(const long& i, const long& j, const double& v)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::put(const long&, const long&, const double&)"
<< std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] dssmatrix::put(long&, long&, double&)" << std::endl
<< "The required component is out of the matrix size." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
for(long c=0; c<VOL; c++){
if(Indx[c]==i && Jndx[c]==j){
std::cerr << "[ERROR] dssmatrix::fput"
<< "(const long&, const long&, const double&)" << std::endl
<< "The required component is already listed." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
}
#endif//CPPL_DEBUG
//// vol check ////
if(VOL==CAP){
expand(CPPL_SECTOR_SIZE);
#ifdef CPPL_VERBOSE
std::cerr << "# [NOTE] dssmatrix::put(long&, long&, double&)" << "The matrix volume was automatically expanded." << std::endl;
#endif//CPPL_VERBOSE
}
//// push ////
const long ii(max(i,j)), jj(min(i,j));
Array[VOL]=v;
Indx[VOL]=ii;
Jndx[VOL]=jj;
Line[ii].push_back(std::pair<long,long>(jj,VOL));
if(i!=j){
Line[jj].push_back(std::pair<long,long>(ii,VOL));
}
VOL++;
}
//=============================================================================
/*! put value without isListed check and volume cheack */
inline void dssmatrix::fput(const long& i, const long& j, const double& v)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::fput(const long&, const long&, const double&)" << std::endl;
#endif//CPPL_VERBOSE
const long ii(max(i,j)), jj(min(i,j));
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] dssmatrix::fput(const long&, lconst ong&, const double&)" << std::endl
<< "The required component is out of the matrix size." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
if(VOL>=CAP){
std::cerr << "[ERROR] dssmatrix::fput(const long&, const long&, const double&)" << std::endl
<< "There is no free space to set a new entry." << std::endl;
exit(1);
}
for(long c=0; c<VOL; c++){
if(Indx[c]==ii && Jndx[c]==jj){
std::cerr << "[ERROR] dssmatrix::fput(const long&, const long&, const double&)" << std::endl
<< "The required component is already listed." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
}
#endif//CPPL_DEBUG
Array[VOL]=v;
Indx[VOL]=ii;
Jndx[VOL]=jj;
Line[ii].push_back(std::pair<long,long>(jj,VOL));
if(i!=j){
Line[jj].push_back(std::pair<long,long>(ii,VOL));
}
VOL++;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! delete the entry of a component */
inline void dssmatrix::del(const long i, const long j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::del(const long&, const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] dssmatrix::del(long&, long&, double&)" << std::endl
<< "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 ////////
for(std::vector< std::pair<long,long> >::iterator p=Line[i].begin(); p!=Line[i].end(); p++){
if(p->first==j){
VOL--;
long c(p->second);
Array[c]=Array[VOL];
Indx[c]=Indx[VOL];
Jndx[c]=Jndx[VOL];
//// remove the component ////
Line[i].erase(p);
if(i!=j){
for(std::vector< std::pair<long,long> >::iterator pj=Line[j].begin(); pj!=Line[i].end(); pj++){
if(pj->first==i){ Line[j].erase(pj); }
}
}
//// reset the moved ////
long I(Indx[c]), J(Jndx[c]);
for(std::vector< std::pair<long,long> >::iterator q=Line[I].begin(); q<=Line[I].end(); q++){
if(q->first==J){ q->second=c; break; }
}
if(I!=J){
for(std::vector< std::pair<long,long> >::iterator q=Line[J].begin(); q<=Line[J].end(); q++){
if(q->first==I){ q->second=c; break; }
}
}
return;
}
}
#ifdef CPPL_DEBUG
std::cerr << "# [NOTE] dssmatrix::del(long&, long&, double&) " << "The required component was not listed. " << "Your input was (" << i << "," << j << ")." << std::endl;
#endif//CPPL_DEBUG
}
//=============================================================================
/*! delete the entry of an element */
inline void dssmatrix::fdel(const long c)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::fdel(const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( c<0 || c>=VOL ){
std::cerr << "[ERROR] dssmatrix::fdel(const long&)" << std::endl
<< "The required element is out of the matrix volume." << std::endl
<< "Your input was (" << c << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
#ifdef CPPL_VERBOSE
std::cerr << "# [NOTE] dssmatrix::fdel(const long&)" << " The (" << Indx[c] << "," << Jndx[c] << ") component is going to be deleted." << std::endl;
#endif//CPPL_VERBOSE
if(c==VOL-1){//if c is the last element
VOL--;
long i(Indx[c]), j(Jndx[c]);
for(std::vector< std::pair<long,long> >::iterator q=Line[i].begin(); q<=Line[i].end(); q++){
if(q->first==j){ Line[i].erase(q); break; }
}
if(i!=j){
for(std::vector< std::pair<long,long> >::iterator q=Line[j].begin(); q<=Line[j].end(); q++){
if(q->first==i){ Line[j].erase(q); break; }
}
}
}
else{
VOL--;
long i(Indx[c]), j(Jndx[c]), I(Indx[VOL]), J(Jndx[VOL]);
Array[c]=Array[VOL];
Indx[c]=Indx[VOL];
Jndx[c]=Jndx[VOL];
//std::cerr << "c=" << c << " i=" << i << " j=" << j << " C=" << VOL << " I=" << I << " J=" << J << std::endl;
for(std::vector< std::pair<long,long> >::iterator q=Line[i].begin(); q<=Line[i].end(); q++){
if(q->first==j){ Line[i].erase(q); break; }
}
if(i!=j){
for(std::vector< std::pair<long,long> >::iterator q=Line[j].begin(); q<=Line[j].end(); q++){
if(q->first==i){ Line[j].erase(q); break; }
}
}
for(std::vector< std::pair<long,long> >::iterator q=Line[I].begin(); q<=Line[I].end(); q++){
if(q->first==J){ q->second=c; break; }
}
if(I!=J){
for(std::vector< std::pair<long,long> >::iterator q=Line[J].begin(); q<=Line[J].end(); q++){
if(q->first==I){ q->second=c; break; }
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
inline std::ostream& operator<<(std::ostream& s, const dssmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] operator<<(std::ostream&, const dssmatrix&)" << std::endl;
#endif//CPPL_VERBOSE
for(long i=0; i<mat.N; i++){
for(long j=0; j<mat.N; j++){
if( i >= j ){
std::vector< std::pair<long,long> >::iterator q;
for(q=mat.Line[i].begin(); q!=mat.Line[i].end(); q++){
if(q->first==j){ break; }
}
if(q!=mat.Line[i].end()){ s << " " << mat.Array[q->second] << " "; }
else{ s << " x "; }
}
else{//i<j
std::vector< std::pair<long,long> >::iterator q;
for(q=mat.Line[i].begin(); q!=mat.Line[i].end(); q++){
if(q->first==j){ break; }
}
if(q!=mat.Line[i].end()){ s << "{" << mat.Array[q->second] << "}"; }
else{ s << "{x}"; }
}
}
s << std::endl;
}
return s;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
inline void dssmatrix::write(const char* filename) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::write(const char*) const" << std::endl;
#endif//CPPL_VERBOSE
std::ofstream s(filename, std::ios::trunc);
s << "dssmatrix" << " " << N << " " << CAP << std::endl;
for(long c=0; c<VOL; c++){
s << Indx[c] << " " << Jndx[c] << " " << Array[c] << std::endl;
}
s.close();
}
//=============================================================================
inline void dssmatrix::read(const char* filename)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::read(const char*)" << std::endl;
#endif//CPPL_VERBOSE
std::ifstream s( filename );
if(!s){
std::cerr << "[ERROR] dssmatrix::read(const char*) " << std::endl
<< "The file \"" << filename << "\" can not be opened." << std::endl;
exit(1);
}
std::string id;
s >> id;
if( id != "dssmatrix" ){
std::cerr << "[ERROR] dssmatrix::read(const char*) " << std::endl
<< "The type name of the file \"" << filename << "\" is not dssmatrix." << std::endl
<< "Its type name was " << id << " ." << std::endl;
exit(1);
}
s >> N >> CAP;
resize(N, CAP);
double val;
long i, j, pos, tmp;
while(!s.eof() && VOL<CAP){
s >> i >> j >> val;
fput(i,j, val);
pos =s.tellg(); s >> tmp; s.seekg(pos);
}
if(!s.eof()){
std::cerr << "[ERROR] dssmatrix::read(const char*) " << std::endl
<< "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.