19 #ifndef TESSERACT_LSTM_NETWORKSCRATCH_H_
20 #define TESSERACT_LSTM_NETWORKSCRATCH_H_
55 : int_mode_(scratch->int_mode_ && src.int_mode()),
56 scratch_space_(scratch) {
57 network_io_ = int_mode_ ? scratch_space_->int_stack_.Borrow()
58 : scratch_space_->float_stack_.Borrow();
62 IO() : int_mode_(false), network_io_(nullptr), scratch_space_(nullptr) {}
65 if (scratch_space_ ==
nullptr) {
67 }
else if (int_mode_) {
68 scratch_space_->int_stack_.Return(network_io_);
70 scratch_space_->float_stack_.Return(network_io_);
78 if (scratch_space_ ==
nullptr) {
79 int_mode_ = scratch->int_mode_ && src.
int_mode();
80 scratch_space_ = scratch;
81 network_io_ = int_mode_ ? scratch_space_->int_stack_.Borrow()
82 : scratch_space_->float_stack_.Borrow();
84 network_io_->
Resize(src, num_features);
87 void Resize2d(
bool int_mode,
int width,
int num_features,
89 if (scratch_space_ ==
nullptr) {
90 int_mode_ = scratch->int_mode_ && int_mode;
91 scratch_space_ = scratch;
92 network_io_ = int_mode_ ? scratch_space_->int_stack_.Borrow()
93 : scratch_space_->float_stack_.Borrow();
95 network_io_->
Resize2d(int_mode, width, num_features);
101 if (scratch_space_ ==
nullptr) {
103 scratch_space_ = scratch;
104 network_io_ = scratch_space_->float_stack_.Borrow();
138 : vec_(nullptr), scratch_space_(scratch) {
142 FloatVec() : vec_(nullptr), data_(nullptr), scratch_space_(nullptr) {}
144 if (scratch_space_ !=
nullptr) scratch_space_->vec_stack_.
Return(vec_);
148 if (scratch_space_ !=
nullptr && vec_ !=
nullptr)
149 scratch_space_->vec_stack_.
Return(vec_);
150 scratch_space_ = scratch;
151 vec_ = scratch_space_->vec_stack_.
Borrow();
158 operator double*()
const {
return data_; }
159 double*
get() {
return data_; }
179 if (scratch_space_ !=
nullptr) scratch_space_->array_stack_.Return(array_);
183 if (scratch_space_ !=
nullptr && array_ !=
nullptr)
184 scratch_space_->array_stack_.Return(array_);
185 scratch_space_ = scratch;
186 array_ = scratch_space_->array_stack_.Borrow();
187 array_->
Resize(size1, size2, 0.0);
213 std::lock_guard<std::mutex> lock(mutex_);
214 if (stack_top_ == stack_.size()) {
215 stack_.push_back(
new T);
218 flags_[stack_top_] =
true;
219 return stack_[stack_top_++];
227 std::lock_guard<std::mutex> lock(mutex_);
229 int index = stack_top_ - 1;
230 while (index >= 0 && stack_[index] != item) --index;
231 if (index >= 0) flags_[index] =
false;
232 while (stack_top_ > 0 && !flags_[stack_top_ - 1]) --stack_top_;
247 Stack<NetworkIO> int_stack_;
248 Stack<NetworkIO> float_stack_;
249 Stack<GenericVector<double> > vec_stack_;
250 Stack<TransposedArray> array_stack_;
255 #endif // TESSERACT_LSTM_NETWORKSCRATCH_H_