19#ifndef TESSERACT_LSTM_NETWORKSCRATCH_H_
20#define TESSERACT_LSTM_NETWORKSCRATCH_H_
54 : int_mode_(scratch->int_mode_ && src.int_mode()), scratch_space_(scratch) {
56 int_mode_ ? scratch_space_->int_stack_.Borrow() : scratch_space_->float_stack_.Borrow();
60 IO() : int_mode_(false), network_io_(nullptr), scratch_space_(nullptr) {}
63 if (scratch_space_ ==
nullptr) {
65 }
else if (int_mode_) {
66 scratch_space_->int_stack_.Return(network_io_);
68 scratch_space_->float_stack_.Return(network_io_);
75 if (scratch_space_ ==
nullptr) {
76 int_mode_ = scratch->int_mode_ && src.
int_mode();
77 scratch_space_ = scratch;
79 int_mode_ ? scratch_space_->int_stack_.Borrow() : scratch_space_->float_stack_.Borrow();
81 network_io_->
Resize(src, num_features);
85 if (scratch_space_ ==
nullptr) {
86 int_mode_ = scratch->int_mode_ && int_mode;
87 scratch_space_ = scratch;
89 int_mode_ ? scratch_space_->int_stack_.Borrow() : scratch_space_->float_stack_.Borrow();
91 network_io_->
Resize2d(int_mode, width, num_features);
96 if (scratch_space_ ==
nullptr) {
98 scratch_space_ = scratch;
99 network_io_ = scratch_space_->float_stack_.Borrow();
136 FloatVec() : vec_(nullptr), data_(nullptr), scratch_space_(nullptr) {}
138 if (scratch_space_ !=
nullptr) {
139 scratch_space_->vec_stack_.
Return(vec_);
144 if (scratch_space_ !=
nullptr && vec_ !=
nullptr) {
145 scratch_space_->vec_stack_.
Return(vec_);
147 scratch_space_ = scratch;
148 vec_ = scratch_space_->vec_stack_.
Borrow();
150 vec_->resize(reserve);
155 Init(size, size, scratch);
169 std::vector<TFloat> *vec_;
185 if (scratch_space_ !=
nullptr) {
186 scratch_space_->array_stack_.Return(array_);
191 if (scratch_space_ !=
nullptr && array_ !=
nullptr) {
192 scratch_space_->array_stack_.Return(array_);
194 scratch_space_ = scratch;
195 array_ = scratch_space_->array_stack_.Borrow();
196 array_->
Resize(size1, size2, 0.0);
218 template <
typename T>
224 for (
auto data : stack_) {
232 std::lock_guard<std::mutex> lock(mutex_);
233 if (stack_top_ == stack_.size()) {
234 stack_.push_back(
new T);
235 flags_.push_back(
false);
237 flags_[stack_top_] =
true;
238 return stack_[stack_top_++];
246 std::lock_guard<std::mutex> lock(mutex_);
248 int index = stack_top_;
249 while (--index >= 0 && stack_[index] != item) {
252 flags_[index] =
false;
254 while (stack_top_ > 0 && !flags_[stack_top_ - 1]) {
260 std::vector<T *> stack_;
261 std::vector<bool> flags_;
262 unsigned stack_top_ = 0;
271 Stack<NetworkIO> int_stack_;
272 Stack<NetworkIO> float_stack_;
273 Stack<std::vector<TFloat>> vec_stack_;
274 Stack<TransposedArray> array_stack_;
void Resize(int size1, int size2, const T &empty)
void Resize(const NetworkIO &src, int num_features)
void ResizeFloat(const NetworkIO &src, int num_features)
void Resize2d(bool int_mode, int width, int num_features)
void set_int_mode(bool int_mode)
~NetworkScratch()=default
void Resize(const NetworkIO &src, int num_features, NetworkScratch *scratch)
void Resize2d(bool int_mode, int width, int num_features, NetworkScratch *scratch)
void ResizeFloat(const NetworkIO &src, int num_features, NetworkScratch *scratch)
IO(const NetworkIO &src, NetworkScratch *scratch)
void Init(int size, NetworkScratch *scratch)
FloatVec(int size, NetworkScratch *scratch)
void Init(int, int reserve, NetworkScratch *scratch)
const TransposedArray & operator*() const
TransposedArray * get() const
void Init(int size1, int size2, NetworkScratch *scratch)