forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Link.h
115 lines (100 loc) · 2.26 KB
/
Link.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __LINK_H__
#define __LINK_H__
#include "Atomic.h"
namespace as {
class Link {
// successor element
Link* link;
public:
Link () : link(0) {}
Link (Link* item) : link(item) {}
// return successor
Link* select () const {
Link* result = 0;
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
result = link;
}
return result;
}
// define successor
void select (Link* item) {
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
link=item;
}
}
// add successor
void append (Link& item) {
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
item.select(select());
select(&item);
}
}
// return tail item
Link* ending () const {
Link* item=0;
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
item=(Link*)this;
while( item->select() != 0 ) {
item = item->select();
}
}
return item;
}
// remove and return successor
Link* unlink () {
Link* item=0;
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
item=select();
if( item!=0 ) {
detach();
}
}
return item;
}
// remove all, return successor
Link* remove () {
Link* item=0;
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
item=select();
select(0);
}
return item;
}
// remove successor
void detach () {
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
select(select()->select());
}
}
// return container instance
Link* search (const Link* item) const {
Link* result = 0;
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
Link* tmp = select();
Link* vor = (Link*)this;
while (result == 0 && tmp != 0) {
if (tmp == item) {
result = vor;
}
vor = tmp;
tmp = tmp->select();
}
}
return result;
}
// remove item
void remove (const Link& item) {
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
Link* vor = search(&item);
if( vor != 0 ) {
vor->unlink();
}
}
}
};
}
#endif