-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc_test.c
More file actions
136 lines (115 loc) · 4.71 KB
/
malloc_test.c
File metadata and controls
136 lines (115 loc) · 4.71 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "/usr/include/sqlite3.h"
int count_element( void * , int , char ** , char ** );
int element_number = -1;//テストデータのデータ数
int RANGE = 50;//gridで区切る格子の大きさ(初期)
int SCOPE = 1000;//x,y座標の最大値
int init(void) {//データベースのデータを格子状に整理する
sqlite3 *conn = NULL;
sqlite3_stmt *stmt = NULL;
sqlite3_stmt *stmt_select_point_table_by_id = NULL;
int ret = 0, max_score_in_grid = 0, i, j, get_x_location, get_y_location, get_score;
int **grid_matrix;
char find_id_sql[256];
const char *insert_to_grid_table_sql = "insert into grid_table(x_range, y_range, number) values(?, ?, ?)";
//gridの初期化
grid_matrix = (int**)malloc(sizeof(int*) * SCOPE );
for (i = 0 ; i < SCOPE/RANGE ; i++ ){
grid_matrix[i] = (int*)malloc(sizeof(int*) * SCOPE );
}
for(i = 0; i < SCOPE/RANGE; i++){
for(j = 0; j < SCOPE/RANGE; j++){
grid_matrix[i][j] = 0;
}
}
ret = sqlite3_open("study.sqlite", &conn);//SQLite3に接続
if(ret != SQLITE_OK){
printf("database is not open!\n");
return -1;
}
sqlite3_exec(conn, "delete from grid_table;", NULL, NULL, NULL);//データをリセット
sqlite3_prepare(conn, insert_to_grid_table_sql, strlen(insert_to_grid_table_sql), &stmt, NULL);
sqlite3_exec(conn, "select count(score) from point_table;", count_element, NULL, NULL);//データの要素数のカウント
for(i=1;i<element_number+1;i++){//grid_matrixにセット
//idがiの要素のx_location,y_location,scoreを取得
sprintf(find_id_sql, "select * from point_table where id = %d;", i);
sqlite3_prepare_v2(conn, find_id_sql, 128, &stmt_select_point_table_by_id, NULL);
while(sqlite3_step(stmt_select_point_table_by_id) == SQLITE_ROW ){
get_x_location = sqlite3_column_int(stmt_select_point_table_by_id, 1);
get_y_location = sqlite3_column_int(stmt_select_point_table_by_id, 2);
get_score = sqlite3_column_int(stmt_select_point_table_by_id, 3);
//gridに値を挿入
/*if(grid_matrix[get_x_location/RANGE][get_y_location/RANGE] == ""){
grid_matrix[get_x_location/RANGE][get_y_location/RANGE] = get_score;
}else{*/
grid_matrix[get_x_location/RANGE][get_y_location/RANGE] += get_score;
//}
if(grid_matrix[get_x_location/RANGE][get_y_location/RANGE] > max_score_in_grid){
max_score_in_grid = grid_matrix[get_x_location/RANGE][get_y_location/RANGE];
}
}
}
printf("%d\n",RANGE);
printf("%d\n",max_score_in_grid);
//領域の大きさと最大スコアが等しくなるように調整する
if(max_score_in_grid != RANGE){
printf("change_");
RANGE = max_score_in_grid;
/*grid_matrix = (int **)realloc(grid_matrix,sizeof(int*) * SCOPE/RANGE);
for(i = 0; i < SCOPE/RANGE; i++){
grid_matrix[i] = (int *)realloc(grid_matrix[i],sizeof(int*) * SCOPE/RANGE);
}
for(i = 0; i < SCOPE/RANGE; i++){
for(j = 0; j < SCOPE/RANGE; j++){
grid_matrix[i][j] = 0;
}
}*/
for(i=1;i<element_number+1;i++){
sprintf(find_id_sql, "select * from point_table where id = %d;", i);
sqlite3_prepare_v2(conn, find_id_sql, 128, &stmt_select_point_table_by_id, NULL);
while(sqlite3_step(stmt_select_point_table_by_id) == SQLITE_ROW ){
get_x_location = sqlite3_column_int(stmt_select_point_table_by_id, 1);
get_y_location = sqlite3_column_int(stmt_select_point_table_by_id, 2);
get_score = sqlite3_column_int(stmt_select_point_table_by_id, 3);
}
//gridに値を挿入
grid_matrix[get_x_location/RANGE][get_y_location/RANGE] += get_score;
if(grid_matrix[get_x_location/RANGE][get_y_location/RANGE] > max_score_in_grid){
max_score_in_grid = grid_matrix[get_x_location/RANGE][get_y_location/RANGE];
}
}
}
for(i = 0; i < SCOPE/RANGE; i++){
for(j = 0; j < SCOPE/RANGE; j++){
printf("a%d\n", grid_matrix[i][j]);
}
}
//grid_tableにセット
for(i=0; i<SCOPE/RANGE; i++){
for(j=0; j<SCOPE/RANGE; j++){
if(grid_matrix[i][j] != 0){
sqlite3_reset(stmt);
sqlite3_bind_int(stmt, 1, i);
sqlite3_bind_int(stmt, 2, j);
sqlite3_bind_int(stmt, 3, grid_matrix[i][j]);
while(SQLITE_DONE != sqlite3_step(stmt)){}
}
}
}
//終了処理
for (i=0 ; i < SCOPE/RANGE ; i++){
free(grid_matrix[i]);
}
free(grid_matrix);
sqlite3_finalize(stmt);
sqlite3_finalize(stmt_select_point_table_by_id);
sqlite3_close(conn);
return 0;
}
//コールバック関数
int count_element(void *get_prm, int col_cnt, char **row_txt, char **col_name){
element_number = atoi(row_txt[0]);//要素数をグローバル関数にセット
return 0;
}