-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils2.c
More file actions
121 lines (110 loc) · 2.53 KB
/
Copy pathutils2.c
File metadata and controls
121 lines (110 loc) · 2.53 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: isel-azz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/11 07:53:00 by isel-azz #+# #+# */
/* Updated: 2024/05/11 07:53:03 by isel-azz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack_needs *get_smallest(t_stack_needs *stack)
{
long smallest;
t_stack_needs *node;
if (stack == NULL)
return (NULL);
smallest = LONG_MAX;
while (stack)
{
if (stack->value < smallest)
{
smallest = stack->value;
node = stack;
}
stack = stack->next;
}
return (node);
}
void set_targets(t_stack_needs *a, t_stack_needs *b)
{
t_stack_needs *a_now;
t_stack_needs *target;
long best_match;
while (b)
{
best_match = LONG_MAX;
a_now = a;
while (a_now)
{
if (a_now->value > b->value && a_now->value < best_match)
{
best_match = a_now->value;
target = a_now;
}
a_now = a_now->next;
}
if (best_match == LONG_MAX)
b->target = get_smallest(a);
else
b->target = target;
b = b->next;
}
}
void set_push_cost(t_stack_needs *a, t_stack_needs *b)
{
int a_len;
int b_len;
a_len = stack_len(a);
b_len = stack_len(b);
while (b)
{
b->push_cost = b->pos;
if (!(b->up_down))
b->push_cost = b_len - (b->pos);
if (b->target->up_down)
b->push_cost += b->target->pos;
else
b->push_cost += a_len - b->target->pos;
b = b->next;
}
}
void set_positions(t_stack_needs *stack)
{
int center;
int i;
if (stack == NULL)
return ;
i = 0;
center = stack_len(stack) / 2;
while (stack)
{
stack->pos = i;
if (i <= center)
stack->up_down = true;
else
stack->up_down = false;
stack = stack->next;
i++;
}
}
void set_priority(t_stack_needs *b)
{
long best_match;
t_stack_needs *node;
if (b == NULL)
return ;
best_match = LONG_MAX;
while (b)
{
if (b->push_cost < best_match)
{
best_match = b->push_cost;
node = b;
}
b = b->next;
}
node->priority = true;
}