MCPcopy Create free account
hub / github.com/cwida/ALP / read_data

Function read_data

data/include/data.hpp:17–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15// we prefer the binary_path over csv_path
16template <typename PT>
17inline void read_data(std::vector<PT>& data, const alp_bench::ColumnDescriptor& column_descriptor) {
18 switch (column_descriptor.file_type) {
19
20 case alp_bench::FileType::BINARY: {
21 // Open the binary file in input mode
22 std::ifstream file(column_descriptor.path, std::ios::binary | std::ios::in);
23
24 if (!file) { throw std::runtime_error("Failed to open file: " + column_descriptor.path); }
25
26 // Get the size of the file
27 file.seekg(0, std::ios::end);
28 std::streamsize file_size = file.tellg();
29 file.seekg(0, std::ios::beg);
30
31 // Ensure the file size is a multiple of the size of a double
32 // if (fileSize % sizeof(double) != 0) { throw std::runtime_error("File size is not a multiple of double
33 // size!"); } Calculate the number of doubles
34 std::size_t n_values = file_size / sizeof(PT);
35
36 // Resize the vector to hold all the doubles
37 data.resize(n_values);
38
39 // Read the data into the vector
40 file.read(reinterpret_cast<char*>(data.data()), file_size);
41
42 // Close the file
43 file.close();
44 } break;
45 case alp_bench::FileType::CSV: {
46 const auto& path = column_descriptor.path;
47 std::ifstream file(path);
48
49 if (!file) { throw std::runtime_error("Failed to open file: " + path); }
50
51 std::string line;
52 // Read each line, convert it to double, and store it in the vector
53 while (std::getline(file, line)) {
54 try {
55 // Convert the string to double and add to the vector
56 data.push_back(std::stod(line));
57 } catch (const std::invalid_argument& e) {
58 throw std::runtime_error("Invalid data in file: " + line);
59 } catch (const std::out_of_range& e) {
60 //
61 throw std::runtime_error("Number out of range in file: " + line);
62 }
63 }
64
65 file.close();
66 } break;
67 case alp_bench::FileType::INVALID:
68 default: {
69 throw std::runtime_error("No bin or csv file specified");
70 }
71 }
72}
73
74} // namespace alp_data

Callers 1

typed_bench_columnMethod · 0.50

Calls 4

closeMethod · 0.65
readMethod · 0.45
dataMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected