-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
192 lines (147 loc) · 5.62 KB
/
list.c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define LEFT_ANCHOR_FLAG (1U << 21)
#define RIGHT_ANCHOR_FLAG (1U << 22)
#define ID_MASK 0xFFFFFu
typedef struct ListItem ListItem;
struct ListItem {
ListItem *leftNeighbour;
ListItem *rightNeighbour;
ListItem *childrenListBeginning;
ListItem *childrenListEnd;
unsigned data;
// Node id stored in the first 20 bits, other bits reserved for flags
};
static void setListItemFlag(ListItem * const targetItem,
const unsigned flag, const bool value) {
assert(targetItem != NULL && (flag & ID_MASK) == 0);
if (value) {
targetItem->data |= flag;
}
else {
targetItem->data &= ~flag;
}
}
void setListItemLeftAnchored(ListItem * const targetItem, const bool value) {
setListItemFlag(targetItem, LEFT_ANCHOR_FLAG, value);
}
void setListItemRightAnchored(ListItem * const targetItem, const bool value) {
setListItemFlag(targetItem, RIGHT_ANCHOR_FLAG, value);
}
static void nullifyConnections(ListItem * const targetItem){
assert(targetItem != NULL);
targetItem->leftNeighbour = NULL;
targetItem->rightNeighbour = NULL;
setListItemLeftAnchored(targetItem, false);
setListItemRightAnchored(targetItem, false);
}
ListItem* getChildrenListBeginningFromListItem(ListItem * const targetItem) {
assert(targetItem != NULL);
return targetItem->childrenListBeginning;
}
ListItem* getChildrenListEndFromListItem(ListItem * const targetItem) {
assert(targetItem != NULL);
return targetItem->childrenListEnd;
}
unsigned getIdFromListItem(ListItem * const targetItem) {
assert(targetItem != NULL);
return targetItem->data & ID_MASK;
}
bool isListItemLeftAnchored(ListItem * const targetItem) {
assert(targetItem != NULL);
return (targetItem->data & LEFT_ANCHOR_FLAG) != 0;
}
bool isListItemRightAnchored(ListItem * const targetItem) {
assert(targetItem != NULL);
return (targetItem->data & RIGHT_ANCHOR_FLAG) != 0;
}
ListItem* nextListItem(ListItem * const targetItem) {
assert(targetItem != NULL);
if (isListItemRightAnchored(targetItem)) {
return NULL;
}
return targetItem->rightNeighbour;
}
ListItem* previousListItem(ListItem * const targetItem) {
assert(targetItem != NULL);
if (isListItemLeftAnchored(targetItem)) {
return NULL;
}
return targetItem->leftNeighbour;
}
ListItem* nextListItemOrAnchor(ListItem * const targetItem) {
assert(targetItem != NULL);
return targetItem->rightNeighbour;
}
ListItem* previousListItemOrAnchor(ListItem * const targetItem) {
assert(targetItem != NULL);
return targetItem->leftNeighbour;
}
void leftAnchorListItem(ListItem * const anchor, ListItem * const targetItem) {
assert(anchor != NULL && targetItem != NULL);
anchor->childrenListBeginning = targetItem;
targetItem->leftNeighbour = anchor;
setListItemLeftAnchored(targetItem, true);
}
void rightAnchorListItem(ListItem * const anchor, ListItem * const targetItem) {
assert(anchor != NULL && targetItem != NULL);
anchor->childrenListEnd = targetItem;
targetItem->rightNeighbour = anchor;
setListItemRightAnchored(targetItem, true);
}
ListItem* createListItem(const unsigned id) {
assert((id & ID_MASK) == id);
ListItem *newListItem = calloc(1, sizeof(ListItem));
assert(newListItem != NULL);
newListItem->data = (id & ID_MASK);
return newListItem;
}
void disconnectListItem(ListItem * const targetItem) {
assert(targetItem != NULL);
if (isListItemLeftAnchored(targetItem)) {
previousListItemOrAnchor(targetItem)->childrenListBeginning = nextListItem(targetItem);
if (nextListItem(targetItem) != NULL) {
setListItemLeftAnchored(nextListItem(targetItem), true);
}
}
else if (previousListItem(targetItem) != NULL) {
previousListItem(targetItem)->rightNeighbour = nextListItemOrAnchor(targetItem);
}
if (isListItemRightAnchored(targetItem)) {
nextListItemOrAnchor(targetItem)->childrenListEnd = previousListItem(targetItem);
if (previousListItem(targetItem) != NULL) {
setListItemRightAnchored(previousListItem(targetItem), true);
}
}
else if (nextListItem(targetItem) != NULL) {
nextListItem(targetItem)->leftNeighbour = previousListItemOrAnchor(targetItem);
}
nullifyConnections(targetItem);
}
void connectListItems(ListItem * const leftItem, ListItem * const rightItem) {
assert(leftItem != NULL && rightItem != NULL);
setListItemRightAnchored(leftItem, false);
setListItemLeftAnchored(rightItem, false);
leftItem->rightNeighbour = rightItem;
rightItem->leftNeighbour = leftItem;
}
void insertListFragmentInPlaceOfListItem(ListItem * const targetItem,
ListItem * const listBeginning,
ListItem * const listEnd) {
assert(targetItem != NULL && listBeginning != NULL && listEnd != NULL);
if (isListItemLeftAnchored(targetItem)) {
leftAnchorListItem(previousListItemOrAnchor(targetItem),
listBeginning);
}
else {
connectListItems(previousListItem(targetItem), listBeginning);
}
if (isListItemRightAnchored(targetItem)) {
rightAnchorListItem(nextListItemOrAnchor(targetItem), listEnd);
}
else {
connectListItems(listEnd, nextListItem(targetItem));
}
nullifyConnections(targetItem);
}