SparseLibrary  Version 1.6.0
HilbertTriplet.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 "Matrix2HilbertCoordinates.hpp"
35 #include<vector>
36 #include<string>
37 #include<fstream>
38 
39 #ifndef _H_HILBERT_TRIPLET
40 #define _H_HILBERT_TRIPLET
41 
45 template< typename T >
47 
48  protected:
49 
51  size_t row;
52 
54  size_t column;
55 
57  size_t hilbert1;
58 
60  size_t hilbert2;
61 
62  public:
63 
65  unsigned long int i() const { return row; }
66 
68  unsigned long int j() const { return column; }
69 
71  T value;
72 
78  HilbertTriplet( unsigned long int i, unsigned long int j, T val ): row( i ), column( j ), hilbert1( 0 ), hilbert2( 0 ), value( val ) {}
79 
81  HilbertTriplet(): row( 0 ), column( 0 ), hilbert1( 0 ), hilbert2( 0 ), value( 0 ) {}
82 
86  }
87 
96  void getHilbertCoordinate( size_t &h1, size_t &h2 ) {
97  h1 = hilbert1;
98  h2 = hilbert2;
99  }
100 
103  return hilbert1;
104  }
105 
108  return hilbert2;
109  }
110 
121  static void save( std::string fn, HilbertTriplet< T >* toWrite, const unsigned long int m, const unsigned long int n, const size_t s ) {
122  std::fstream myFile ( fn.c_str(), std::ios::out | std::ios::binary);
123  myFile.write( (char*) &m, sizeof( unsigned long int ) );
124  myFile.write( (char*) &n, sizeof( unsigned long int ) );
125  for( size_t i = 0; i<s; i++ ) {
126  const unsigned long int wi = toWrite[ i ].i();
127  const unsigned long int wj = toWrite[ i ].j();
128  const double wv = toWrite[ i ].value;
129 #ifdef _DEBUG
130  std::cout << "Wrote: ( " << wi << " , " << wj << " , " << wv << " ) " << std::endl;
131 #endif
132  myFile.write( (char*) &( wi ), sizeof( unsigned long int ) );
133  myFile.write( (char*) &( wj ), sizeof( unsigned long int ) );
134  myFile.write( (char*) &( wv ), sizeof( T ) );
135  }
136  myFile.close();
137  }
138 
148  static void save( std::string fn, std::vector< HilbertTriplet< T > > &toWrite, const unsigned long int m, const unsigned long int n ) {
149  save( fn, &( toWrite[ 0 ] ), m, n, toWrite.size() );
150  }
151 
152 };
153 
154 #endif
155 
Hilbert-coordinate-aware triplet.
Definition: HilbertTriplet.hpp:46
static void save(std::string fn, std::vector< HilbertTriplet< T > > &toWrite, const unsigned long int m, const unsigned long int n)
Saves a std::vector of Hilbert triplets to a file, in binary format.
Definition: HilbertTriplet.hpp:148
size_t getLeastSignificantHilbertBits()
Definition: HilbertTriplet.hpp:107
HilbertTriplet(unsigned long int i, unsigned long int j, T val)
Base constructor.
Definition: HilbertTriplet.hpp:78
T value
Value stored at this triplet.
Definition: HilbertTriplet.hpp:71
static void IntegerToHilbert(const size_t i, const size_t j, size_t &h1, size_t &h2)
New method, October 2010.
Definition: Matrix2HilbertCoordinates.cpp:48
size_t getMostSignificantHilbertBits()
Definition: HilbertTriplet.hpp:102
static void save(std::string fn, HilbertTriplet< T > *toWrite, const unsigned long int m, const unsigned long int n, const size_t s)
Saves an array of Hilbert triplets to a file, in binary format.
Definition: HilbertTriplet.hpp:121
size_t column
The column coordinate of this triplet.
Definition: HilbertTriplet.hpp:54
size_t hilbert1
Most significant part of a 128-bits Hilbert coordinate, for one-shot, non-iterative, calculation.
Definition: HilbertTriplet.hpp:57
void getHilbertCoordinate(size_t &h1, size_t &h2)
Gets the Hilbert coordinates.
Definition: HilbertTriplet.hpp:96
size_t row
The row coordinate of this triplet.
Definition: HilbertTriplet.hpp:51
unsigned long int i() const
Definition: HilbertTriplet.hpp:65
HilbertTriplet()
Base constructor.
Definition: HilbertTriplet.hpp:81
void calculateHilbertCoordinate()
Calculates the full Hilbert coordinate.
Definition: HilbertTriplet.hpp:84
size_t hilbert2
Least significant part of a 128-bits Hilbert coordinate, for one-shot, non-iterative, calculation.
Definition: HilbertTriplet.hpp:60
unsigned long int j() const
Definition: HilbertTriplet.hpp:68