Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
youngwolf
qme
提交
31c3cbc4
Q
qme
项目概览
youngwolf
/
qme
通知
5
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Q
qme
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
31c3cbc4
编写于
2月 02, 2024
作者:
Y
youngwolf
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Code optimization -- eliminate dynamic_pointer_cast at execution time.
上级
96397f05
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
16 addition
and
27 deletion
+16
-27
question_exp.h
question_exp.h
+16
-27
未找到文件。
question_exp.h
浏览文件 @
31c3cbc4
...
@@ -89,7 +89,8 @@ public:
...
@@ -89,7 +89,8 @@ public:
virtual
bool
is_composite
()
const
{
return
false
;}
virtual
bool
is_composite
()
const
{
return
false
;}
virtual
int
get_depth
()
const
{
return
1
;}
virtual
int
get_depth
()
const
{
return
1
;}
virtual
void
show_immediate_value
()
const
{}
virtual
void
show_immediate_value
()
const
{}
virtual
void
clear
()
{}
virtual
void
safe_delete
()
const
{}
//used in qme::safe_delete function, do not call it directly
virtual
void
clear
()
{}
//used in qme::safe_delete function, do not call it directly
};
};
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
...
@@ -119,6 +120,8 @@ public:
...
@@ -119,6 +120,8 @@ public:
virtual
std
::
shared_ptr
<
data_exp
<
T
>>
to_negative
()
const
=
0
;
virtual
std
::
shared_ptr
<
data_exp
<
T
>>
to_negative
()
const
=
0
;
virtual
T
operator
()(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
)
const
=
0
;
virtual
T
operator
()(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
)
const
=
0
;
//used in qme::safe_execute function, do not call it directly
virtual
T
safe_execute
(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
cb
)
const
{
return
operator
()(
cb
);}
};
};
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
...
@@ -395,6 +398,7 @@ public:
...
@@ -395,6 +398,7 @@ public:
}
}
}
}
virtual
T
safe_execute
(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
)
const
{
throw
(
"unsupported safe execute operation!"
);}
virtual
void
clear
()
{
dexp_l
.
reset
();
dexp_r
.
reset
();}
virtual
void
clear
()
{
dexp_l
.
reset
();
dexp_r
.
reset
();}
private:
private:
...
@@ -842,7 +846,10 @@ template <typename T> inline int travel_exp(const T& exp,
...
@@ -842,7 +846,10 @@ template <typename T> inline int travel_exp(const T& exp,
const
std
::
function
<
void
(
const
T
&
)
>&
backtrack_handler
)
const
std
::
function
<
void
(
const
T
&
)
>&
backtrack_handler
)
{
{
if
(
!
exp
->
is_composite
())
if
(
!
exp
->
is_composite
())
{
left_handler
(
exp
);
return
1
;
return
1
;
}
auto
depth
=
1
,
max_depth
=
0
;
auto
depth
=
1
,
max_depth
=
0
;
std
::
list
<
std
::
pair
<
T
,
bool
>>
exps
;
//true - left branch, false - right branch
std
::
list
<
std
::
pair
<
T
,
bool
>>
exps
;
//true - left branch, false - right branch
...
@@ -941,20 +948,13 @@ private:
...
@@ -941,20 +948,13 @@ private:
T
value
;
T
value
;
};
};
template
<
typename
T
>
class
question_exp
;
template
<
typename
T
>
inline
T
safe_execute
(
const
std
::
shared_ptr
<
data_exp
<
T
>>&
dexp
,
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
cb
)
template
<
typename
T
>
inline
T
safe_execute
(
const
std
::
shared_ptr
<
data_exp
<
T
>>&
dexp
,
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
cb
)
{
{
auto
qexp
=
std
::
dynamic_pointer_cast
<
question_exp
<
T
>>
(
dexp
);
if
(
qexp
)
//for question_exp, recursion still happens here
return
qexp
->
safe_execute
(
cb
);
else
if
(
!
dexp
->
is_composite
())
return
(
*
dexp
)(
cb
);
std
::
list
<
immediate_data
<
T
>>
res
;
std
::
list
<
immediate_data
<
T
>>
res
;
travel_exp
<
std
::
shared_ptr
<
data_exp
<
T
>>>
(
dexp
,
travel_exp
<
std
::
shared_ptr
<
data_exp
<
T
>>>
(
dexp
,
[
&
](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
res
.
emplace_back
(
safe_execute
(
data
,
cb
));},
[
&
](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
res
.
emplace_back
(
data
->
safe_execute
(
cb
));},
[](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
)
{
return
false
;},
[](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
)
{
return
false
;},
[
&
](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
res
.
emplace_back
(
safe_execute
(
data
,
cb
));},
[
&
](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
res
.
emplace_back
(
data
->
safe_execute
(
cb
));},
[
&
](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
[
&
](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
T
re
=
res
.
back
();
T
re
=
res
.
back
();
res
.
pop_back
();
res
.
pop_back
();
...
@@ -967,16 +967,10 @@ template <typename T> inline T safe_execute(const std::shared_ptr<data_exp<T>>&
...
@@ -967,16 +967,10 @@ template <typename T> inline T safe_execute(const std::shared_ptr<data_exp<T>>&
template
<
typename
T
>
inline
void
safe_delete
(
const
std
::
shared_ptr
<
data_exp
<
T
>>&
dexp
)
template
<
typename
T
>
inline
void
safe_delete
(
const
std
::
shared_ptr
<
data_exp
<
T
>>&
dexp
)
{
{
auto
qexp
=
std
::
dynamic_pointer_cast
<
question_exp
<
T
>>
(
dexp
);
if
(
qexp
)
//for question_exp, recursion still happens here
return
qexp
->
safe_delete
();
else
if
(
!
dexp
->
is_composite
())
return
;
travel_exp
<
std
::
shared_ptr
<
data_exp
<
T
>>>
(
dexp
,
travel_exp
<
std
::
shared_ptr
<
data_exp
<
T
>>>
(
dexp
,
[](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
safe_delete
(
data
);},
[](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
data
->
safe_delete
(
);},
[](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
)
{
return
false
;},
[](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
)
{
return
false
;},
[](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
safe_delete
(
data
);},
[](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
data
->
safe_delete
(
);},
[](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
data
->
clear
();}
[](
const
std
::
shared_ptr
<
data_exp
<
T
>>&
data
)
{
data
->
clear
();}
);
);
}
}
...
@@ -995,15 +989,12 @@ public:
...
@@ -995,15 +989,12 @@ public:
virtual
bool
operator
()(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
)
const
=
0
;
virtual
bool
operator
()(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
)
const
=
0
;
//is recursion fully eliminated or not depends on the data_exp(s) this judge_exp holds
//is recursion fully eliminated or not depends on the data_exp(s) this judge_exp holds
//used in qme::safe_execute function, do not call it directly
virtual
bool
safe_execute
(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
)
const
=
0
;
virtual
bool
safe_execute
(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
)
const
=
0
;
virtual
void
safe_delete
()
const
=
0
;
};
};
template
<
typename
T
>
inline
bool
safe_execute
(
const
std
::
shared_ptr
<
judge_exp
<
T
>>&
jexp
,
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
cb
)
template
<
typename
T
>
inline
bool
safe_execute
(
const
std
::
shared_ptr
<
judge_exp
<
T
>>&
jexp
,
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
cb
)
{
{
if
(
!
jexp
->
is_composite
())
return
jexp
->
safe_execute
(
cb
);
auto
re
=
false
;
auto
re
=
false
;
travel_exp
<
std
::
shared_ptr
<
judge_exp
<
T
>>>
(
jexp
,
travel_exp
<
std
::
shared_ptr
<
judge_exp
<
T
>>>
(
jexp
,
[
&
](
const
std
::
shared_ptr
<
judge_exp
<
T
>>&
judge
)
{
re
=
judge
->
safe_execute
(
cb
);},
[
&
](
const
std
::
shared_ptr
<
judge_exp
<
T
>>&
judge
)
{
re
=
judge
->
safe_execute
(
cb
);},
...
@@ -1028,9 +1019,6 @@ template <typename T> inline bool safe_execute(const std::shared_ptr<judge_exp<T
...
@@ -1028,9 +1019,6 @@ template <typename T> inline bool safe_execute(const std::shared_ptr<judge_exp<T
template
<
typename
T
>
inline
void
safe_delete
(
const
std
::
shared_ptr
<
judge_exp
<
T
>>&
jexp
)
template
<
typename
T
>
inline
void
safe_delete
(
const
std
::
shared_ptr
<
judge_exp
<
T
>>&
jexp
)
{
{
if
(
!
jexp
->
is_composite
())
return
jexp
->
safe_delete
();
travel_exp
<
std
::
shared_ptr
<
judge_exp
<
T
>>>
(
jexp
,
travel_exp
<
std
::
shared_ptr
<
judge_exp
<
T
>>>
(
jexp
,
[](
const
std
::
shared_ptr
<
judge_exp
<
T
>>&
judge
)
{
judge
->
safe_delete
();},
[](
const
std
::
shared_ptr
<
judge_exp
<
T
>>&
judge
)
{
judge
->
safe_delete
();},
[](
const
std
::
shared_ptr
<
judge_exp
<
T
>>&
)
{
return
false
;},
[](
const
std
::
shared_ptr
<
judge_exp
<
T
>>&
)
{
return
false
;},
...
@@ -1233,7 +1221,6 @@ public:
...
@@ -1233,7 +1221,6 @@ public:
}
}
virtual
bool
safe_execute
(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
)
const
{
throw
(
"unsupported safe execute operation!"
);}
virtual
bool
safe_execute
(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
)
const
{
throw
(
"unsupported safe execute operation!"
);}
virtual
void
safe_delete
()
const
{
throw
(
"unsupported safe delete operation!"
);}
virtual
void
clear
()
{
jexp_l
.
reset
();
jexp_r
.
reset
();}
virtual
void
clear
()
{
jexp_l
.
reset
();
jexp_r
.
reset
();}
protected:
protected:
...
@@ -1302,10 +1289,12 @@ public:
...
@@ -1302,10 +1289,12 @@ public:
virtual
T
operator
()(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
cb
)
const
{
return
(
*
jexp
)(
cb
)
?
(
*
dexp_l
)(
cb
)
:
(
*
dexp_r
)(
cb
);}
virtual
T
operator
()(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
cb
)
const
{
return
(
*
jexp
)(
cb
)
?
(
*
dexp_l
)(
cb
)
:
(
*
dexp_r
)(
cb
);}
//for question_exp, recursion still happens here if this question_exp is a sub expression
virtual
T
safe_execute
(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
cb
)
const
virtual
T
safe_execute
(
const
std
::
function
<
T
(
const
std
::
string
&
)
>&
cb
)
const
{
return
qme
::
safe_execute
(
jexp
,
cb
)
?
qme
::
safe_execute
(
dexp_l
,
cb
)
:
qme
::
safe_execute
(
dexp_r
,
cb
);}
{
return
qme
::
safe_execute
(
jexp
,
cb
)
?
qme
::
safe_execute
(
dexp_l
,
cb
)
:
qme
::
safe_execute
(
dexp_r
,
cb
);}
void
safe_delete
()
{
qme
::
safe_delete
(
jexp
);
qme
::
safe_delete
(
dexp_l
);
qme
::
safe_delete
(
dexp_r
);}
//for question_exp, recursion still happens here if this question_exp is a sub expression
virtual
void
safe_delete
()
const
{
qme
::
safe_delete
(
jexp
);
qme
::
safe_delete
(
dexp_l
);
qme
::
safe_delete
(
dexp_r
);}
protected:
protected:
std
::
shared_ptr
<
data_exp
<
T
>>
clone
()
const
{
return
std
::
make_shared
<
question_exp
<
T
>>
(
jexp
,
dexp_l
,
dexp_r
);}
std
::
shared_ptr
<
data_exp
<
T
>>
clone
()
const
{
return
std
::
make_shared
<
question_exp
<
T
>>
(
jexp
,
dexp_l
,
dexp_r
);}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录