Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Oneflow-Inc
oneflow
提交
2fc7de77
O
oneflow
项目概览
Oneflow-Inc
/
oneflow
上一次同步 2 年多
通知
13
Star
2733
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
oneflow
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
2fc7de77
编写于
1月 16, 2020
作者:
L
lixinqi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
list_head
上级
250c5d7b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
234 addition
and
21 deletion
+234
-21
oneflow/core/common/list_head.h
oneflow/core/common/list_head.h
+26
-21
oneflow/core/common/list_head_test.cpp
oneflow/core/common/list_head_test.cpp
+208
-0
未找到文件。
oneflow/core/common/list_head.h
浏览文件 @
2fc7de77
#ifndef ONEFLOW_CORE_COMMON_LIST_H_
#define ONEFLOW_CORE_COMMON_LIST_H_
#ifndef ONEFLOW_CORE_COMMON_LIST_H
EAD_H
_
#define ONEFLOW_CORE_COMMON_LIST_H
EAD_H
_
#include "oneflow/core/common/struct_traits.h"
#include <glog/logging.h>
...
...
@@ -10,10 +10,6 @@ struct ListHead final {
public:
ListHead
()
{
Clear
();
}
void
Clear
()
{
prev_
=
this
;
next_
=
this
;
}
ListHead
*
prev
()
const
{
return
prev_
;
}
ListHead
*
next
()
const
{
return
next_
;
}
...
...
@@ -21,6 +17,10 @@ struct ListHead final {
prev
->
set_next
(
this
);
this
->
set_prev
(
prev
);
}
void
Clear
()
{
prev_
=
this
;
next_
=
this
;
}
private:
void
set_prev
(
ListHead
*
prev
)
{
prev_
=
prev
;
}
...
...
@@ -48,9 +48,10 @@ class EmbeddedListViewImpl {
using
container_type
=
typename
ContainerField
::
struct_type
;
using
item_type
=
typename
ItemField
::
struct_type
;
static_assert
(
std
::
is_same
<
ContainerField
::
field_type
,
ListHead
>::
value
,
static_assert
(
std
::
is_same
<
typename
ContainerField
::
field_type
,
ListHead
>::
value
,
"no ListHead found in container"
);
static_assert
(
std
::
is_same
<
ItemField
::
field_type
,
ListHead
>::
value
,
"no ListHead found in item"
);
static_assert
(
std
::
is_same
<
typename
ItemField
::
field_type
,
ListHead
>::
value
,
"no ListHead found in item"
);
explicit
EmbeddedListViewImpl
(
container_type
*
container
)
:
container_
(
container
)
{}
~
EmbeddedListViewImpl
()
=
default
;
...
...
@@ -58,14 +59,7 @@ class EmbeddedListViewImpl {
CHECK_NE
(
item
,
end_item
());
ListHead
*
list_head
=
ItemField
::
FieldPtr4StructPtr
(
item
);
list_head
->
next
()
->
AppendTo
(
list_head
->
prev
());
item
->
Clear
();
}
void
InsertAfter
(
item_type
*
prev_item
,
item_type
*
new_item
)
{
ListHead
*
prev_list_head
=
ItemField
::
FieldPtr4StructPtr
(
prev_item
);
ListHead
*
next_list_head
=
prev_list_head
->
next
();
ListHead
*
new_list_head
=
ItemField
::
FieldPtr4StructPtr
(
new_item
);
new_list_head
->
AppendTo
(
prev_list_head
);
next_list_head
->
AppendTo
(
new_list_head
);
list_head
->
Clear
();
}
void
PushBack
(
item_type
*
item
)
{
InsertAfter
(
last_item
(),
item
);
}
void
PushFront
(
item_type
*
item
)
{
InsertAfter
(
end_item
(),
item
);
}
...
...
@@ -96,14 +90,25 @@ class EmbeddedListViewImpl {
}
private:
void
InsertAfter
(
item_type
*
prev_item
,
item_type
*
new_item
)
{
ListHead
*
prev_list_head
=
ItemField
::
FieldPtr4StructPtr
(
prev_item
);
ListHead
*
next_list_head
=
prev_list_head
->
next
();
ListHead
*
new_list_head
=
ItemField
::
FieldPtr4StructPtr
(
new_item
);
new_list_head
->
AppendTo
(
prev_list_head
);
next_list_head
->
AppendTo
(
new_list_head
);
}
container_type
*
container_
;
};
#define _DEFINE_EMBEDDED_LIST_VIEW(container_field, item_field) \
template<> \
class EmbeddedListView<container_field> final \
: EmbeddedListViewImpl<container_field, item_field> {};
#define _DEFINE_EMBEDDED_LIST_VIEW(container_field, item_field) \
template<> \
class EmbeddedListView<container_field> final \
: public EmbeddedListViewImpl<container_field, item_field> { \
public: \
explicit EmbeddedListView(container_type* container) \
: EmbeddedListViewImpl<container_field, item_field>(container) {} \
};
}
// namespace oneflow
#endif // ONEFLOW_CORE_COMMON_LIST_H_
#endif // ONEFLOW_CORE_COMMON_LIST_H
EAD_H
_
oneflow/core/common/list_head_test.cpp
0 → 100644
浏览文件 @
2fc7de77
#include "oneflow/core/common/list_head.h"
#include "oneflow/core/common/util.h"
namespace
oneflow
{
namespace
test
{
struct
ListHeadFoo
final
{
int
head_field_0
;
int
head_field_1
;
ListHead
bar_list
;
};
struct
ListItemBar
final
{
int
value
;
ListHead
bar_list
;
};
}
// namespace test
DEFINE_EMBEDDED_LIST_VIEW
(
test
::
ListHeadFoo
,
bar_list
,
test
::
ListItemBar
,
bar_list
);
using
BarListView
=
EMBEDDED_LIST_VIEW
(
test
::
ListHeadFoo
,
bar_list
);
namespace
test
{
TEST
(
ListHead
,
init
)
{
ListHead
list_head
;
ASSERT_EQ
(
&
list_head
,
list_head
.
prev
());
ASSERT_EQ
(
&
list_head
,
list_head
.
next
());
}
TEST
(
ListHead
,
append_to
)
{
ListHead
list_head0
;
ListHead
list_head1
;
list_head1
.
AppendTo
(
&
list_head0
);
ASSERT_EQ
(
&
list_head0
,
list_head1
.
prev
());
ASSERT_EQ
(
&
list_head1
,
list_head0
.
next
());
}
TEST
(
ListHead
,
clear
)
{
ListHead
list_head0
;
ListHead
list_head1
;
list_head1
.
AppendTo
(
&
list_head0
);
list_head1
.
Clear
();
ASSERT_EQ
(
&
list_head1
,
list_head1
.
prev
());
ASSERT_EQ
(
&
list_head1
,
list_head1
.
next
());
}
TEST
(
EmbeddedListView
,
empty
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ASSERT_TRUE
(
list_view
.
empty
());
}
TEST
(
EmbeddedListView
,
push_front
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ListItemBar
item0
;
list_view
.
PushFront
(
&
item0
);
ASSERT_EQ
(
head
.
bar_list
.
next
(),
&
item0
.
bar_list
);
ASSERT_EQ
(
head
.
bar_list
.
prev
(),
&
item0
.
bar_list
);
ASSERT_EQ
(
item0
.
bar_list
.
next
(),
&
head
.
bar_list
);
ASSERT_EQ
(
item0
.
bar_list
.
prev
(),
&
head
.
bar_list
);
ListItemBar
item1
;
list_view
.
PushFront
(
&
item1
);
ASSERT_EQ
(
head
.
bar_list
.
next
(),
&
item1
.
bar_list
);
ASSERT_EQ
(
item1
.
bar_list
.
prev
(),
&
head
.
bar_list
);
ASSERT_EQ
(
item1
.
bar_list
.
next
(),
&
item0
.
bar_list
);
ASSERT_EQ
(
item0
.
bar_list
.
prev
(),
&
item1
.
bar_list
);
ASSERT_EQ
(
item0
.
bar_list
.
next
(),
&
head
.
bar_list
);
ASSERT_EQ
(
head
.
bar_list
.
prev
(),
&
item0
.
bar_list
);
}
TEST
(
EmbeddedListView
,
end
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ListItemBar
*
end_item
=
list_view
.
end_item
();
ListItemBar
item0
;
list_view
.
PushFront
(
&
item0
);
ASSERT_EQ
(
end_item
,
list_view
.
end_item
());
}
TEST
(
EmbeddedListView
,
begin
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ASSERT_EQ
(
list_view
.
begin_item
(),
list_view
.
end_item
());
ListItemBar
item0
;
list_view
.
PushFront
(
&
item0
);
ASSERT_EQ
(
list_view
.
begin_item
(),
&
item0
);
ListItemBar
item1
;
list_view
.
PushFront
(
&
item1
);
ASSERT_EQ
(
list_view
.
begin_item
(),
&
item1
);
}
TEST
(
EmbeddedListView
,
last
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ASSERT_EQ
(
list_view
.
begin_item
(),
list_view
.
end_item
());
ListItemBar
item0
;
list_view
.
PushFront
(
&
item0
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item0
);
ListItemBar
item1
;
list_view
.
PushFront
(
&
item1
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item0
);
}
TEST
(
EmbeddedListView
,
push_back
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ASSERT_EQ
(
list_view
.
begin_item
(),
list_view
.
end_item
());
ListItemBar
item0
;
list_view
.
PushBack
(
&
item0
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item0
);
ListItemBar
item1
;
list_view
.
PushBack
(
&
item1
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item1
);
}
TEST
(
EmbeddedListView
,
erase
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ASSERT_EQ
(
list_view
.
begin_item
(),
list_view
.
end_item
());
ListItemBar
item0
;
list_view
.
PushBack
(
&
item0
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item0
);
ListItemBar
item1
;
list_view
.
PushBack
(
&
item1
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item1
);
list_view
.
Erase
(
&
item0
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item1
);
ASSERT_EQ
(
list_view
.
begin_item
(),
&
item1
);
ASSERT_EQ
(
item0
.
bar_list
.
prev
(),
&
item0
.
bar_list
);
ASSERT_EQ
(
item0
.
bar_list
.
next
(),
&
item0
.
bar_list
);
}
TEST
(
EmbeddedListView
,
pop_front
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ASSERT_EQ
(
list_view
.
begin_item
(),
list_view
.
end_item
());
ListItemBar
item0
;
list_view
.
PushBack
(
&
item0
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item0
);
ListItemBar
item1
;
list_view
.
PushBack
(
&
item1
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item1
);
list_view
.
PopFront
();
ASSERT_EQ
(
list_view
.
last_item
(),
&
item1
);
ASSERT_EQ
(
list_view
.
begin_item
(),
&
item1
);
ASSERT_EQ
(
item0
.
bar_list
.
prev
(),
&
item0
.
bar_list
);
ASSERT_EQ
(
item0
.
bar_list
.
next
(),
&
item0
.
bar_list
);
}
TEST
(
EmbeddedListView
,
pop_back
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ASSERT_EQ
(
list_view
.
begin_item
(),
list_view
.
end_item
());
ListItemBar
item0
;
list_view
.
PushBack
(
&
item0
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item0
);
ListItemBar
item1
;
list_view
.
PushBack
(
&
item1
);
ASSERT_EQ
(
list_view
.
last_item
(),
&
item1
);
list_view
.
PopBack
();
ASSERT_EQ
(
list_view
.
last_item
(),
&
item0
);
ASSERT_EQ
(
list_view
.
begin_item
(),
&
item0
);
ASSERT_EQ
(
item1
.
bar_list
.
prev
(),
&
item1
.
bar_list
);
ASSERT_EQ
(
item1
.
bar_list
.
next
(),
&
item1
.
bar_list
);
}
TEST
(
EmbeddedListView
,
next_item
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ListItemBar
item0
;
list_view
.
PushBack
(
&
item0
);
ListItemBar
item1
;
list_view
.
PushBack
(
&
item1
);
ListItemBar
*
item
=
list_view
.
begin_item
();
ASSERT_EQ
(
item
,
&
item0
);
item
=
list_view
.
next_item
(
item
);
ASSERT_EQ
(
item
,
&
item1
);
item
=
list_view
.
next_item
(
item
);
ASSERT_EQ
(
item
,
list_view
.
end_item
());
item
=
list_view
.
next_item
(
item
);
ASSERT_EQ
(
item
,
&
item0
);
}
TEST
(
EmbeddedListView
,
prev_item
)
{
ListHeadFoo
head
;
BarListView
list_view
(
&
head
);
ListItemBar
item0
;
list_view
.
PushBack
(
&
item0
);
ListItemBar
item1
;
list_view
.
PushBack
(
&
item1
);
ListItemBar
*
item
=
list_view
.
begin_item
();
ASSERT_EQ
(
item
,
&
item0
);
item
=
list_view
.
prev_item
(
item
);
ASSERT_EQ
(
item
,
list_view
.
end_item
());
item
=
list_view
.
prev_item
(
item
);
ASSERT_EQ
(
item
,
&
item1
);
item
=
list_view
.
prev_item
(
item
);
ASSERT_EQ
(
item
,
&
item0
);
}
}
// namespace test
}
// namespace oneflow
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录