Skip to content

Commit 743f594

Browse files
committed
simplestl stack
1 parent a7db028 commit 743f594

File tree

4 files changed

+91
-82
lines changed

4 files changed

+91
-82
lines changed

src/expression.cpp

Lines changed: 36 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
148148
};
149149

150150
// scan and stack
151-
std::vector<typed_value> exprstack;
151+
std::stack<typed_value> exprstack;
152152
for (int i = (int)tokens.size() - 1; i >= 0; i--)
153153
{
154154
const std::string& t = tokens[i];
@@ -179,21 +179,14 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
179179

180180
// NCNN_LOGE("t = %s => %d", t.c_str(), size);
181181

182-
exprstack.push_back(size);
182+
exprstack.push(size);
183183
}
184184
else if (t == "+" || t == "-" || t == "*" || t == "//" || t == "max" || t == "min")
185185
{
186-
#if NCNN_SIMPLESTL
187-
typed_value ta = exprstack[exprstack.size() - 1];
188-
exprstack.resize(exprstack.size() - 1);
189-
typed_value tb = exprstack[exprstack.size() - 1];
190-
exprstack.resize(exprstack.size() - 1);
191-
#else
192-
typed_value ta = exprstack.back();
193-
exprstack.pop_back();
194-
typed_value tb = exprstack.back();
195-
exprstack.pop_back();
196-
#endif
186+
typed_value ta = exprstack.top();
187+
exprstack.pop();
188+
typed_value tb = exprstack.top();
189+
exprstack.pop();
197190

198191
if (ta.type == 0 && tb.type == 0)
199192
{
@@ -233,7 +226,7 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
233226
{
234227
r = std::min(a, b);
235228
}
236-
exprstack.push_back(r);
229+
exprstack.push(r);
237230
}
238231
else
239232
{
@@ -265,18 +258,13 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
265258
{
266259
r = std::min(a, b);
267260
}
268-
exprstack.push_back(r);
261+
exprstack.push(r);
269262
}
270263
}
271264
else if (t == "abs" || t == "neg" || t == "sign" || t == "square")
272265
{
273-
#if NCNN_SIMPLESTL
274-
typed_value ta = exprstack[exprstack.size() - 1];
275-
exprstack.resize(exprstack.size() - 1);
276-
#else
277-
typed_value ta = exprstack.back();
278-
exprstack.pop_back();
279-
#endif
266+
typed_value ta = exprstack.top();
267+
exprstack.pop();
280268

281269
if (ta.type == 0)
282270
{
@@ -299,7 +287,7 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
299287
{
300288
r = a * a;
301289
}
302-
exprstack.push_back(r);
290+
exprstack.push(r);
303291
}
304292
else
305293
{
@@ -322,23 +310,18 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
322310
{
323311
r = a * a;
324312
}
325-
exprstack.push_back(r);
313+
exprstack.push(r);
326314
}
327315
}
328316
else if (t == "trunc" || t == "ceil" || t == "floor" || t == "round")
329317
{
330-
#if NCNN_SIMPLESTL
331-
typed_value ta = exprstack[exprstack.size() - 1];
332-
exprstack.resize(exprstack.size() - 1);
333-
#else
334-
typed_value ta = exprstack.back();
335-
exprstack.pop_back();
336-
#endif
318+
typed_value ta = exprstack.top();
319+
exprstack.pop();
337320

338321
if (ta.type == 0)
339322
{
340323
const int a = ta.i;
341-
exprstack.push_back(a);
324+
exprstack.push(a);
342325
}
343326
else
344327
{
@@ -361,7 +344,7 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
361344
{
362345
r = (int)round(a);
363346
}
364-
exprstack.push_back(r);
347+
exprstack.push(r);
365348
}
366349
}
367350
else if (t == "acos"
@@ -384,13 +367,8 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
384367
|| t == "tan"
385368
|| t == "tanh")
386369
{
387-
#if NCNN_SIMPLESTL
388-
typed_value ta = exprstack[exprstack.size() - 1];
389-
exprstack.resize(exprstack.size() - 1);
390-
#else
391-
typed_value ta = exprstack.back();
392-
exprstack.pop_back();
393-
#endif
370+
typed_value ta = exprstack.top();
371+
exprstack.pop();
394372

395373
const float a = ta.type == 0 ? ta.i : ta.f;
396374

@@ -471,7 +449,7 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
471449
{
472450
r = tanhf(a);
473451
}
474-
exprstack.push_back(r);
452+
exprstack.push(r);
475453
}
476454
else if (t == "/"
477455
|| t == "atan2"
@@ -480,17 +458,10 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
480458
|| t == "remainder"
481459
|| t == "logaddexp")
482460
{
483-
#if NCNN_SIMPLESTL
484-
typed_value ta = exprstack[exprstack.size() - 1];
485-
exprstack.resize(exprstack.size() - 1);
486-
typed_value tb = exprstack[exprstack.size() - 1];
487-
exprstack.resize(exprstack.size() - 1);
488-
#else
489-
typed_value ta = exprstack.back();
490-
exprstack.pop_back();
491-
typed_value tb = exprstack.back();
492-
exprstack.pop_back();
493-
#endif
461+
typed_value ta = exprstack.top();
462+
exprstack.pop();
463+
typed_value tb = exprstack.top();
464+
exprstack.pop();
494465

495466
const float a = ta.type == 0 ? ta.i : ta.f;
496467
const float b = tb.type == 0 ? tb.i : tb.f;
@@ -522,21 +493,14 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
522493
{
523494
r = logf(expf(a) + expf(b));
524495
}
525-
exprstack.push_back(r);
496+
exprstack.push(r);
526497
}
527498
else if (t == "and" || t == "or" || t == "xor" || t == "lshift" || t == "rshift")
528499
{
529-
#if NCNN_SIMPLESTL
530-
typed_value ta = exprstack[exprstack.size() - 1];
531-
exprstack.resize(exprstack.size() - 1);
532-
typed_value tb = exprstack[exprstack.size() - 1];
533-
exprstack.resize(exprstack.size() - 1);
534-
#else
535-
typed_value ta = exprstack.back();
536-
exprstack.pop_back();
537-
typed_value tb = exprstack.back();
538-
exprstack.pop_back();
539-
#endif
500+
typed_value ta = exprstack.top();
501+
exprstack.pop();
502+
typed_value tb = exprstack.top();
503+
exprstack.pop();
540504

541505
// assert ta.type == 0 && tb.type == 0
542506

@@ -564,7 +528,7 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
564528
{
565529
r = a >> b;
566530
}
567-
exprstack.push_back(r);
531+
exprstack.push(r);
568532
}
569533
else
570534
{
@@ -575,38 +539,28 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
575539
int nscanf = sscanf(t.c_str(), "%f", &vf);
576540
if (nscani == 1 && nscanf == 1 && vi == vf)
577541
{
578-
exprstack.push_back(vi);
542+
exprstack.push(vi);
579543
}
580544
else if (nscanf == 1)
581545
{
582-
exprstack.push_back(vf);
546+
exprstack.push(vf);
583547
}
584548
else
585549
{
586550
NCNN_LOGE("malformed literal token %s", t.c_str());
587-
exprstack.push_back(0);
551+
exprstack.push(0);
588552
}
589553
}
590554
}
591555

