Skip to content

Commit

Permalink
Further fixes for compilation warnings with weird index types.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Jul 28, 2023
1 parent d6cb425 commit cf36c04
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
7 changes: 4 additions & 3 deletions include/tatami/sparse/CompressedSparseMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../base/utils.hpp"
#include "primary_extraction.hpp"
#include "SparseSecondaryExtractorCore.hpp"
#include "../utils/ElementType.hpp"

#include <vector>
#include <algorithm>
Expand Down Expand Up @@ -91,7 +92,7 @@ class CompressedSparseMatrix : public Matrix<Value_, Index_> {
throw std::runtime_error("last element of 'indptrs' should be equal to length of 'indices'");
}

Stored<IndexStorage_> max_index = (row_ ? ncols : nrows);
ElementType<IndexStorage_> max_index = (row_ ? ncols : nrows);
for (size_t i = 1; i < indptrs.size(); ++i) {
auto start = indptrs[i- 1], end = indptrs[i];
if (end < start || end > last) {
Expand Down Expand Up @@ -299,8 +300,8 @@ class CompressedSparseMatrix : public Matrix<Value_, Index_> {
******* Secondary extraction ********
*************************************/
private:
typedef Stored<IndexStorage_> StoredIndex;
typedef Stored<PointerStorage_> StoredPointer;
typedef ElementType<IndexStorage_> StoredIndex;
typedef ElementType<PointerStorage_> StoredPointer;

struct SecondaryModifier {
static void increment(StoredPointer& ptr, const IndexStorage_&, StoredPointer) { ++ptr; }
Expand Down
5 changes: 3 additions & 2 deletions include/tatami/sparse/FragmentedSparseMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../base/utils.hpp"
#include "primary_extraction.hpp"
#include "SparseSecondaryExtractorCore.hpp"
#include "../utils/ElementType.hpp"

#include <vector>
#include <algorithm>
Expand Down Expand Up @@ -82,7 +83,7 @@ class FragmentedSparseMatrix : public Matrix<Value_, Index_> {
}
}

Stored<Stored<IndexVectorStorage_> > max_index = (row_ ? ncols : nrows);
ElementType<ElementType<IndexVectorStorage_> > max_index = (row_ ? ncols : nrows);
for (size_t i = 0, end = indices.size(); i < end; ++i) {
const auto& curv = values[i];
const auto& curi = indices[i];
Expand Down Expand Up @@ -288,7 +289,7 @@ class FragmentedSparseMatrix : public Matrix<Value_, Index_> {
*************************************/
private:
typedef typename std::remove_reference<decltype(std::declval<IndexVectorStorage_>()[0])>::type IndexStorage;
typedef Stored<IndexStorage> StoredIndex;
typedef ElementType<IndexStorage> StoredIndex;

struct SecondaryModifier {
static void increment(size_t& ptr, const IndexStorage&, size_t) { ++ptr; }
Expand Down
5 changes: 3 additions & 2 deletions include/tatami/sparse/SemiCompressedSparseMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../base/utils.hpp"
#include "utils.hpp"
#include "SparseSecondaryExtractorCore.hpp"
#include "../utils/ElementType.hpp"

#include <vector>
#include <algorithm>
Expand Down Expand Up @@ -111,8 +112,8 @@ class SemiCompressedSparseMatrix : public Matrix<Value_, Index_> {
IndexStorage_ indices;
PointerStorage_ indptrs;

typedef Stored<IndexStorage_> StoredIndex;
typedef Stored<PointerStorage_> StoredPointer;
typedef ElementType<IndexStorage_> StoredIndex;
typedef ElementType<PointerStorage_> StoredPointer;

public:
Index_ nrow() const { return nrows; }
Expand Down
3 changes: 0 additions & 3 deletions include/tatami/sparse/SparseSecondaryExtractorCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

namespace tatami {

template<class Storage_>
using Stored = typename std::remove_reference<decltype(std::declval<Storage_>()[0])>::type;

template<typename Index_, typename StoredIndex_, typename CustomPointer_, class CustomPointerModifier_>
struct SparseSecondaryExtractorCore {
protected:
Expand Down
7 changes: 4 additions & 3 deletions include/tatami/sparse/primary_extraction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "utils.hpp"
#include <utility>
#include <algorithm>
#include "../utils/ElementType.hpp"

namespace tatami {

Expand Down Expand Up @@ -36,11 +37,11 @@ std::pair<size_t, size_t> extract_primary_dimension(
auto eIt = indices.begin() + sparse_utils::get_upper_limit(indices, indptrs, i);

if (iIt != eIt) {
if (start > *iIt) {
if (start > static_cast<Index_>(*iIt)) {
iIt = std::lower_bound(iIt, eIt, start);
}

auto last = start + length;
ElementType<IndexStorage_> last = start + length;

// Comparing the one-past-the-last requested index with the last observed index at 'eIt'.
// If the former is less than the latter, then we need to do a binary search.
Expand Down Expand Up @@ -143,7 +144,7 @@ void primary_dimension(

Index_ counter = 0;
while (counter < length) {
auto current = subset[counter];
ElementType<IndexStorage_> current = subset[counter];

while (iIt != eIt && current > *iIt) {
++iIt;
Expand Down
21 changes: 21 additions & 0 deletions include/tatami/utils/ElementType.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef TATAMI_ELEMENTTYPE_HPP
#define TATAMI_ELEMENTTYPE_HPP

/**
* @file ElementType.hpp
* @brief Get type of elements in an array.
*/

namespace tatami {

/**
* @tparam Array_ Some array of values that are accessed with `[`.
*
* Extract the type of array elements, after stripping away references and `const`-ness.
*/
template<class Array_>
using ElementType = typename std::remove_cv<typename std::remove_reference<decltype(std::declval<Array_>()[0])>::type>::type;

}

#endif
17 changes: 13 additions & 4 deletions tests/src/sparse/CompressedSparseMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,19 @@ TEST(CompressedSparseMatrix, ConstructionFail) {

TEST(CompressedSparseMatrix, OddTypes) {
// Checking for compilation warnings here when the interface and storage types are different.
std::vector<uint8_t> values;
std::vector<uint16_t> indices;
std::vector<uint64_t> indptr(11);
tatami::CompressedSparseRowMatrix<double, int, decltype(values), decltype(indices), decltype(indptr)> rmat(10, 20, values, indices, indptr);
{
std::vector<uint8_t> values;
std::vector<uint16_t> indices;
std::vector<uint64_t> indptr(11);
tatami::CompressedSparseRowMatrix<double, int, decltype(values), decltype(indices), decltype(indptr)> rmat(10, 20, values, indices, indptr);
}

{
std::vector<uint8_t> values;
std::vector<uint32_t> indices; // Check for signed/unsigned comparisons with the interface index type.
std::vector<uint64_t> indptr(11);
tatami::CompressedSparseRowMatrix<double, int32_t, decltype(values), decltype(indices), decltype(indptr)> rmat(10, 20, values, indices, indptr);
}
}

/*************************************
Expand Down

0 comments on commit cf36c04

Please sign in to comment.