MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / read_idx

Function read_idx

examples/common/idxio.h:42–78  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40
41template<class ty>
42void read_idx(std::vector<dim_t> &dims, std::vector<ty> &data,
43 const char *name) {
44 std::ifstream f(name, std::ios::in | std::ios::binary);
45 if (!f.is_open()) throw std::runtime_error("Unable to open file");
46
47 Data d;
48 f.read(d.bytes, sizeof(d.bytes));
49
50 if (d.bytes[2] != 8) { throw std::runtime_error("Unsupported data type"); }
51
52 unsigned numdims = d.bytes[3];
53 unsigned elemsize = 1;
54
55 // Read the dimensions
56 size_t elem = 1;
57 dims = std::vector<dim_t>(numdims);
58 for (unsigned i = 0; i < numdims; i++) {
59 f.read(d.bytes, sizeof(d.bytes));
60
61 // Big endian to little endian
62 for (int j = 0; j < 4; j++) d.bytes[j] = reverse_char(d.bytes[j]);
63 unsigned dim = reverse(d.dim);
64
65 elem *= dim;
66 dims[i] = (dim_t)dim;
67 }
68
69 // Read the data
70 std::vector<char> cdata(elem);
71 f.read(&cdata[0], elem * elemsize);
72 std::vector<unsigned char> ucdata(cdata.begin(), cdata.end());
73
74 data = std::vector<ty>(ucdata.begin(), ucdata.end());
75
76 f.close();
77 return;
78}

Callers 1

setup_mnistFunction · 0.85

Calls 3

reverse_charFunction · 0.85
reverseFunction · 0.85
closeMethod · 0.80

Tested by

no test coverage detected