-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement segment based optimization
- Loading branch information
Showing
3 changed files
with
162 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,36 +6,58 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/02/14 14:44:38 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/14 14:45:03 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/14 16:22:59 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "large_sort.h" | ||
#include "push_swap.h" | ||
|
||
void set_is_opt(t_stack **p_b) | ||
static int _calc_max_segment(t_stack *st_b) | ||
{ | ||
int i; | ||
t_stack *st_b; | ||
int size_b; | ||
int opt_cost; | ||
int min_index; | ||
int max_seg; | ||
|
||
size_b = stack_size(*p_b); | ||
i = 0; | ||
st_b = *p_b; | ||
opt_cost = INT_MAX; | ||
min_index = -1; | ||
while (i < size_b) | ||
max_seg = INT_MIN; | ||
while (st_b) | ||
{ | ||
if (ft_chmin(&opt_cost, get_content(st_b)->min_cost)) | ||
min_index = i; | ||
ft_chmax(&max_seg, get_content(st_b)->segment); | ||
st_b = st_b->next; | ||
i++; | ||
} | ||
return (max_seg); | ||
} | ||
|
||
static int _calc_target_index(t_stack *st_b, int max_seg) | ||
{ | ||
int target_index; | ||
int cost; | ||
int i; | ||
|
||
target_index = -1; | ||
cost = INT_MAX; | ||
i = 0; | ||
while (st_b) | ||
{ | ||
if (get_content(st_b)->segment == max_seg && ft_chmin(&cost, | ||
get_content(st_b)->min_cost)) | ||
target_index = i; | ||
i++; | ||
st_b = st_b->next; | ||
} | ||
return (target_index); | ||
} | ||
|
||
void set_is_opt(t_stack **p_b) | ||
{ | ||
int i; | ||
t_stack *st_b; | ||
int max_seg; | ||
int target_index; | ||
|
||
st_b = *p_b; | ||
while (i++ < min_index) | ||
max_seg = _calc_max_segment(st_b); | ||
target_index = _calc_target_index(st_b, max_seg); | ||
i = 0; | ||
while (i++ < target_index) | ||
st_b = st_b->next; | ||
get_content(st_b)->is_opt = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters