-
Notifications
You must be signed in to change notification settings - Fork 3
/
detect_cycle.c
99 lines (78 loc) · 2 KB
/
detect_cycle.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
/*
* detect_cycle.c -- Generic cycle detection using Brent's algorithm
*
* Created by: Philipp Rudo <[email protected]>
*
* Copyright (c) 2022 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdlib.h>
#include "detect_cycle.h"
struct detect_cycle {
/* First entry of the list */
void *head;
/* Variables required by Brent's algorithm */
void *fast_p;
void *slow_p;
unsigned long length;
unsigned long power;
/* Function to get the next entry in the list */
dc_next_t next;
/* Private data passed to next */
void *data;
};
struct detect_cycle *dc_init(void *head, void *data, dc_next_t next)
{
struct detect_cycle *new;
new = malloc(sizeof(*new));
if (!new)
return NULL;
new->next = next;
new->data = data;
new->head = head;
new->slow_p = head;
new->fast_p = head;
new->length = 0;
new->power = 2;
return new;
}
int dc_next(struct detect_cycle *dc, void **next)
{
if (dc->length == dc->power) {
dc->length = 0;
dc->power *= 2;
dc->slow_p = dc->fast_p;
}
dc->fast_p = dc->next(dc->fast_p, dc->data);
dc->length++;
if (dc->slow_p == dc->fast_p)
return 1;
*next = dc->fast_p;
return 0;
}
void dc_find_start(struct detect_cycle *dc, void **first, unsigned long *len)
{
void *slow_p, *fast_p;
unsigned long tmp;
slow_p = fast_p = dc->head;
tmp = dc->length;
while (tmp) {
fast_p = dc->next(fast_p, dc->data);
tmp--;
}
while (slow_p != fast_p) {
slow_p = dc->next(slow_p, dc->data);
fast_p = dc->next(fast_p, dc->data);
}
*first = slow_p;
*len = dc->length;
}