forked from activeloopai/deeplake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_buffer.hpp
More file actions
138 lines (110 loc) · 2.87 KB
/
Copy pathread_buffer.hpp
File metadata and controls
138 lines (110 loc) · 2.87 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#pragma once
#include "span_cast.hpp"
#include <cstdint>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <boost/container/vector.hpp>
namespace base {
class read_buffer
{
public:
using value_type = uint8_t;
read_buffer() = default;
explicit read_buffer(std::string&& s)
{
auto h = std::make_shared<std::string>(std::move(s));
data_ = span_cast<const uint8_t>(std::span<const char>(h->data(), h->size()));
holder_ = std::move(h);
}
template <typename T>
explicit read_buffer(std::vector<T>&& b)
: data_(span_cast<const uint8_t>(std::span<T>(b.data(), b.size())))
, holder_(std::make_shared<std::vector<T>>(std::move(b)))
{
}
template <typename T>
explicit read_buffer(boost::container::vector<T>&& b)
: data_(span_cast<const uint8_t>(std::span<T>(b.data(), b.size())))
, holder_(std::make_shared<boost::container::vector<T>>(std::move(b)))
{
}
read_buffer(std::shared_ptr<void> holder, std::span<const uint8_t> data)
: data_(data)
, holder_(std::move(holder))
{
}
const uint8_t* data() const noexcept
{
return data_.data();
}
size_t size() const noexcept
{
return data_.size();
}
bool empty() const noexcept
{
return size() == 0;
}
template <typename T>
std::span<const T> span() const noexcept
{
return span_cast<const T>(span());
}
std::span<const uint8_t> span() const noexcept
{
return data_;
}
std::string_view string_view() const noexcept
{
return base::string_view_cast(base::span_cast<const char>(span()));
}
read_buffer chunk(int64_t start, int64_t end) const noexcept
{
return read_buffer(holder_, std::span<const uint8_t>(data_.begin() + start, data_.begin() + end));
}
template <typename T>
read_buffer chunk(int64_t start, int64_t end) const noexcept
{
return read_buffer(holder_, span_cast<const uint8_t>(span<T>().subspan(start, end - start)));
}
const auto& operator[](size_t i) const noexcept
{
return span()[i];
}
auto begin() const noexcept
{
return span().begin();
}
auto end() const noexcept
{
return span().end();
}
auto rbegin() const noexcept
{
return span().rbegin();
}
auto rend() const noexcept
{
return span().rend();
}
bool is_shared() const noexcept
{
return holder_.use_count() > 1;
}
int64_t use_count() const noexcept
{
return holder_.use_count();
}
const std::shared_ptr<void>& owner() const noexcept
{
return holder_;
}
private:
std::span<const uint8_t> data_ = {};
std::shared_ptr<void> holder_ = nullptr;
};
} // namespace base