YSTest: node_base.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

node_base.cpp
浏览该文件的文档.
1 /*
2 © 2019 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "ystdex/node_base.h"
29 #include "ystdex/functional.hpp" // for yverify, ystdex::retry_on_cond;
30
31 namespace ystdex
32 {
33
34 namespace details
35 {
36
37 void
38 list_node_base::swap(list_node_base& x, list_node_base& y) ynothrow
39 {
40 if(x.next != &x)
41 {
42 if(y.next != &y)
43 {
44 std::swap(x.next, y.next),
45 std::swap(x.prev, y.prev);
46 yunseq(
47 x.next->prev = x.prev->next = &x,
48 y.next->prev = y.prev->next = &y
49 );
50 }
51 else
52 {
53 yunseq(
54 y.next = x.next,
55 y.prev = x.prev
56 );
57 yunseq(
58 y.next->prev = y.prev->next = &y,
59 x.next = x.prev = &x
60 );
61 }
62 }
63 else if(y.next != &y)
64 {
65 yunseq(
66 x.next = y.next,
67 x.prev = y.prev
68 );
69 yunseq(
70 x.next->prev = x.prev->next = &x,
71 y.next = y.prev = &y
72 );
73 }
74 }
75
76 void
77 list_node_base::transfer(list_node_base::base_ptr first,
78 list_node_base::base_ptr last) ynothrow
79 {
80 // NOTE: The destination node of the splice operations is specified by
81 // %this.
82 yverify(last != this);
83 // NOTE: Determine the next node of the back node of the range, the node
84 // before the range and the node before the destination node.
85 last->prev->next = this;
86 first->prev->next = last;
87 prev->next = first;
88
89 const auto tmp(prev);
90
91 // NOTE: Determine the previous node of the node before the destination
92 // node, the next node of the back node of the range in the source and the
93 // front node of the range.
94 prev = last->prev;
95 last->prev = first->prev;
96 first->prev = tmp;
97 }
98
99
100 void
101 list_node_base::reverse_nodes() ynothrow
102 {
103 auto x(this);
104
105 ystdex::retry_on_cond([&]() ynothrow{
106 return x != this;
107 }, [&]() ynothrow{
108 std::swap(x->next, x->prev);
109 x = x->prev;
110 });
111 }
112
113 void
114 list_node_base::hook(base_ptr position) ynothrow
115 {
116 yunseq(next = position, prev = position->prev);
117 position->prev->next = this;
118 position->prev = this;
119 }
120
121 void
122 list_node_base::unhook() ynothrow
123 {
124 const auto saved_next(next);
125 const auto saved_prev(prev);
126
127 saved_prev->next = saved_next;
128 saved_next->prev = saved_prev;
129 }
130
131
132 list_header::list_header(list_header&& x) ynothrow
133 : list_node_base{x.next, x.prev}, node_count(x.node_count)
134 {
135 if(x.next != &x.base())
136 {
137 next->prev = prev->next = &base();
138 x.reset();
139 }
140 else
141 reset_links();
142 }
143
144 void
145 list_header::move_nodes(list_header&& x)
146 {
147 auto& xnode(x.base());
148
149 if(xnode.next == &xnode)
150 reset();
151 else
152 {
153 const auto p_node(&base());
154
155 yunseq(p_node->next = xnode.next, p_node->prev = xnode.prev);
156 p_node->next->prev = p_node->prev->next = p_node;
157 node_count = x.node_count;
158 x.reset();
159 }
160 }
161
162 } // namespace details;
163
164 } // namespace ystdex;
165
x
const EncapsulationBase & x
Definition: NPLA1Forms.cpp:1536
ystdex::cpp2017::swap
void swap(optional< _type > &x, optional< _type > &y)
Definition: optional.h:888
ystdex::details::list_node_base::transfer
void transfer(base_ptr, base_ptr)
Definition: node_base.cpp:77
ystdex::retry_on_cond
invoke_result_t< _fCallable, _tParams... > retry_on_cond(_fCond cond, _fCallable &&f, _tParams &&... args)
循环重复调用：代替直接使用 do-while 语句以避免过多引入作用域外的变量。
Definition: functional.hpp:554
ystdex::details::list_node_base::prev
base_ptr prev
Definition: node_base.h:53
next
tidy_ptr< slink > next
Definition: memory_resource.cpp:223
ystdex::details::list_node_base::next
base_ptr next
Definition: node_base.h:52
ystdex::details::list_header
Definition: node_base.h:84
yverify
#define yverify(_expr)
验证：执行语义。
Definition: cassert.h:144
node_base.h
作为节点序列容器实现的节点基础。
ystdex::details::list_node_base::reverse_nodes
void reverse_nodes()
Definition: node_base.cpp:101
ystdex::details::list_header::move_nodes
void move_nodes(list_header &&)
Definition: node_base.cpp:145
functional.hpp
函数和可调用对象。
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1535
ystdex::details::list_node_base::hook
void hook(base_ptr)
Definition: node_base.cpp:114
ystdex::details::list_node_base::unhook
void unhook()
Definition: node_base.cpp:122
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
ystdex::details::list_header::base
list_node_base & base()
Definition: node_base.h:108
ystdex::details::list_header::node_count
size_t node_count
Definition: node_base.h:86
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
ystdex::details::list_header::reset
void reset()
Definition: node_base.h:100
ystdex::details::list_node_base
Definition: node_base.h:47
ystdex::details::list_header::list_header
list_header()
Definition: node_base.h:88
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
ystdex::details::list_node_base::swap
static void swap(list_node_base &, list_node_base &)
Definition: node_base.cpp:38