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> &&
33  "Value type is not floating point");
34  assert(byte2 == std::is_signed_v<T> && "Value type is not signed");
35  assert(byte3 == columnMajor && "Major direction does not match");
36 }
37 
38 // performs some simple user checks on the matrices metadata
39 template <typename T, typename indexT, bool columnMajor>
40 void SparseMatrix<T, indexT, 1, columnMajor>::userChecks() {
41  assert((innerDim > 1 || outerDim > 1 || nnz > 1) &&
42  "The matrix must have at least one row, column, and nonzero value");
43  assert(std::is_floating_point<indexT>::value == false &&
44  "The index type must be a non-floating point type");
45  assert((std::is_arithmetic<T>::value && std::is_arithmetic<indexT>::value) &&
46  "The value and index types must be numeric types");
47  assert((std::is_same<indexT, bool>::value == false) &&
48  "The index type must not be bool");
49  assert((innerDim < std::numeric_limits<indexT>::max() &&
50  outerDim < std::numeric_limits<indexT>::max()) &&
51  "The number of rows and columns must be less than the maximum value "
52  "of the index type");
53  checkValueType();
54 }
55 
56 // Calculates the current byte size of the matrix in memory
57 template <typename T, typename indexT, bool columnMajor>
58 void SparseMatrix<T, indexT, 1, columnMajor>::calculateCompSize() {
59  // set compSize to zero
60  compSize = 0;
61 
62  // add the size of the metadata
63  compSize += META_DATA_SIZE;
64 
65  // add the csc vectors
66  compSize += sizeof(T) * nnz; // values
67  compSize += sizeof(indexT) * nnz; // innerIdx
68  compSize += sizeof(indexT) * (outerDim + 1); // outerPtr
69 }
70 
71 } // namespace IVSparse