Support primitive integral types in wq_for without a wrapper

上级 a08f3595
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include "lib.h" #include "lib.h"
#include "fcntl.h" #include "fcntl.h"
#include "wq.hh" #include "wq.hh"
#include "wqit.hh"
static int branch; static int branch;
...@@ -12,10 +11,10 @@ static void ...@@ -12,10 +11,10 @@ static void
dolevel(int fd, int depth) dolevel(int fd, int depth)
{ {
if (depth > 0) { if (depth > 0) {
wq_num<int> it(0); int it = 0;
wq_for_serial<wq_num<int> >(it, wq_for_serial<int>(it,
[](wq_num<int> &it)->bool { return it < branch; }, [](int &it)->bool { return it < branch; },
[&fd, &depth](int i)->void [&fd, &depth](int i)->void
{ {
char name[] = "a"; char name[] = "a";
*name += i; *name += i;
......
...@@ -13,14 +13,6 @@ public: ...@@ -13,14 +13,6 @@ public:
return *this; return *this;
} }
const char * copy_value() {
char *buf = (char*)malloc(256);
return name(buf, 256);
}
bool end() const { return end_; }
private:
char *name(char *buf, size_t n) const { char *name(char *buf, size_t n) const {
n = MIN(DIRSIZ+1, n); n = MIN(DIRSIZ+1, n);
memmove(buf, de_.name, n-1); memmove(buf, de_.name, n-1);
...@@ -28,6 +20,9 @@ private: ...@@ -28,6 +20,9 @@ private:
return buf; return buf;
} }
bool end() const { return end_; }
private:
void refill(void) { void refill(void) {
int r; int r;
...@@ -45,3 +40,10 @@ private: ...@@ -45,3 +40,10 @@ private:
bool end_; bool end_;
struct dirent de_; struct dirent de_;
}; };
static inline const char*
copy_value(const dirit &it)
{
char *buf = (char*)malloc(256);
return it.name(buf, 256);
}
...@@ -13,7 +13,7 @@ struct forwork : public work { ...@@ -13,7 +13,7 @@ struct forwork : public work {
: it_(it), cond_(cond), body_(body), frame_(frame) {} : it_(it), cond_(cond), body_(body), frame_(frame) {}
virtual void run() { virtual void run() {
decltype(it_.copy_value()) v = it_.copy_value(); decltype(copy_value(it_)) v = copy_value(it_);
++it_; ++it_;
if (cond_(it_)) { if (cond_(it_)) {
forwork<IT, BODY> *w = new forwork<IT, BODY>(it_, cond_, body_, frame_); forwork<IT, BODY> *w = new forwork<IT, BODY>(it_, cond_, body_, frame_);
...@@ -48,7 +48,10 @@ wq_for(IT &init, bool (*cond)(IT &it), BODY body) ...@@ -48,7 +48,10 @@ wq_for(IT &init, bool (*cond)(IT &it), BODY body)
// XXX(sbw) should be able to coarsen loop // XXX(sbw) should be able to coarsen loop
decltype(init.copy_value()) v = init.copy_value(); if (!cond(init))
return;
decltype(copy_value(init)) v = copy_value(init);
++init; ++init;
if (cond(init)) { if (cond(init)) {
forwork<IT, BODY> *w = new forwork<IT, BODY>(init, cond, body, frame); forwork<IT, BODY> *w = new forwork<IT, BODY>(init, cond, body, frame);
...@@ -56,8 +59,6 @@ wq_for(IT &init, bool (*cond)(IT &it), BODY body) ...@@ -56,8 +59,6 @@ wq_for(IT &init, bool (*cond)(IT &it), BODY body)
wq_push(w); wq_push(w);
} }
// XXX(sbw) oops, skip first cond check
body(v); body(v);
while (!frame.zero()) while (!frame.zero())
...@@ -69,5 +70,12 @@ static inline void ...@@ -69,5 +70,12 @@ static inline void
wq_for_serial(IT &init, bool (*cond)(IT &it), BODY body) wq_for_serial(IT &init, bool (*cond)(IT &it), BODY body)
{ {
for (; cond(init); ++init) for (; cond(init); ++init)
body(init.copy_value()); body(copy_value(init));
}
template <typename T>
static inline T
copy_value(const T &it)
{
return it;
} }
template<typename T>
struct wq_num
{
wq_num(T v) { v_ = v; }
bool operator!= (const wq_num<T> &i) const { return v_ != i.v_; }
bool operator!= (const T &v) const { return v_ != v; }
bool operator< (const wq_num<T> &i) const { return v_ < i.v_; }
bool operator< (const T &v) const { return v_ < v; }
T copy_value() {
return v_;
}
wq_num<T>& operator ++() {
++v_;
return *this;
}
T v_;
};
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论