592556
std::vector<int> list;
593-
#if NCNN_SIMPLESTL
594-
int size = exprstack[exprstack.size() - 1].to_int();
595-
exprstack.resize(exprstack.size() - 1);
596-
#else
597-
int size = exprstack.back().to_int();
598-
exprstack.pop_back();
599-
#endif
557+
int size = exprstack.top().to_int();
558+
exprstack.pop();
600559
list.push_back(size);
601560
while (!exprstack.empty())
602561
{
603-
#if NCNN_SIMPLESTL
604-
size = exprstack[exprstack.size() - 1].to_int();
605-
exprstack.resize(exprstack.size() - 1);
606-
#else
607-
size = exprstack.back().to_int();
608-
exprstack.pop_back();
609-
#endif
562+
size = exprstack.top().to_int();
563+
exprstack.pop();
610564
list.push_back(size);
611565
}
612566

src/platform.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ static inline void swap_endianness_32(void* x)
272272
#include <algorithm>
273273
#include <list>
274274
#include <vector>
275+
#include <stack>
275276
#include <string>
276277
#endif
277278

src/simplestl.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,20 @@ struct vector
498498
return pos;
499499
}
500500

501+
void pop_back()
502+
{
503+
if (size_ > 0)
504+
{
505+
data_[size_ - 1].~T();
506+
size_--;
507+
}
508+
}
509+
510+
T& back() const
511+
{
512+
return data_[size_ - 1];
513+
}
514+
501515
protected:
502516
T* data_;
503517
size_t size_;
@@ -519,6 +533,40 @@ struct vector
519533
}
520534
};
521535

536+
template<typename T>
537+
struct stack : protected vector<T>
538+
{
539+
void push(const T& t)
540+
{
541+
vector<T>::push_back(t);
542+
}
543+
544+
void pop()
545+
{
546+
vector<T>::pop_back();
547+
}
548+
549+
T& top()
550+
{
551+
return vector<T>::back();
552+
}
553+
554+
bool empty() const
555+
{
556+
return vector<T>::empty();
557+
}
558+
559+
size_t size() const
560+
{
561+
return vector<T>::size();
562+
}
563+
564+
void clear()
565+
{
566+
vector<T>::clear();
567+
}
568+
};
569+
522570
struct NCNN_EXPORT string : public vector<char>
523571
{
524572
string()

tools/pnnx/src/pass_ncnn/convert_reshape_expression.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ void convert_reshape_expression(Graph& graph)
303303
}
304304
else
305305
{
306+
if (input_index > 9)
307+
{
308+
// ncnn can only handle at most 10 reference blobs
309+
fprintf(stderr, "expression with large reference id %d is not supported yet\n");
310+
}
311+
306312
int bi = std::stoi(b);
307313

308314
const int a_batch_index = ordered_references[input_index]->params["__batch_index"].i;

0 commit comments

Comments
 (0)