From d8c117d6d3093396333cb8e55ffc60152204182c Mon Sep 17 00:00:00 2001 From: Grace Dinh <34658064+gdinh@users.noreply.github.com> Date: Fri, 19 Jul 2024 18:16:56 -0400 Subject: [PATCH 1/4] change filename vars to const char* (#8) * change filenames to const char* --------- Co-authored-by: Benjamin Brock --- examples/benchmark_write.c | 2 +- include/binsparse/detail/parse_dataset.h | 2 +- include/binsparse/hdf5_wrapper.h | 8 ++++---- .../binsparse/matrix_market/matrix_market_inspector.h | 2 +- include/binsparse/matrix_market/matrix_market_read.h | 9 +++++---- include/binsparse/matrix_market/matrix_market_write.h | 2 +- include/binsparse/read_matrix.h | 4 ++-- include/binsparse/write_matrix.h | 2 +- 8 files changed, 16 insertions(+), 15 deletions(-) diff --git a/examples/benchmark_write.c b/examples/benchmark_write.c index bbd1b91..49beab5 100644 --- a/examples/benchmark_write.c +++ b/examples/benchmark_write.c @@ -60,7 +60,7 @@ void flush_writes() { #endif } -void delete_file(char* file_name) { +void delete_file(const char* file_name) { char command[2048]; snprintf(command, 2047, "rm %s", file_name); system(command); diff --git a/include/binsparse/detail/parse_dataset.h b/include/binsparse/detail/parse_dataset.h index 434c821..53123c8 100644 --- a/include/binsparse/detail/parse_dataset.h +++ b/include/binsparse/detail/parse_dataset.h @@ -35,7 +35,7 @@ bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) { } } -char* bsp_get_file_extension(char* file_name) { +const char* bsp_get_file_extension(const char* file_name) { int64_t len = strlen(file_name); for (int64_t i = len - 1; i >= 0; i--) { if (file_name[i] == '.') { diff --git a/include/binsparse/hdf5_wrapper.h b/include/binsparse/hdf5_wrapper.h index c08d02c..e720222 100644 --- a/include/binsparse/hdf5_wrapper.h +++ b/include/binsparse/hdf5_wrapper.h @@ -7,7 +7,7 @@ // Write an array to a dataset / file // Returns 0 on success, nonzero on error. -int bsp_write_array(hid_t f, char* label, bsp_array_t array, +int bsp_write_array(hid_t f, const char* label, bsp_array_t array, int compression_level) { if (array.type == BSP_COMPLEX_FLOAT32 || array.type == BSP_COMPLEX_FLOAT64) { array = bsp_complex_array_to_fp(array); @@ -61,7 +61,7 @@ int bsp_write_array(hid_t f, char* label, bsp_array_t array, return 0; } -bsp_array_t bsp_read_array(hid_t f, char* label) { +bsp_array_t bsp_read_array(hid_t f, const char* label) { hid_t dset = H5Dopen2(f, label, H5P_DEFAULT); if (dset == H5I_INVALID_HID) { @@ -100,7 +100,7 @@ bsp_array_t bsp_read_array(hid_t f, char* label) { return array; } -void bsp_write_attribute(hid_t f, char* label, char* string) { +void bsp_write_attribute(hid_t f, const char* label, const char* string) { hid_t strtype = H5Tcopy(H5T_C_S1); H5Tset_size(strtype, strlen(string)); H5Tset_cset(strtype, H5T_CSET_UTF8); @@ -116,7 +116,7 @@ void bsp_write_attribute(hid_t f, char* label, char* string) { H5Sclose(dataspace); } -char* bsp_read_attribute(hid_t f, char* label) { +char* bsp_read_attribute(hid_t f, const char* label) { hid_t attribute = H5Aopen(f, label, H5P_DEFAULT); hid_t strtype = H5Aget_type(attribute); diff --git a/include/binsparse/matrix_market/matrix_market_inspector.h b/include/binsparse/matrix_market/matrix_market_inspector.h index d33878c..23e57e1 100644 --- a/include/binsparse/matrix_market/matrix_market_inspector.h +++ b/include/binsparse/matrix_market/matrix_market_inspector.h @@ -26,7 +26,7 @@ typedef struct bsp_mm_metadata { char* comments; } bsp_mm_metadata; -bsp_mm_metadata bsp_mmread_metadata(char* file_path) { +bsp_mm_metadata bsp_mmread_metadata(const char* file_path) { FILE* f = fopen(file_path, "r"); assert(f != NULL); diff --git a/include/binsparse/matrix_market/matrix_market_read.h b/include/binsparse/matrix_market/matrix_market_read.h index e2e1fb3..d6745e4 100644 --- a/include/binsparse/matrix_market/matrix_market_read.h +++ b/include/binsparse/matrix_market/matrix_market_read.h @@ -9,7 +9,8 @@ #include #include -bsp_matrix_t bsp_mmread_explicit_array(char* file_path, bsp_type_t value_type, +bsp_matrix_t bsp_mmread_explicit_array(const char* file_path, + bsp_type_t value_type, bsp_type_t index_type) { bsp_mm_metadata metadata = bsp_mmread_metadata(file_path); @@ -96,7 +97,7 @@ bsp_matrix_t bsp_mmread_explicit_array(char* file_path, bsp_type_t value_type, return matrix; } -bsp_matrix_t bsp_mmread_explicit_coordinate(char* file_path, +bsp_matrix_t bsp_mmread_explicit_coordinate(const char* file_path, bsp_type_t value_type, bsp_type_t index_type) { bsp_mm_metadata metadata = bsp_mmread_metadata(file_path); @@ -254,7 +255,7 @@ bsp_matrix_t bsp_mmread_explicit_coordinate(char* file_path, return matrix; } -bsp_matrix_t bsp_mmread_explicit(char* file_path, bsp_type_t value_type, +bsp_matrix_t bsp_mmread_explicit(const char* file_path, bsp_type_t value_type, bsp_type_t index_type) { bsp_mm_metadata metadata = bsp_mmread_metadata(file_path); @@ -267,7 +268,7 @@ bsp_matrix_t bsp_mmread_explicit(char* file_path, bsp_type_t value_type, } } -bsp_matrix_t bsp_mmread(char* file_path) { +bsp_matrix_t bsp_mmread(const char* file_path) { bsp_mm_metadata metadata = bsp_mmread_metadata(file_path); bsp_type_t value_type; diff --git a/include/binsparse/matrix_market/matrix_market_write.h b/include/binsparse/matrix_market/matrix_market_write.h index 3446bda..75f9aeb 100644 --- a/include/binsparse/matrix_market/matrix_market_write.h +++ b/include/binsparse/matrix_market/matrix_market_write.h @@ -6,7 +6,7 @@ #include -void bsp_mmwrite(char* file_path, bsp_matrix_t matrix) { +void bsp_mmwrite(const char* file_path, bsp_matrix_t matrix) { FILE* f = fopen(file_path, "w"); assert(f != NULL); diff --git a/include/binsparse/read_matrix.h b/include/binsparse/read_matrix.h index 12be8fd..40f2694 100644 --- a/include/binsparse/read_matrix.h +++ b/include/binsparse/read_matrix.h @@ -105,7 +105,7 @@ bsp_matrix_t bsp_read_matrix_from_group(hid_t f) { return matrix; } -size_t bsp_final_dot(char* str) { +size_t bsp_final_dot(const char* str) { size_t dot_idx = 0; for (size_t i = 0; str[i] != '\0'; i++) { if (str[i] == '.') { @@ -115,7 +115,7 @@ size_t bsp_final_dot(char* str) { return dot_idx; } -bsp_matrix_t bsp_read_matrix(char* file_name, char* group) { +bsp_matrix_t bsp_read_matrix(const char* file_name, char* group) { if (group == NULL) { size_t idx = bsp_final_dot(file_name); if (strcmp(file_name + idx, ".hdf5") == 0 || diff --git a/include/binsparse/write_matrix.h b/include/binsparse/write_matrix.h index 0bd3657..8eded01 100644 --- a/include/binsparse/write_matrix.h +++ b/include/binsparse/write_matrix.h @@ -121,7 +121,7 @@ int bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix, cJSON* user_json, return 0; } -int bsp_write_matrix(char* fname, bsp_matrix_t matrix, char* group, +int bsp_write_matrix(const char* fname, bsp_matrix_t matrix, char* group, cJSON* user_json, int compression_level) { if (group == NULL) { hid_t f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); From a4392ed76b2b121baffe2ddc333785ec3b36a6e7 Mon Sep 17 00:00:00 2001 From: Benjamin Brock Date: Fri, 19 Jul 2024 15:19:08 -0700 Subject: [PATCH 2/4] Two non-`const` strings that we missed. --- include/binsparse/read_matrix.h | 2 +- include/binsparse/write_matrix.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/binsparse/read_matrix.h b/include/binsparse/read_matrix.h index 40f2694..f290b79 100644 --- a/include/binsparse/read_matrix.h +++ b/include/binsparse/read_matrix.h @@ -115,7 +115,7 @@ size_t bsp_final_dot(const char* str) { return dot_idx; } -bsp_matrix_t bsp_read_matrix(const char* file_name, char* group) { +bsp_matrix_t bsp_read_matrix(const char* file_name, const char* group) { if (group == NULL) { size_t idx = bsp_final_dot(file_name); if (strcmp(file_name + idx, ".hdf5") == 0 || diff --git a/include/binsparse/write_matrix.h b/include/binsparse/write_matrix.h index 8eded01..76dd7c7 100644 --- a/include/binsparse/write_matrix.h +++ b/include/binsparse/write_matrix.h @@ -121,7 +121,7 @@ int bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix, cJSON* user_json, return 0; } -int bsp_write_matrix(const char* fname, bsp_matrix_t matrix, char* group, +int bsp_write_matrix(const char* fname, bsp_matrix_t matrix, const char* group, cJSON* user_json, int compression_level) { if (group == NULL) { hid_t f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); From 2ddb952bf064844cdc8fe9b93d518658bb3bfd89 Mon Sep 17 00:00:00 2001 From: Benjamin Brock Date: Wed, 24 Jul 2024 11:45:26 -0700 Subject: [PATCH 3/4] Fix for C++ --- include/binsparse/detail/cpp/array.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/binsparse/detail/cpp/array.hpp b/include/binsparse/detail/cpp/array.hpp index c351202..2c9d268 100644 --- a/include/binsparse/detail/cpp/array.hpp +++ b/include/binsparse/detail/cpp/array.hpp @@ -2,6 +2,7 @@ #ifdef __cplusplus +#include #include #include @@ -71,7 +72,7 @@ void bsp_array_read(bsp_array_t array, size_t index, T& value) { [&](auto* ptr) { using U = std::remove_pointer_t; - if constexpr (std::is_assignable_v) { + if constexpr (std::is_assignable_v) { value = ptr[index]; } }, @@ -87,7 +88,7 @@ void bsp_array_write(bsp_array_t array, size_t index, U value) { [&](auto* ptr) { using T = std::remove_pointer_t; - if constexpr (std::is_assignable_v) { + if constexpr (std::is_assignable_v) { ptr[index] = value; } }, @@ -105,7 +106,7 @@ void bsp_array_awrite(bsp_array_t array_0, size_t index_0, bsp_array_t array_1, using T = std::remove_pointer_t; using U = std::remove_pointer_t; - if constexpr (std::is_assignable_v) { + if constexpr (std::is_assignable_v) { ptr_0[index_0] = ptr_1[index_1]; } }, From bb40398492130801b293afda32fa0109b42e2b4b Mon Sep 17 00:00:00 2001 From: Benjamin Brock Date: Thu, 25 Jul 2024 13:18:15 -0700 Subject: [PATCH 4/4] Update examples (#9) --- examples/mtx2bsp.c | 4 ++-- examples/simple_matrix_read.c | 8 ++++---- examples/simple_matrix_write.c | 6 +++--- examples/simple_read.c | 2 +- examples/simple_write.c | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/mtx2bsp.c b/examples/mtx2bsp.c index 2ddc93b..7de6d8f 100644 --- a/examples/mtx2bsp.c +++ b/examples/mtx2bsp.c @@ -70,8 +70,8 @@ int main(int argc, char** argv) { compression_level = atoi(argv[4]); } - char* input_file_extension = bsp_get_file_extension(input_fname); - char* output_file_extension = bsp_get_file_extension(output_fname); + const char* input_file_extension = bsp_get_file_extension(input_fname); + const char* output_file_extension = bsp_get_file_extension(output_fname); if (input_file_extension == NULL || strcmp(input_file_extension, ".mtx") != 0) { diff --git a/examples/simple_matrix_read.c b/examples/simple_matrix_read.c index 7d4c0c3..aaa8884 100644 --- a/examples/simple_matrix_read.c +++ b/examples/simple_matrix_read.c @@ -1,14 +1,14 @@ #include int main(int argc, char** argv) { - char* file_name = "test.hdf5"; + const char* file_name = "test.hdf5"; bsp_matrix_t mat = bsp_read_matrix(file_name, NULL); if (mat.format == BSP_COO) { - float* values = mat.values.data; - int* rowind = mat.indices_0.data; - int* colind = mat.indices_1.data; + float* values = (float*) mat.values.data; + int* rowind = (int*) mat.indices_0.data; + int* colind = (int*) mat.indices_1.data; for (size_t i = 0; i < mat.nnz; i++) { printf("%d, %d: %f\n", rowind[i], colind[i], values[i]); diff --git a/examples/simple_matrix_write.c b/examples/simple_matrix_write.c index 88ac963..bf551d2 100644 --- a/examples/simple_matrix_write.c +++ b/examples/simple_matrix_write.c @@ -7,9 +7,9 @@ int main(int argc, char** argv) { bsp_matrix_t mat = bsp_generate_coo(m, n, nnz, BSP_FLOAT32, BSP_INT32); - float* values = mat.values.data; - int* rowind = mat.indices_0.data; - int* colind = mat.indices_1.data; + float* values = (float*) mat.values.data; + int* rowind = (int*) mat.indices_0.data; + int* colind = (int*) mat.indices_1.data; for (size_t i = 0; i < nnz; i++) { printf("%d, %d: %f\n", rowind[i], colind[i], values[i]); diff --git a/examples/simple_read.c b/examples/simple_read.c index 94da3cf..225ea54 100644 --- a/examples/simple_read.c +++ b/examples/simple_read.c @@ -7,7 +7,7 @@ int main(int argc, char** argv) { bsp_array_t array = bsp_read_array(f, "test"); - int* values = array.data; + int* values = (int*) array.data; for (size_t i = 0; i < array.size; i++) { printf("%lu: %d\n", i, values[i]); diff --git a/examples/simple_write.c b/examples/simple_write.c index e4f5952..15e3b36 100644 --- a/examples/simple_write.c +++ b/examples/simple_write.c @@ -1,13 +1,13 @@ #include int main(int argc, char** argv) { - char* file_name = "test.hdf5"; + const char* file_name = "test.hdf5"; hid_t f = H5Fcreate(file_name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); bsp_array_t array = bsp_construct_array_t(1000, BSP_INT32); - int* values = array.data; + int* values = (int*) array.data; for (size_t i = 0; i < array.size; i++) { bsp_array_write(array, i, i);