-
Notifications
You must be signed in to change notification settings - Fork 89
/
GrowableBuffer.cpp
153 lines (134 loc) · 4.66 KB
/
GrowableBuffer.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
#include "awkward/builder/ArrayBuilderOptions.h"
#include "awkward/builder/GrowableBuffer.h"
#include "awkward/kernel-dispatch.h"
#include <complex>
#include <cmath>
#include <cstring>
namespace awkward {
template <typename T>
GrowableBuffer<T>
GrowableBuffer<T>::empty(const ArrayBuilderOptions& options) {
return GrowableBuffer<T>::empty(options, 0);
}
template <typename T>
GrowableBuffer<T>
GrowableBuffer<T>::empty(const ArrayBuilderOptions& options,
int64_t minreserve) {
size_t actual = (size_t)options.initial();
if (actual < (size_t)minreserve) {
actual = (size_t)minreserve;
}
std::shared_ptr<T> ptr = kernel::malloc<T>(kernel::lib::cpu, (int64_t)(actual*sizeof(T)));
return GrowableBuffer(options, ptr, 0, (int64_t)actual);
}
template <typename T>
GrowableBuffer<T>
GrowableBuffer<T>::full(const ArrayBuilderOptions& options,
T value,
int64_t length) {
GrowableBuffer<T> out = empty(options, length);
T* rawptr = out.ptr().get();
for (int64_t i = 0; i < length; i++) {
rawptr[i] = value;
}
return GrowableBuffer<T>(options, out.ptr(), length, out.reserved());
}
template <typename T>
GrowableBuffer<T>
GrowableBuffer<T>::arange(const ArrayBuilderOptions& options,
int64_t length) {
size_t actual = (size_t)options.initial();
if (actual < (size_t)length) {
actual = (size_t)length;
}
std::shared_ptr<T> ptr = kernel::malloc<T>(kernel::lib::cpu, (int64_t)(actual*sizeof(T)));
T* rawptr = ptr.get();
for (int64_t i = 0; i < length; i++) {
rawptr[i] = (T)i;
}
return GrowableBuffer(options, ptr, length, (int64_t)actual);
}
template <typename T>
GrowableBuffer<T>::GrowableBuffer(const ArrayBuilderOptions& options,
std::shared_ptr<T> ptr,
int64_t length,
int64_t reserved)
: options_(options)
, ptr_(ptr)
, length_(length)
, reserved_(reserved) { }
template <typename T>
GrowableBuffer<T>::GrowableBuffer(const ArrayBuilderOptions& options)
: GrowableBuffer(options,
kernel::malloc<T>(kernel::lib::cpu, options.initial()*(int64_t)sizeof(T)),
0,
options.initial()) { }
template <typename T>
const std::shared_ptr<T>
GrowableBuffer<T>::ptr() const {
return ptr_;
}
template <typename T>
int64_t
GrowableBuffer<T>::length() const {
return length_;
}
template <typename T>
void
GrowableBuffer<T>::set_length(int64_t newlength) {
if (newlength > reserved_) {
set_reserved(newlength);
}
length_ = newlength;
}
template <typename T>
int64_t
GrowableBuffer<T>::reserved() const {
return reserved_;
}
template <typename T>
void
GrowableBuffer<T>::set_reserved(int64_t minreserved) {
if (minreserved > reserved_) {
std::shared_ptr<T> ptr = kernel::malloc<T>(kernel::lib::cpu, minreserved*(int64_t)sizeof(T));
memcpy(ptr.get(), ptr_.get(), (size_t)length_ * sizeof(T));
ptr_ = ptr;
reserved_ = minreserved;
}
}
template <typename T>
void
GrowableBuffer<T>::clear() {
length_ = 0;
reserved_ = options_.initial();
ptr_ = kernel::malloc<T>(kernel::lib::cpu, options_.initial()*(int64_t)sizeof(T));
}
template <typename T>
void
GrowableBuffer<T>::append(T datum) {
if (length_ == reserved_) {
set_reserved((int64_t)ceil(reserved_ * options_.resize()));
}
ptr_.get()[length_] = datum;
length_++;
}
template <typename T>
T
GrowableBuffer<T>::getitem_at_nowrap(int64_t at) const {
return ptr_.get()[at];
}
template class EXPORT_TEMPLATE_INST GrowableBuffer<bool>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<int8_t>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<int16_t>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<int32_t>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<int64_t>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<uint8_t>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<uint16_t>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<uint32_t>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<uint64_t>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<float>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<double>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<std::complex<float>>;
template class EXPORT_TEMPLATE_INST GrowableBuffer<std::complex<double>>;
}