34 namespace Clustering {
37 template <
typename NUM>
38 std::tuple<NUM*, std::size_t, std::size_t>
39 read_coords(std::string filename, std::vector<std::size_t> usecols) {
42 std::size_t n_cols_used=0;
44 std::ifstream ifs(filename);
48 while (linebuf.empty() && ifs.good()) {
49 std::getline(ifs, linebuf);
51 std::stringstream ss(linebuf);
52 n_cols = std::distance(std::istream_iterator<std::string>(ss),
53 std::istream_iterator<std::string>());
58 std::getline(ifs, linebuf);
59 if ( ! linebuf.empty()) {
65 ifs.seekg(0, std::ios::beg);
67 std::map<std::size_t, bool> col_used;
68 if (usecols.size() == 0) {
71 for (std::size_t i=0; i < n_cols; ++i) {
76 n_cols_used = usecols.size();
77 for (std::size_t i=0; i < n_cols; ++i) {
80 for (std::size_t i: usecols) {
87 NUM* coords = (NUM*) _mm_malloc(
sizeof(NUM)*n_rows*n_cols_used, DC_MEM_ALIGNMENT);
88 ASSUME_ALIGNED(coords);
90 for (std::size_t cur_row = 0; cur_row < n_rows; ++cur_row) {
91 std::size_t cur_col = 0;
92 for (std::size_t i=0; i < n_cols; ++i) {
96 coords[cur_row*n_cols_used + cur_col] = buf;
101 return std::make_tuple(coords, n_rows, n_cols_used);
105 template <
typename NUM>
111 template <
typename KEY,
typename VAL>
113 write_map(std::string filename, std::map<KEY, VAL> mapping) {
114 std::ofstream ofs(filename);
116 std::cerr <<
"error: cannot open file '" << filename <<
"' for writing." << std::endl;
119 for (
auto key_val: mapping) {
120 ofs << key_val.first <<
" " << key_val.second <<
"\n";
124 template <
typename NUM>
127 std::vector<NUM> dat;
128 std::ifstream ifs(filename);
130 std::cerr <<
"error: cannot open file '" << filename <<
"'" << std::endl;
145 template <
typename NUM>
148 std::ofstream ofs(filename);
150 std::cerr <<
"error: cannot open file '" << filename <<
"' for writing." << std::endl;
153 if (with_scientific_format) {
154 ofs << std::scientific;
161 template <
typename NUM>
164 std::stringstream ss(s);