aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_sort_s_t.c
blob: 9cbde8b5b7fa14170a0cf00c4e6985386645af4f (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_sort_s_t.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/24 20:22:45 by rbousset          #+#    #+#             */
/*   Updated: 2020/03/09 18:56:01 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stdlib.h>
#include <stdint.h>

float
	**ft_alloc_dist_tab(void)
{
	uint16_t	i;
	float		**dist_tab;

	if (!(dist_tab = (float **)malloc((FT_TOTAL_SPRT + 1) * sizeof(float*))))
		return (NULL);
	i = 0;
	while (i < FT_TOTAL_SPRT + 1)
	{
		if (!(dist_tab[i] = (float *)malloc(4096 * sizeof(float))))
			return (NULL);
		i++;
	}
	return (dist_tab);
}

static void
	ft_init_st(int32_t *st_o)
{
	int8_t	i;

	i = 0;
	while (i < FT_TOTAL_SPRT)
	{
		st_o[i] = i;
		i++;
	}
}

static void
	ft_swap_them(int16_t i, float **dist_tab)
{
	float       tmp_i[4096];

	ft_memcpy(tmp_i, dist_tab[i], 4096);
	ft_memcpy(dist_tab[i], dist_tab[i + 1], 4096);
	ft_memcpy(dist_tab[i + 1], tmp_i, 4096);
}

void
	ft_sort_s_t(t_cub *cl, float **dist_tab)
{
	int16_t		i;
	int32_t     tmp;

	i = -1;
	ft_init_st(cl->mlist.st_o);
	while (++i < FT_TOTAL_SPRT)
	{
		if (i < FT_TOTAL_SPRT && dist_tab[i][0]
			&& dist_tab[i][0] < dist_tab[i + 1][0])
		{
			tmp = cl->mlist.st_o[i];
			cl->mlist.st_o[i] = cl->mlist.st_o[i + 1];
			cl->mlist.st_o[i + 1] = tmp;
			ft_swap_them(i, dist_tab);
			i = -1;
		}
	}
	i = -1;
	while (++i < FT_TOTAL_SPRT + 1)
		ft_memdel((void*)&dist_tab[i]);
	ft_memdel((void*)&dist_tab);
}