MLX
Loading...
Searching...
No Matches
load.h
Go to the documentation of this file.
1// Copyright © 2023 Apple Inc.
2
3#pragma once
4
5#include <fstream>
6#include <istream>
7#include <memory>
8
9namespace mlx::core {
10
11namespace io {
12
13class Reader {
14 public:
15 virtual bool is_open() const = 0;
16 virtual bool good() const = 0;
17 virtual size_t tell() = 0; // tellp is non-const in iostream
18 virtual void seek(
19 int64_t off,
20 std::ios_base::seekdir way = std::ios_base::beg) = 0;
21 virtual void read(char* data, size_t n) = 0;
22 virtual std::string label() const = 0;
23};
24
25class Writer {
26 public:
27 virtual bool is_open() const = 0;
28 virtual bool good() const = 0;
29 virtual size_t tell() = 0;
30 virtual void seek(
31 int64_t off,
32 std::ios_base::seekdir way = std::ios_base::beg) = 0;
33 virtual void write(const char* data, size_t n) = 0;
34 virtual std::string label() const = 0;
35};
36
37class FileReader : public Reader {
38 public:
39 explicit FileReader(std::ifstream is)
40 : is_(std::move(is)), label_("stream") {}
41 explicit FileReader(std::string file_path)
42 : is_(std::ifstream(file_path, std::ios::binary)),
43 label_(std::move(file_path)) {}
44
45 bool is_open() const override {
46 return is_.is_open();
47 }
48
49 bool good() const override {
50 return is_.good();
51 }
52
53 size_t tell() override {
54 return is_.tellg();
55 }
56
57 void seek(int64_t off, std::ios_base::seekdir way = std::ios_base::beg)
58 override {
59 is_.seekg(off, way);
60 }
61
62 void read(char* data, size_t n) override {
63 is_.read(data, n);
64 }
65
66 std::string label() const override {
67 return "file " + label_;
68 }
69
70 private:
71 std::ifstream is_;
72 std::string label_;
73};
74
75class FileWriter : public Writer {
76 public:
77 explicit FileWriter(std::ofstream os)
78 : os_(std::move(os)), label_("stream") {}
79 explicit FileWriter(std::string file_path)
80 : os_(std::ofstream(file_path, std::ios::binary)),
81 label_(std::move(file_path)) {}
82
83 bool is_open() const override {
84 return os_.is_open();
85 }
86
87 bool good() const override {
88 return os_.good();
89 }
90
91 size_t tell() override {
92 return os_.tellp();
93 }
94
95 void seek(int64_t off, std::ios_base::seekdir way = std::ios_base::beg)
96 override {
97 os_.seekp(off, way);
98 }
99
100 void write(const char* data, size_t n) override {
101 os_.write(data, n);
102 }
103
104 std::string label() const override {
105 return "file " + label_;
106 }
107
108 private:
109 std::ofstream os_;
110 std::string label_;
111};
112
113} // namespace io
114} // namespace mlx::core
Definition load.h:37
bool good() const override
Definition load.h:49
FileReader(std::ifstream is)
Definition load.h:39
void read(char *data, size_t n) override
Definition load.h:62
std::string label() const override
Definition load.h:66
FileReader(std::string file_path)
Definition load.h:41
bool is_open() const override
Definition load.h:45
size_t tell() override
Definition load.h:53
void seek(int64_t off, std::ios_base::seekdir way=std::ios_base::beg) override
Definition load.h:57
Definition load.h:75
FileWriter(std::string file_path)
Definition load.h:79
std::string label() const override
Definition load.h:104
void seek(int64_t off, std::ios_base::seekdir way=std::ios_base::beg) override
Definition load.h:95
bool good() const override
Definition load.h:87
size_t tell() override
Definition load.h:91
FileWriter(std::ofstream os)
Definition load.h:77
void write(const char *data, size_t n) override
Definition load.h:100
bool is_open() const override
Definition load.h:83
Definition load.h:13
virtual bool good() const =0
virtual size_t tell()=0
virtual bool is_open() const =0
virtual std::string label() const =0
virtual void seek(int64_t off, std::ios_base::seekdir way=std::ios_base::beg)=0
virtual void read(char *data, size_t n)=0
Definition load.h:25
virtual bool good() const =0
virtual size_t tell()=0
virtual std::string label() const =0
virtual bool is_open() const =0
virtual void seek(int64_t off, std::ios_base::seekdir way=std::ios_base::beg)=0
virtual void write(const char *data, size_t n)=0
array std(const array &a, bool keepdims, int ddof=0, StreamOrDevice s={})
Computes the standard deviation of the elements of an array.
Definition allocator.h:7