-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintervalset.c
418 lines (366 loc) · 12.3 KB
/
intervalset.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/******************************************************************************
*
* COPYRIGHT NOTICE
* Copyright (c) 2015
* All rights reserved
*
* @author : Gaoyang Dai
* @email : edward.d.erlic@gmail.com
* @file : /home/dgy/Programming/VInterval/intervalset.c
* @date : 2015/03/29 08:47
* @description :
*
*****************************************************************************/
#include <stdio.h>
#include "intervalset.h"
/*typedef struct interval_node {*/
/*interval item ;*/
/*interval *next ;*/
/*} ;*/
interval_node *make_node(interval item)
{
interval_node *p = malloc(sizeof *p) ;
if (p == NULL) {
perror("out of memory!") ;
exit(EXIT_FAILURE) ;
}
p->item.low_value = item.low_value ;
p->item.up_value = item.up_value ;
p->next = NULL ;
return p;
}
void free_node(interval_node *p)
{
free(p);
}
void destroy_set(interval_node *head)
{
interval_node *q, *p = head;
while (p) {
q = p;
p = p->next;
free_node(q);
}
}
interval_node *copy_set(interval_node *src)
{
if (src == NULL) {
perror("source copy input is NULL!!") ;
exit(EXIT_FAILURE) ;
}
interval temp_interval ;
interval_node *iter = src, *result = NULL, *p = NULL ;
temp_interval.low_value = iter->item.low_value ;
temp_interval.up_value = iter->item.up_value ;
result = make_node(temp_interval) ;
p = result ;
while (iter->next != NULL) {
iter = iter->next ;
temp_interval.low_value = iter->item.low_value ;
temp_interval.up_value = iter->item.up_value ;
p->next = make_node(temp_interval) ;
p = p->next ;
}
return result ;
}
interval_node *search_node(interval_node *head, interval key)
{
interval_node *p;
for (p = head->next; p; p = p->next)
if (p->item.low_value == key.low_value && p->item.up_value == key.up_value)
return p;
return NULL;
}
void insert_node(interval_node *head, interval_node *p)
{
p->next = head->next;
head->next = p;
}
/*void delete(interval_node *head, interval_node *p)*/
/*{*/
/*interval_node *pre;*/
/*if (p == head) {*/
/*head = p->next;*/
/*return;*/
/*}*/
/*for (pre = head; pre; pre = pre->next)*/
/*if (pre->next == p) {*/
/*pre->next = p->next;*/
/*return;*/
/*}*/
/*}*/
/*void traverse(void (*visit)(interval_node *))*/
/*{*/
/*interval_node * p;*/
/*for (p = head; p; p = p->next)*/
/*visit(p);*/
/*}*/
/*void push(interval_node * p)*/
/*{*/
/*insert(p);*/
/*}*/
/*interval_node * pop(void)*/
/*{*/
/*if (head == NULL)*/
/*return NULL;*/
/*else {*/
/*interval_node * p = head;*/
/*head = head->next ;*/
/*return p;*/
/*}*/
/*}*/
//interval and set union operate, destroy old head and return new set in head.
void interval_set_union(interval a, interval_node *head) {
if (head == NULL) {
perror("input interval_set's head is NULL!") ;
exit(EXIT_FAILURE) ;
}
if (head->next == NULL) {
head->next = make_node(a) ;
return ;
}
interval_node *iter = NULL, *p = NULL, *q = NULL, *pre_p = NULL ;
unsigned int p_loc = 0, q_loc = 0, i = 1 ;
for (iter = head->next; iter; iter = iter->next) {
//handle the condition that a's low_value is before first interval
if (iter == head->next && a.low_value <= iter->item.up_value) {
p = iter ;
pre_p = head ;
p_loc = i ;
}
//find a's low_value's interval in the head.
else if (iter->next != NULL && a.low_value > iter->item.up_value && a.low_value <= (iter->next)->item.up_value) {
p = iter->next ;
pre_p = iter ;
p_loc = i + 1 ;
}
//handle the condition that a's up_value is over the last interval
if (iter->next == NULL && a.up_value >= iter->item.low_value) {
q = iter ;
q_loc = i ;
}
//find a's up_value's interval in the head
else if (iter->next != NULL && a.up_value >= iter->item.low_value && a.up_value < (iter->next)->item.low_value) {
q = iter ;
q_loc = i ;
}
i++ ;
}
interval_node *temp_node = NULL ;
//a is before the head
if (p != NULL && q == NULL) {
temp_node = make_node(a) ;
temp_node->next = head->next ;
head->next = temp_node ;
}
//a is over the head
else if (p == NULL && q != NULL) {
q->next = make_node(a) ;
}
//a is in the vacuum of two head's interval, directly insert a
else if (p_loc > q_loc) {
temp_node = make_node(a) ;
q->next = temp_node ;
temp_node->next = p ;
}
//a intersect head's some interval
else {
interval temp_interval ;
temp_interval.low_value = interval_value_min(p->item.low_value, a.low_value) ;
temp_interval.up_value = interval_value_max(q->item.up_value, a.up_value) ;
temp_node = make_node(temp_interval) ;
pre_p->next = temp_node ;
temp_node->next = q->next ;
q->next = NULL ;
//free unuseful node
destroy_set(p) ;
}
};
//interval and set intersect operate, destroy old head and return new set in head
void interval_set_intersect(interval a, interval_node *head) {
/* interval head_item ;*/
/*head_item.low_value = 0 ;*/
/*head_item.up_value = 0 ;*/
/*interval_node *result = make_node(head_item) ;*/
if (head == NULL) {
perror("input interval_set's head is NULL!") ;
exit(EXIT_FAILURE) ;
}
if (head->next == NULL) {
return ;
}
interval_node *iter = NULL, *p = NULL, *q = NULL, *pre_p = NULL ;
unsigned int p_loc = 0, q_loc = 0, i = 1 ;
for (iter = head->next; iter; iter = iter->next) {
if (iter == head->next && a.low_value <= iter->item.up_value) {
p = iter ;
pre_p = head ;
p_loc = i ;
}
else if (iter->next != NULL && a.low_value > iter->item.up_value && a.low_value <= (iter->next)->item.up_value) {
p = iter->next ;
pre_p = iter ;
p_loc = i + 1 ;
}
if (iter->next == NULL && a.up_value >= iter->item.low_value) {
q = iter ;
q_loc = i ;
}
else if (iter->next != NULL && a.up_value >= iter->item.low_value && a.up_value < (iter->next)->item.low_value) {
q = iter ;
q_loc = i ;
}
i++ ;
}
//a doesn't intersect with head
if ((p != NULL && q == NULL) || (p == NULL && q != NULL) || (p_loc > q_loc)) {
destroy_set(head->next) ;
head->next = NULL ;
return ;
}
//a is enclosed by a interval of head
else if (p == q) {
interval_node *temp_node = NULL ;
interval temp_interval ;
temp_interval.low_value = interval_value_max(p->item.low_value, a.low_value) ;
temp_interval.up_value = interval_value_min(q->item.up_value, a.up_value) ;
temp_node = make_node(temp_interval) ;
destroy_set(head->next) ;
head->next = temp_node ;
return ;
}
//a is intersect with some of the interval of head
else {
interval_node *temp_node_head = NULL, *temp_node_tail = NULL ;
interval temp_head, temp_tail ;
temp_head.low_value = interval_value_max(p->item.low_value, a.low_value) ;
temp_head.up_value = p->item.up_value ;
temp_tail.up_value = interval_value_min(q->item.up_value, a.up_value) ;
temp_tail.low_value = q->item.low_value ;
temp_node_head = make_node(temp_head) ;
temp_node_tail = make_node(temp_tail) ;
if (p->next != q) {
temp_node_head->next = p->next ;
iter = p ;
while (iter->next != q) {
iter = iter->next ;
}
iter->next = temp_node_tail ;
}
else {
temp_node_head->next = temp_node_tail ;
}
p->next = NULL ;
destroy_set(head->next) ;
destroy_set(q) ;
head->next = temp_node_head ;
return ;
}
};
//set and set union operate, no change head_a and head_b , return a new result use new dynamic space.
interval_node *set_set_union(interval_node *head_a, interval_node *head_b) {
if (head_a == NULL || head_b == NULL) {
perror("input interval_set's head is NULL!") ;
exit(EXIT_FAILURE) ;
}
//handle null and XN condition.
if ((head_a->next == NULL) || (head_a->item.low_value == MIN_VALUE && head_a->item.up_value == MAX_VALUE)) {
return copy_set(head_b) ;
}
if ((head_b->next == NULL) || (head_b->item.low_value == MIN_VALUE && head_b->item.up_value == MAX_VALUE)) {
return copy_set(head_a) ;
}
interval_node *iter = NULL, *result = NULL ;
result = copy_set(head_b) ;
//union set and set
iter = head_a->next ;
while (iter != NULL) {
interval_set_union(iter->item, result) ;
iter = iter->next ;
}
return result ;
};
//set and set union operate, no change head_a and head_b, return a new result use new dynamic space.
interval_node *set_set_intersect(interval_node *head_a, interval_node *head_b) {
if (head_a == NULL || head_b == NULL) {
perror("input interval_set's head is NULL!") ;
exit(EXIT_FAILURE) ;
}
interval_node *iter = NULL, *temp_head = NULL, *result = NULL ;
interval temp_interval ;
temp_interval.low_value = 0 ;
temp_interval.up_value = 0 ;
result = make_node(temp_interval) ;
if (head_a->item.low_value == MIN_VALUE && head_a->item.up_value == MAX_VALUE) {
return copy_set(head_b) ;
}
else if (head_b->item.low_value == MIN_VALUE && head_b->item.up_value == MAX_VALUE) {
return copy_set(head_a) ;
}
else if (head_a->next == NULL || head_b->next == NULL) {
return result ;
}
iter = head_a->next ;
//intersect set and set
while (iter != NULL) {
temp_head = copy_set(head_b) ;
interval_set_intersect(iter->item, temp_head) ;
result = set_set_union(temp_head, result) ;
iter = iter->next ;
}
return result ;
};
//set and set add, sub, mul, div operate, 1 --add 2 -- sub 3 -- mul 4 -- div
interval_node *set_set_arithmetic(interval_node *head_a, interval_node *head_b, unsigned int operator_in) {
if (head_a == NULL || head_b == NULL) {
perror("input interval_set's head is NULL!") ;
exit(EXIT_FAILURE) ;
}
if (operator_in != 1 && operator_in != 2 && operator_in != 3 && operator_in != 4) {
perror("operator_in must be from 1 to 4!") ;
exit(EXIT_FAILURE) ;
}
interval_node *iter_a = NULL, *iter_b = NULL, *temp_head = NULL, *result = NULL ;
interval temp_interval ;
if ((head_a->item.low_value == MIN_VALUE && head_a->item.up_value == MAX_VALUE) ||
(head_b->item.low_value == MIN_VALUE && head_b->item.up_value == MAX_VALUE)) {
temp_interval.low_value == MIN_VALUE ;
temp_interval.up_value == MAX_VALUE ;
result = make_node(temp_interval) ;
return result;
}
else if (head_a->next == NULL || head_b->next == NULL) {
perror("input interval set's member is NULL, can't calculate set and set arithmetic!") ;
exit(EXIT_FAILURE) ;
}
temp_interval.low_value = 0 ;
temp_interval.up_value = 0 ;
result = make_node(temp_interval) ;
iter_a = head_a->next ;
iter_b = head_b->next ;
//calculate result
while (iter_a != NULL) {
while (iter_b != NULL) {
if (operator_in == 1) {
interval_add(&iter_a->item, &iter_b->item, &temp_interval) ;
interval_set_union(temp_interval, result) ;
}
else if (operator_in == 2) {
interval_sub(&iter_a->item, &iter_b->item, &temp_interval) ;
interval_set_union(temp_interval, result) ;
}
else if (operator_in == 3) {
interval_mul(&iter_a->item, &iter_b->item, &temp_interval) ;
interval_set_union(temp_interval, result) ;
}
else if (operator_in == 4) {
interval_div(&iter_a->item, &iter_b->item, &temp_interval) ;
interval_set_union(temp_interval, result) ;
}
iter_b = iter_b->next ;
}
iter_b = head_b->next ;
iter_a = iter_a->next ;
}
return result ;
};