IVSparse  v1.0
A sparse matrix compression library.
CSC_Private_Methods.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 namespace IVSparse {
12 
13  // Encodes the value type of the matrix in a uint32_t
14  template <typename T, typename indexT, bool columnMajor>
15  void SparseMatrix<T, indexT, 1, columnMajor>::encodeValueType() {
16  uint8_t byte0 = sizeof(T);
17  uint8_t byte1 = std::is_floating_point<T>::value ? 1 : 0;
18  uint8_t byte2 = std::is_signed_v<T> ? 1 : 0;
19  uint8_t byte3 = columnMajor ? 1 : 0;
20 
21  val_t = (byte3 << 24) | (byte2 << 16) | (byte1 << 8) | byte0;
22  }
23 
24  // Checks if the value type is correct for the matrix
25  template <typename T, typename indexT, bool columnMajor>
26  void SparseMatrix<T, indexT, 1, columnMajor>::checkValueType() {
27  uint8_t byte0 = val_t & 0xFF;
28  uint8_t byte1 = (val_t >> 8) & 0xFF;
29  uint8_t byte2 = (val_t >> 16) & 0xFF;
30  uint8_t byte3 = (val_t >> 24) & 0xFF;
31  assert(byte0 == sizeof(T) && "Value type size does not match");
32  assert(byte1 == std::is_floating_point_v<T> && "Value type is not floating point");
33  assert(byte2 == std::is_signed_v<T> && "Value type is not signed");
34  assert(byte3 == columnMajor && "Major direction does not match");
35  }
36 
37  // performs some simple user checks on the matrices metadata
38  template <typename T, typename indexT, bool columnMajor>
39  void SparseMatrix<T, indexT, 1, columnMajor>::userChecks() {
40  assert((innerDim > 1 || outerDim > 1 || nnz > 1) && "The matrix must have at least one row, column, and nonzero value");
41  assert(std::is_floating_point<indexT>::value == false && "The index type must be a non-floating point type");
42  assert((std::is_arithmetic<T>::value && std::is_arithmetic<indexT>::value) && "The value and index types must be numeric types");
43  assert((std::is_same<indexT, bool>::value == false) && "The index type must not be bool");
44  assert((innerDim < std::numeric_limits<indexT>::max() && outerDim < std::numeric_limits<indexT>::max()) && "The number of rows and columns must be less than the maximum value of the index type");
45  checkValueType();
46  }
47 
48  // Calculates the current byte size of the matrix in memory
49  template <typename T, typename indexT, bool columnMajor>
50  void SparseMatrix<T, indexT, 1, columnMajor>::calculateCompSize() {
51  // set compSize to zero
52  compSize = 0;
53 
54  // add the size of the metadata
55  compSize += META_DATA_SIZE;
56 
57  // add the csc vectors
58  compSize += sizeof(T) * nnz; // values
59  compSize += sizeof(indexT) * nnz; // innerIdx
60  compSize += sizeof(indexT) * (outerDim + 1); // outerPtr
61  }
62 
63 } // namespace IVSparse