-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweight.h
37 lines (33 loc) · 1.04 KB
/
weight.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
#include <iostream>
#include <vector>
#include <utility>
class weight {
public:
weight() {}
weight(size_t len) : value(len) {}
weight(weight&& f) : value(std::move(f.value)) {}
weight(const weight& f) = default;
weight& operator =(const weight& f) = default;
float& operator[] (size_t i) { return value[i]; }
const float& operator[] (size_t i) const { return value[i]; }
size_t size() const { return value.size(); }
public:
friend std::ostream& operator <<(std::ostream& out, const weight& w) {
auto& value = w.value;
uint64_t size = value.size();
out.write(reinterpret_cast<const char*>(&size), sizeof(uint64_t));
out.write(reinterpret_cast<const char*>(value.data()), sizeof(float) * size);
return out;
}
friend std::istream& operator >>(std::istream& in, weight& w) {
auto& value = w.value;
uint64_t size = 0;
in.read(reinterpret_cast<char*>(&size), sizeof(uint64_t));
value.resize(size);
in.read(reinterpret_cast<char*>(value.data()), sizeof(float) * size);
return in;
}
protected:
std::vector<float> value;
};