Skip to content

Commit f15d5d1

Browse files
committed
portable fix
1 parent c274386 commit f15d5d1

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

inst/include/wrappers/matrices.hpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,23 @@ inline doubles_matrix<> as_doubles_matrix(const Mat<double>& A) {
144144
}
145145

146146
inline integers_matrix<> as_integers_matrix(const Mat<int>& A) {
147+
// Fast path: int to int
147148
return Mat_to_dblint_matrix_<int, integers_matrix<>>(A);
148149
}
149150

151+
inline integers_matrix<> as_integers_matrix(const Mat<long long>& A) {
152+
// Explicit cast for long long to int
153+
const int n = A.n_rows;
154+
const int m = A.n_cols;
155+
writable::integers_matrix<> B(n, m);
156+
int* B_data = INTEGER(B);
157+
const long long* A_data = A.memptr();
158+
for (int i = 0; i < n * m; ++i) {
159+
B_data[i] = static_cast<int>(A_data[i]);
160+
}
161+
return B;
162+
}
163+
150164
// Convert umat/imat to integers_matrix<>
151165

152166
template <typename T>
@@ -175,17 +189,6 @@ inline integers_matrix<> as_integers_matrix(const umat& A) {
175189
return as_integers_matrix_template(A);
176190
}
177191

178-
// Always provide for Mat<int>
179-
inline integers_matrix<> as_integers_matrix(const Mat<int>& A) {
180-
return Mat_to_dblint_matrix_<int, integers_matrix<>>(A);
181-
}
182-
// On 64-bit word systems, imat is Mat<long>
183-
#if defined(ARMA_64BIT_WORD)
184-
inline integers_matrix<> as_integers_matrix(const Mat<long>& A) {
185-
return Mat_to_dblint_matrix_<long, integers_matrix<>>(A);
186-
}
187-
#endif
188-
189192
// Complex
190193

191194
template <typename T>

inst/include/wrappers/sparse_matrices.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,6 @@ inline integers_matrix<> as_integers_matrix(const SpMat<int>& A) {
131131
return SpMat_to_dblint_matrix_<int, integers_matrix<>>(A);
132132
}
133133

134-
// On 64-bit word systems, SpMat<sword> is SpMat<long>
135-
#if defined(ARMA_64BIT_WORD)
136-
inline integers_matrix<> as_integers_matrix(const SpMat<long>& A) {
137-
return SpMat_to_dblint_matrix_<long, integers_matrix<>>(A);
138-
}
139-
#endif
140-
141134
inline doubles_matrix<> as_doubles_matrix(const SpMat<float>& A) {
142135
SpMat<double> B = arma::conv_to<SpMat<double>>::from(A);
143136
return as_doubles_matrix(B);

inst/include/wrappers/vectors.hpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,21 @@ inline U Col_to_dblint_(const Col<T>& x) {
114114
}
115115

116116
inline integers as_integers(const Col<int>& x) {
117+
// Fast path: int to int
117118
return Col_to_dblint_<int, integers>(x);
118119
}
119120

121+
inline integers as_integers(const Col<long long>& x) {
122+
// Explicit cast for long long to int
123+
const size_t n = x.n_elem;
124+
writable::integers y(n);
125+
const long long* x_data = x.memptr();
126+
for (size_t i = 0; i < n; ++i) {
127+
y[i] = static_cast<int>(x_data[i]);
128+
}
129+
return y;
130+
}
131+
120132
inline doubles as_doubles(const Col<double>& x) {
121133
return Col_to_dblint_<double, doubles>(x);
122134
}
@@ -131,13 +143,6 @@ inline integers as_integers(const uvec& x) {
131143
return y;
132144
}
133145

134-
// On 64-bit word systems, ivec is Col<long>
135-
#if defined(ARMA_64BIT_WORD)
136-
inline integers as_integers(const Col<long>& x) {
137-
return Col_to_dblint_<long, integers>(x);
138-
}
139-
#endif
140-
141146
inline integers as_integers(const uword& x) {
142147
writable::integers y(1);
143148

0 commit comments

Comments
 (0)