SparseLibrary  Version 1.6.0
Triplet.hpp
1 /*
2  * Copyright (c) 2007-2014, A. N. Yzelman, Utrecht University 2007-2011;
3  * KU Leuven 2011-2014.
4  * R. H. Bisseling, Utrecht University 2007-2014.
5  *
6  * This file is part of the Sparse Library.
7  *
8  * This library was developed under supervision of Prof. dr. Rob H. Bisseling at
9  * Utrecht University, from 2007 until 2011. From 2011-2014, development continued
10  * at KU Leuven, where Prof. dr. Dirk Roose contributed significantly to the ideas
11  * behind the newer parts of the library code.
12  *
13  * The Sparse Library is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by the
15  * Free Software Foundation, either version 3 of the License, or (at your
16  * option) any later version.
17  *
18  * The Sparse Library is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with the Sparse Library. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 
28 /*
29  * File created by:
30  * A. N. Yzelman, Dept. of Mathematics, Utrecht University, 2007.
31  */
32 
33 
34 #include<cstdlib>
35 #include<string>
36 #include<vector>
37 #include<fstream>
38 #include<iostream>
39 
40 //#define _DEBUG
41 
42 #include "ULIdef.hpp"
43 
44 #ifndef _H_TRIPLET
45 #define _H_TRIPLET
46 
51 template< typename T >
52 class Triplet {
53 
54  protected:
55 
57  ULI row;
58 
60  ULI column;
61 
62  public:
63 
64 #ifdef TRIPLET_META
65 
66  TRIPLET_META_TYPE meta;
67 #endif
68 
70  ULI i() const { return row; }
71 
73  ULI j() const { return column; }
74 
76  void rowOffset( ULI offset ) { row -= offset; }
77 
79  void setPosition( const unsigned long int i, const unsigned long int j ) {
80  row = i;
81  column = j;
82  }
83 
85  void setRowPosition( const unsigned long int i ) {
86  setPosition( i, column );
87  }
88 
90  void setColumnPosition( const unsigned long int j ) {
91  setPosition( row, j );
92  }
93 
95  T value;
96 
104  Triplet( ULI ii, ULI ij, T val ): row( ii ), column( ij ), value( val ) {}
105 
107  Triplet(): row( 0 ), column( 0 ), value( 0 ) {}
108 
110  Triplet( const Triplet< T > &toCopy ): row( toCopy.i() ), column( toCopy.j() ), value( toCopy.value ) {
111 #ifdef TRIPLET_META
112  meta = toCopy.meta;
113 #endif
114  }
115 
123  static std::vector< Triplet< T > > load( const std::string fn, ULI &m, ULI &n ) {
124  std::fstream file( fn.c_str(), std::ios::in | std::ios::binary );
125  ULI i; ULI j; double v;
126  std::vector< Triplet< T > > ret;
127  if( !file.is_open() ) {
128  std::cerr << "Error while opening file" << std::endl;
129  exit( 1 );
130  }
131  file.read( (char*) &i, sizeof( ULI ) );
132  file.read( (char*) &j, sizeof( ULI ) );
133  m = i;
134  n = j;
135 #ifdef _DEBUG
136  std::cout << "m: " << m << ", n: " << n << std::endl;
137 #endif
138  while( true ) {
139  file.read( (char*) &i, sizeof( ULI ) );
140  if( !file ) break;
141  file.read( (char*) &j, sizeof( ULI ) );
142  file.read( (char*) &v, sizeof( T ) );
143 #ifdef _DEBUG
144  std::cout << "Pushed back: ( " << i << " , " << j << " , " << v << " )" << std::endl;
145 #endif
146  ret.push_back( Triplet< T >( i, j, v ) );
147  }
148  file.close();
149  return ret;
150  }
151 
160  static std::vector< Triplet< T > > loadCRS( const std::string fn, ULI &m, ULI &n ) {
161  std::fstream file( fn.c_str(), std::ios::in );
162 
163  if ( !file.is_open() ) {
164  std::cerr << "Error while opening file" << std::endl;
165  exit( 1 );
166  }
167 
168  ULI nonzeroes;
169  file >> m;
170  file >> n;
171  file >> nonzeroes;
172 
173  std::vector< ULI > row_start( m+1 );
174  for( size_t i = 0; i < m+1; ++i )
175  file >> row_start[ i ];
176 
177  std::vector< ULI > cols( nonzeroes );
178  for( size_t j = 0; j < nonzeroes; ++j )
179  file >> cols[ j ];
180 
181  T curval;
182  ULI currow = 0;
183  std::vector< Triplet< T > > ret;
184 
185  for( size_t k = 0; k < nonzeroes; ++k ) {
186  while( k == row_start[ currow + 1 ] ) {
187  currow++;
188  if( currow > m + 1 ) {
189  std::cerr << "Error in CRS file" << std::endl;
190  exit( 1 );
191  }
192  }
193  file >> curval;
194  ret.push_back( Triplet< T >( currow, cols[ k ], curval ) );
195  }
196 
197  file.close();
198 
199  return ret;
200  }
201 
210  static void save( std::string fn, Triplet< T >* toWrite, const ULI m, const ULI n, const ULI s ) {
211  std::fstream myFile ( fn.c_str(), std::ios::out | std::ios::binary);
212  myFile.write( (char*) &m, sizeof( ULI ) );
213  myFile.write( (char*) &n, sizeof( ULI ) );
214  for( unsigned long int i = 0; i<s; i++ ) {
215  const ULI wi = toWrite[ i ].i();
216  const ULI wj = toWrite[ i ].j();
217  const double wv = toWrite[ i ].value;
218 #ifdef _DEBUG
219  std::cout << "Wrote: ( " << wi << " , " << wj << " , " << wv << " ) " << std::endl;
220 #endif
221  myFile.write( (char*) &( wi ), sizeof( ULI ) );
222  myFile.write( (char*) &( wj ), sizeof( ULI ) );
223  myFile.write( (char*) &( wv ), sizeof( T ) );
224  }
225  myFile.close();
226  }
227 
229  void transpose() { const ULI t = row; row = column; column = t; }
230 
238  static void save( std::string fn, std::vector< Triplet< T > > &toWrite, const ULI m, const ULI n ) {
239  save( fn, &( toWrite[ 0 ] ), m, n, toWrite.size() );
240  }
241 
242 };
243 
244 #endif
245 
static std::vector< Triplet< T > > load(const std::string fn, ULI &m, ULI &n)
Loads an array of triplets from a binary file.
Definition: Triplet.hpp:123
void setRowPosition(const unsigned long int i)
Set the row coordinate of this nonzero.
Definition: Triplet.hpp:85
static void save(std::string fn, Triplet< T > *toWrite, const ULI m, const ULI n, const ULI s)
Saves an array of triplets to a file, in binary format.
Definition: Triplet.hpp:210
Triplet()
Base constructor.
Definition: Triplet.hpp:107
void setPosition(const unsigned long int i, const unsigned long int j)
Set the coordinates of this nonzero.
Definition: Triplet.hpp:79
ULI row
The row coordinate of this triplet.
Definition: Triplet.hpp:57
ULI i() const
Definition: Triplet.hpp:70
Triplet(const Triplet< T > &toCopy)
Copy constructor.
Definition: Triplet.hpp:110
Triplet(ULI ii, ULI ij, T val)
Base constructor.
Definition: Triplet.hpp:104
static void save(std::string fn, std::vector< Triplet< T > > &toWrite, const ULI m, const ULI n)
Saves a std::vector of triplets to a file, in binary format.
Definition: Triplet.hpp:238
ULI column
The column coordinate of this triplet.
Definition: Triplet.hpp:60
void transpose()
Transposes this triplet, i.e., swapping the row and column value.
Definition: Triplet.hpp:229
T value
Value stored at this triplet.
Definition: Triplet.hpp:95
static std::vector< Triplet< T > > loadCRS(const std::string fn, ULI &m, ULI &n)
Loads a CRS text file and transforms it into a vector of Triplets.
Definition: Triplet.hpp:160
void rowOffset(ULI offset)
Subtracts a given value from this nonzero row index.
Definition: Triplet.hpp:76
ULI j() const
Definition: Triplet.hpp:73
A single triplet value.
Definition: Triplet.hpp:52
void setColumnPosition(const unsigned long int j)
Set the column coordinate of this nonzero.
Definition: Triplet.hpp:90