Skip to content

Commit 90f1273

Browse files
committed
Add clang-format
1 parent 5eda04e commit 90f1273

24 files changed

+397
-385
lines changed

.clang-format

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
BasedOnStyle: LLVM
3+
PointerAlignment: Left
4+
ColumnLimit: 80
5+
---

.pre-commit-config.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
repos:
2+
3+
- repo: https://github.com/pre-commit/mirrors-clang-format
4+
rev: v16.0.6
5+
hooks:
6+
- id: clang-format
7+
8+
- repo: https://github.com/pre-commit/pre-commit-hooks
9+
rev: v4.4.0
10+
hooks:
11+
- id: trailing-whitespace
12+
- id: end-of-file-fixer
13+
- id: mixed-line-ending
14+
- id: check-added-large-files

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ bbrock@FVFJ21QKQ6LWmac:~/src/binsparse-reference-impl/build/examples$ make -j
101101
[100%] Linking CXX executable convert_binsparse
102102
[100%] Built target convert_binsparse
103103
bbrock@mymac:~/src/binsparse-reference-impl/build/examples$
104-
```
104+
```

examples/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ endfunction()
66
add_example(test)
77
add_example(convert_binsparse)
88
add_example(inspect_binsparse)
9-

examples/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ BINSPARSE_DIR ?= ../include
1313
# HDF5 library location
1414
HDF5_DIR ?= /opt/homebrew/Cellar/hdf5/1.14.2
1515
HDF5_CXXFLAGS ?= -I$(HDF5_DIR)/include
16-
HDF5_LIBRARY_FLAGS ?= -L$(HDF5_DIR)/lib -lhdf5_hl_cpp -lhdf5_cpp -lhdf5_hl -lhdf5
16+
HDF5_LIBRARY_FLAGS ?= -L$(HDF5_DIR)/lib -lhdf5_hl_cpp -lhdf5_cpp -lhdf5_hl -lhdf5
1717

1818
# = = = = = = = = = = = = = = = = = = = = = = = = = = = #
1919

examples/convert_binsparse.cpp

+37-20
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,57 @@
11
#include <binsparse/binsparse.hpp>
2-
#include <iostream>
3-
#include <concepts>
42
#include <complex>
3+
#include <concepts>
4+
#include <iostream>
55

