-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_comparison.c
132 lines (122 loc) · 3.18 KB
/
time_comparison.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
//
// Created by IamaU on 01/12/2023.
//
#include "time_comparison.h"
int int_pow(int x, int y){
int tmp = x;
if(y == 1){
return 1;
}
for(int i = 0; i < y-1; i++){
x = x * tmp;
}
return x;
}
int * generate_array(int n){
int size = pow(2,n) - 1;
int * tab = (int *)malloc(size * sizeof(int));
for(int i = 0; i < size; i++){
tab[i] = 0;
}
int j = 2;
while(j <= size){
for(int i = 0; i < size; i++){
if((i+1) % j == 0){
tab[i] ++;
}
}
j = j * 2;
}
return tab;
}
t_d_list * create_time_comparison_list(int n){
int * tab = generate_array(n);
t_d_list * L = create_list(n);
int size = pow(2,n) - 1;
int cell_level;
for(int i = 0; i < size; i++){
cell_level = tab[i] + 1;
insert_cell(L, create_cell(i+1,cell_level));
}
return L;
}
int classic_seek_value(t_d_list * L, int n){
t_d_cell * tmp = L->level[0];
int i = 1;
while(tmp != NULL){
if(tmp->value == n){
return i;
}
tmp = tmp->level[0];
i++;
}
return 0;
}
int advanced_seek_value(t_d_list *L, int n){
if(L->size == 1){
return classic_seek_value(L, n);
}
else {
int pretmp_lvl = L->size - 2;
int i = pow(2, pretmp_lvl+1);
t_d_cell *pretmp = L->level[pretmp_lvl];
t_d_cell *tmp = L->level[pretmp_lvl+1];
while(tmp->value >= n && tmp == L->level[pretmp_lvl+1] && pretmp_lvl > 0){
if(tmp->value == n){
return i;
}
else{
pretmp_lvl--;
i = pow(2, pretmp_lvl+1);
pretmp = L->level[pretmp_lvl];
tmp = L->level[pretmp_lvl+1];
}
}
if(pretmp_lvl == 0){
i = i - pow(2, pretmp_lvl);
while(pretmp != NULL){
if(pretmp->value == n){
return i;
}
pretmp = pretmp->level[0];
i++;
}
return 0; //everything is supposed ok here
}
else{
while(pretmp_lvl > 0){
if(tmp == NULL){
i = i - pow(2, pretmp_lvl);
tmp = pretmp->level[pretmp_lvl];
pretmp_lvl--;
}
else{
if(tmp->value <= n){
if(tmp->value == n){
return i;
}
else{
pretmp = tmp;
tmp = tmp->level[pretmp_lvl + 1];
i = i + pow(2, pretmp_lvl+1);
}
}
else{
i = i - pow(2, pretmp_lvl);
tmp = pretmp->level[pretmp_lvl];
pretmp_lvl--;
}
}
}
i = i - pow(2, pretmp_lvl+1);
while(pretmp != NULL){
if(pretmp->value == n){
return i;
}
pretmp = pretmp->level[0];
i++;
}
return 0;
}
}
}