Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDILab开源决策智能平台
treevalue
提交
a114920f
T
treevalue
项目概览
OpenDILab开源决策智能平台
/
treevalue
大约 1 年 前同步成功
通知
3
Star
213
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
treevalue
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a114920f
编写于
12月 30, 2021
作者:
HansBug
😆
浏览文件
操作
浏览文件
下载
差异文件
dev(hansbug): merge from main branch
上级
a95cdaa7
c2cb0b6e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
33 addition
and
27 deletion
+33
-27
treevalue/tree/tree/functional.pxd
treevalue/tree/tree/functional.pxd
+4
-4
treevalue/tree/tree/functional.pyx
treevalue/tree/tree/functional.pyx
+29
-23
未找到文件。
treevalue/tree/tree/functional.pxd
浏览文件 @
a114920f
...
...
@@ -8,10 +8,10 @@ from libcpp cimport bool
from
.tree
cimport
TreeValue
from
..common.storage
cimport
TreeStorage
cdef
class
_ValuePathFuncWrapper
:
cdef
readonly
object
func
cdef
int
index
cdef
object
_c_no_arg
(
object
func
,
object
v
,
object
p
)
cdef
object
_c_one_arg
(
object
func
,
object
v
,
object
p
)
cdef
object
_c_two_args
(
object
func
,
object
v
,
object
p
)
cdef
object
_c_wrap_mapping_func
(
object
func
)
cdef
object
_c_delayed_mapping
(
object
so
,
object
func
,
tuple
path
,
bool
delayed
)
cdef
TreeStorage
_c_mapping
(
TreeStorage
st
,
object
func
,
tuple
path
,
bool
delayed
)
cpdef
TreeValue
mapping
(
TreeValue
tree
,
object
func
,
bool
delayed
=
*
)
...
...
treevalue/tree/tree/functional.pyx
浏览文件 @
a114920f
...
...
@@ -4,6 +4,7 @@
# mapping, filter_, mask, reduce_
import
cython
from
functools
import
partial
from
libcpp
cimport
bool
from
.tree
cimport
TreeValue
...
...
@@ -12,23 +13,28 @@ from ..common.delay cimport undelay
from
..common.delay
import
delayed_partial
from
..common.storage
cimport
TreeStorage
cdef
class
_ValuePathFuncWrapper
:
def
__cinit__
(
self
,
func
):
self
.
func
=
func
self
.
index
=
2
cdef
inline
object
_c_no_arg
(
object
func
,
object
v
,
object
p
):
return
func
()
def
__call__
(
self
,
object
v
,
tuple
path
):
while
True
:
if
self
.
index
==
0
:
return
self
.
func
()
cdef
inline
object
_c_one_arg
(
object
func
,
object
v
,
object
p
):
return
func
(
v
)
try
:
if
self
.
index
==
1
:
return
self
.
func
(
v
)
else
:
return
self
.
func
(
v
,
path
)
except
TypeError
:
self
.
index
-=
1
cdef
inline
object
_c_two_args
(
object
func
,
object
v
,
object
p
):
return
func
(
v
,
p
)
cdef
inline
object
_c_wrap_mapping_func
(
object
func
):
cdef
int
argcnt
try
:
argcnt
=
func
.
__code__
.
co_argcount
except
AttributeError
:
argcnt
=
1
if
argcnt
==
1
:
return
partial
(
_c_one_arg
,
func
)
elif
argcnt
>
1
:
return
partial
(
_c_two_args
,
func
)
else
:
return
partial
(
_c_no_arg
,
func
)
cdef
object
_c_delayed_mapping
(
object
so
,
object
func
,
tuple
path
,
bool
delayed
):
cdef
object
nso
=
undelay
(
so
)
...
...
@@ -83,9 +89,9 @@ cpdef TreeValue mapping(TreeValue tree, object func, bool delayed=False):
- ``lambda v, p: f(v, p)``, which means map the original value and full path (in form of ``tuple``)
\
to a new values based on given ``v`` and given ``p``.
When the given ``func`` is
used, it is firstly tries as ``lambda v, p: f(v, p)``. If ``TypeError`` is
\
raised, then the next is ``lambda v: f(v)``, and the ``lambda :v``. So the fastest way to use this
\
function is to given a ``lambda v, p: f(v, p)`` in it
.
When the given ``func`` is
a python function (with ``__code__`` attribute), its number of arguments will
\
be detected automatically, and use the correct way to call it when doing mapping, otherwise it will be
\
directly used with the pattern of ``lambda v: f(v)``
.
Returns:
- tree (:obj:`_TreeValue`): Mapped tree value object.
...
...
@@ -96,7 +102,7 @@ cpdef TreeValue mapping(TreeValue tree, object func, bool delayed=False):
>>> mapping(t, lambda: 1) # TreeValue({'a': 1, 'b': 1, 'x': {'c': 1, 'd': 1}})
>>> mapping(t, lambda x, p: p) # TreeValue({'a': ('a',), 'b': ('b',), 'x': {'c': ('x', 'c'), 'd': ('x', 'd')}})
"""
return
type
(
tree
)(
_c_mapping
(
tree
.
_detach
(),
_
ValuePathFuncWrapper
(
func
),
(),
delayed
))
return
type
(
tree
)(
_c_mapping
(
tree
.
_detach
(),
_
c_wrap_mapping_func
(
func
),
(),
delayed
))
cdef
TreeStorage
_c_filter_
(
TreeStorage
st
,
object
func
,
tuple
path
,
bool
remove_empty
):
cdef
dict
_d_st
=
st
.
detach
()
...
...
@@ -142,9 +148,9 @@ cpdef TreeValue filter_(TreeValue tree, object func, bool remove_empty=True):
- ``lambda v, p: f(v, p)``, which means map the original value and full path (in form of ``tuple``)
\
to a new values based on given ``v`` and given ``p``.
When the given ``func`` is
used, it is firstly tries as ``lambda v, p: f(v, p)``. If ``TypeError`` is
\
raised, then the next is ``lambda v: f(v)``, and the ``lambda :v``. So the fastest way to use this
\
function is to given a ``lambda v, p: f(v, p)`` in it
.
When the given ``func`` is
a python function (with ``__code__`` attribute), its number of arguments will
\
be detected automatically, and use the correct way to call it when doing filter, otherwise it will be
\
directly used with the pattern of ``lambda v: f(v)``
.
Returns:
- tree (:obj:`_TreeValue`): Filtered tree value object.
...
...
@@ -156,7 +162,7 @@ cpdef TreeValue filter_(TreeValue tree, object func, bool remove_empty=True):
>>> filter_(t, lambda x: x % 2 == 1) # TreeValue({'a': 1, 'x': {'c': 3}})
>>> filter_(t, lambda x, p: p[0] in {'b', 'x'}) # TreeValue({'b': 2, 'x': {'c': 3, 'd': 4}})
"""
return
type
(
tree
)(
_c_filter_
(
tree
.
_detach
(),
_
ValuePathFuncWrapper
(
func
),
(),
remove_empty
))
return
type
(
tree
)(
_c_filter_
(
tree
.
_detach
(),
_
c_wrap_mapping_func
(
func
),
(),
remove_empty
))
cdef
object
_c_mask
(
TreeStorage
st
,
object
sm
,
tuple
path
,
bool
remove_empty
):
cdef
bool
_b_tree_mask
=
isinstance
(
sm
,
TreeStorage
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录