66
template <typename T, typename I>
7-
void convert_to_binsparse(std::string input_file, std::string output_file, std::string format, std::string comment) {
7+
void convert_to_binsparse(std::string input_file, std::string output_file,
8+
std::string format, std::string comment) {
89
nlohmann::json user_keys;
910
user_keys["comment"] = comment;
1011
if (format == "CSR") {
1112
std::cout << "Reading in " << input_file << "...\n";
12-
auto x = binsparse::__detail::mmread<T, I, binsparse::__detail::csr_matrix_owning<T, I>>(input_file);
13-
binsparse::csr_matrix<T, I> matrix{x.values().data(), x.colind().data(), x.rowptr().data(), std::get<0>(x.shape()), std::get<1>(x.shape()), I(x.size())};
13+
auto x = binsparse::__detail::mmread<
14+
T, I, binsparse::__detail::csr_matrix_owning<T, I>>(input_file);
15+
binsparse::csr_matrix<T, I> matrix{
16+
x.values().data(), x.colind().data(), x.rowptr().data(),
17+
std::get<0>(x.shape()), std::get<1>(x.shape()), I(x.size())};
1418
binsparse::write_csr_matrix(output_file, matrix, user_keys);
15-
std::cout << "Writing to binsparse file " << output_file << " using " << format << " format...\n";
19+
std::cout << "Writing to binsparse file " << output_file << " using "
20+
<< format << " format...\n";
1621
} else {
17-
auto x = binsparse::__detail::mmread<T, I, binsparse::__detail::coo_matrix_owning<T, I>>(input_file);
18-
binsparse::coo_matrix<T, I> matrix{x.values().data(), x.rowind().data(), x.colind().data(), std::get<0>(x.shape()), std::get<1>(x.shape()), I(x.size())};
22+
auto x = binsparse::__detail::mmread<
23+
T, I, binsparse::__detail::coo_matrix_owning<T, I>>(input_file);
24+
binsparse::coo_matrix<T, I> matrix{
25+
x.values().data(), x.rowind().data(), x.colind().data(),
26+
std::get<0>(x.shape()), std::get<1>(x.shape()), I(x.size())};
1927
binsparse::write_coo_matrix(output_file, matrix, user_keys);
20-
std::cout << "Writing to binsparse file " << output_file << " using " << format << " format...\n";
28+
std::cout << "Writing to binsparse file " << output_file << " using "
29+
<< format << " format...\n";
2130
}
2231
}
2332

2433
template <typename I>
25-
void convert_to_binsparse(std::string input_file, std::string output_file, std::string type,
26-
std::string format, std::string comment) {
34+
void convert_to_binsparse(std::string input_file, std::string output_file,
35+
std::string type, std::string format,
36+
std::string comment) {
2737
if (type == "real") {
2838
convert_to_binsparse<float, I>(input_file, output_file, format, comment);
2939
} else if (type == "complex") {
3040
assert(false);
31-
// convert_to_binsparse<std::complex<float>, I>(input_file, output_file, format, comment);
41+
// convert_to_binsparse<std::complex<float>, I>(input_file, output_file,
42+
// format, comment);
3243
} else if (type == "integer") {
3344
convert_to_binsparse<int64_t, I>(input_file, output_file, format, comment);
3445
} else if (type == "pattern") {
3546
convert_to_binsparse<uint8_t, I>(input_file, output_file, format, comment);
3647
}
3748
}
3849

39-
4050
int main(int argc, char** argv) {
4151

4252
if (argc < 3) {
43-
std::cout << "usage: ./convert_binsparse [input_file.mtx] [output_file.hdf5] [optional: format {CSR, COO}]\n";
53+
std::cout << "usage: ./convert_binsparse [input_file.mtx] "
54+
"[output_file.hdf5] [optional: format {CSR, COO}]\n";
4455
return 1;
4556
}
4657

@@ -61,7 +72,8 @@ int main(int argc, char** argv) {
6172

6273
auto [m, n, nnz, type, comment] = binsparse::mmread_metadata(input_file);
6374

64-
std::cout << "Matrix is " << m << " x " << n << " with " << nnz << " values.\n";
75+
std::cout << "Matrix is " << m << " x " << n << " with " << nnz
76+
<< " values.\n";
6577
std::cout << "Type: " << type << std::endl;
6678
std::cout << "Comment:\n";
6779
std::cout << comment;
@@ -71,15 +83,20 @@ int main(int argc, char** argv) {
7183
auto max_size = std::max({m, n, nnz});
7284

7385
if (max_size + 1 <= std::numeric_limits<uint8_t>::max()) {
74-
convert_to_binsparse<uint8_t>(input_file, output_file, type, format, comment);
86+
convert_to_binsparse<uint8_t>(input_file, output_file, type, format,
87+
comment);
7588
} else if (max_size + 1 <= std::numeric_limits<uint16_t>::max()) {
76-
convert_to_binsparse<uint16_t>(input_file, output_file, type, format, comment);
89+
convert_to_binsparse<uint16_t>(input_file, output_file, type, format,
90+
comment);
7791
} else if (max_size + 1 <= std::numeric_limits<uint32_t>::max()) {
78-
convert_to_binsparse<uint32_t>(input_file, output_file, type, format, comment);
92+
convert_to_binsparse<uint32_t>(input_file, output_file, type, format,
93+
comment);
7994
} else if (max_size + 1 <= std::numeric_limits<uint64_t>::max()) {
80-
convert_to_binsparse<uint64_t>(input_file, output_file, type, format, comment);
95+
convert_to_binsparse<uint64_t>(input_file, output_file, type, format,
96+
comment);
8197
} else {
82-
throw std::runtime_error("Error! Matrix dimensions or NNZ too large to handle.");
98+
throw std::runtime_error(
99+
"Error! Matrix dimensions or NNZ too large to handle.");
83100
}
84101

85102
return 0;

examples/convert_matrixmarket.cpp

+28-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <binsparse/binsparse.hpp>
2-
#include <iostream>
3-
#include <concepts>
42
#include <complex>
3+
#include <concepts>
4+
#include <iostream>
55

66
int main(int argc, char** argv) {
77

@@ -16,38 +16,43 @@ int main(int argc, char** argv) {
1616

1717
std::cout << "Inspecting Binsparse v" << metadata["version"] << " file...\n";
1818
std::cout << metadata["format"] << " format matrix of dimension "
19-
<< metadata["shape"] << " with " << metadata["nnz"] << " nonzeros\n";
19+
<< metadata["shape"] << " with " << metadata["nnz"]
20+
<< " nonzeros\n";
2021

2122
if (metadata["format"] == "COO") {
2223
auto i0 = metadata["data_types"]["indices_0"];
2324
auto i1 = metadata["data_types"]["indices_1"];
2425
auto t = metadata["data_types"]["values"];
2526

26-
binsparse::visit_label({i0, i1, t},
27-
[&]<typename I1, typename I2, typename T>(I1 i, I2 j, T v) {
28-
using I = std::conditional_t<std::numeric_limits<I1>::max() < std::numeric_limits<I2>::max(),
29-
I2, I1>;
30-
std::cout << "Reading binsparse with index and value types: "
31-
<< binsparse::type_info<I>::label() << " "
32-
<< binsparse::type_info<T>::label() << "\n";
33-
34-
auto m = binsparse::read_coo_matrix<T, I>(input_file);
35-
});
27+
binsparse::visit_label(
28+
{i0, i1, t},
29+
[&]<typename I1, typename I2, typename T>(I1 i, I2 j, T v) {
30+
using I = std::conditional_t<std::numeric_limits<I1>::max() <
31+
std::numeric_limits<I2>::max(),
32+
I2, I1>;
33+
std::cout << "Reading binsparse with index and value types: "
34+
<< binsparse::type_info<I>::label() << " "
35+
<< binsparse::type_info<T>::label() << "\n";
36+
37+
auto m = binsparse::read_coo_matrix<T, I>(input_file);
38+
});
3639
} else if (metadata["format"] == "CSR") {
3740
auto i0 = metadata["data_types"]["pointers_to_1"];
3841
auto i1 = metadata["data_types"]["indices_1"];
3942
auto t = metadata["data_types"]["values"];
4043

41-
binsparse::visit_label({i0, i1, t},
42-
[&]<typename I1, typename I2, typename T>(I1 i, I2 j, T v) {
43-
using I = std::conditional_t<std::numeric_limits<I1>::max() < std::numeric_limits<I2>::max(),
44-
I2, I1>;
45-
std::cout << "Reading binsparse with index and value types: "
46-
<< binsparse::type_info<I>::label() << " "
47-
<< binsparse::type_info<T>::label() << "\n";
48-
49-
auto m = binsparse::read_csr_matrix<T, I>(input_file);
50-
});
44+
binsparse::visit_label(
45+
{i0, i1, t},
46+
[&]<typename I1, typename I2, typename T>(I1 i, I2 j, T v) {
47+
using I = std::conditional_t<std::numeric_limits<I1>::max() <
48+
std::numeric_limits<I2>::max(),
49+
I2, I1>;
50+
std::cout << "Reading binsparse with index and value types: "
51+
<< binsparse::type_info<I>::label() << " "
52+
<< binsparse::type_info<T>::label() << "\n";
53+
54+
auto m = binsparse::read_csr_matrix<T, I>(input_file);
55+
});
5156
} else {
5257
assert(false);
5358
}

examples/inspect_binsparse.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <binsparse/binsparse.hpp>
2-
#include <iostream>
3-
#include <concepts>
42
#include <complex>
3+
#include <concepts>
4+
#include <iostream>
55
#include <ranges>
66

77
class Foo {};
@@ -21,7 +21,8 @@ int main(int argc, char** argv) {
2121

2222
std::cout << "Inspecting Binsparse v" << metadata["version"] << " file...\n";
2323
std::cout << metadata["format"] << " format matrix of dimension "
24-
<< metadata["shape"] << " with " << metadata["nnz"] << " nonzeros\n";
24+
<< metadata["shape"] << " with " << metadata["nnz"]
25+
<< " nonzeros\n";
2526

2627
if (metadata["format"] == "COO") {
2728
auto i0 = metadata["data_types"]["indices_0"];
@@ -40,7 +41,6 @@ int main(int argc, char** argv) {
4041
assert(false);
4142
}
4243

43-
4444
nlohmann::json user;
4545
std::cout << "User-provided keys:\n";
4646
std::size_t n_keys = 0;

examples/simple_io.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
#include <iostream>
21
#include <binsparse/binsparse.hpp>
2+
#include <iostream>
33

44
int main(int argc, char** argv) {
55
auto mat = binsparse::read_coo_matrix<float, std::size_t>("data/matrix.hdf5");
66

77
for (size_t i = 0; i < mat.nnz; i++) {
8-
std::cout << mat.rowind[i] << ", " << mat.colind[i] << ": " << mat.values[i] << std::endl;
8+
std::cout << mat.rowind[i] << ", " << mat.colind[i] << ": " << mat.values[i]
9+
<< std::endl;
910
}
1011

1112
binsparse::write_coo_matrix("new_matrix.hdf5", mat);

0 commit comments

Comments
 (0)