-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSPKDArray.c
More file actions
241 lines (222 loc) · 6.94 KB
/
SPKDArray.c
File metadata and controls
241 lines (222 loc) · 6.94 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// Created by Ofir on 15/03/2017.
//
#include <stdlib.h>
#include <stdbool.h>
#include "SPKDArray.h"
#include "SPBPriorityQueue.h"
#include <stdio.h>
struct sp_kd_array_t{
SPPoint** pointsArray;
int** sortArray;
int dimension;
int numOfPoints;
};
void spDestroyKDArrayWithDim(SPKDArray arr, int lastInitializedDim) {
if(arr){
for (int i = 0; i < arr->dimension && i < lastInitializedDim; ++i) {
free(arr->sortArray[i]);
}
for (int i = 0; i < arr->numOfPoints; ++i) {
spPointDestroy(arr->pointsArray[i]);
}
free(arr->sortArray);
free(arr->pointsArray);
free(arr);
}
}
void spDestroyKDArray(SPKDArray arr){
if (arr){
spDestroyKDArrayWithDim(arr, arr->dimension);
}
}
SPKDArray spKDArrayConstructor(int dimension, int size){
if(dimension < 1 || size < 1){
return NULL;
}
SPKDArray arr = malloc(sizeof(struct sp_kd_array_t));
if(!arr){
return NULL;
}
arr->dimension = dimension;
arr->numOfPoints = size;
arr->pointsArray = malloc(size * sizeof(SPPoint*));
if(!(arr->pointsArray)){
free(arr);
return NULL;
}
arr->sortArray = malloc(dimension * sizeof(int*));
if(!(arr->sortArray)){
free(arr->pointsArray);
free(arr);
return NULL;
}
for (int i = 0; i < dimension; ++i) {
arr->sortArray[i] = malloc(size * sizeof(int));
if(!(arr->sortArray[i])){
spDestroyKDArrayWithDim(arr, i);
return NULL;
}
}
return arr;
}
int elementByValueComparator(const void *element1, const void *element2){
double value1 = (*(BPQueueElement*)element1).value;
double value2 = (*(BPQueueElement*)element2).value;
if(value1 - value2 == 0){
return 0;
}
else if( value1 - value2 > 0){
return 1;
}
return -1;
}
SPKDArray spInitSPKDArray(SPPoint** pointsArr, int arrSize){
if(!pointsArr || arrSize < 1){ // input check
return NULL;
}
int pointDim = spPointGetDimension(pointsArr[0]); // basic KDArray initialization
SPKDArray array = spKDArrayConstructor(pointDim, arrSize);
if (!array){
return NULL;
}
for (int i = 0; i < arrSize; ++i) { // update to concrete points array
if(pointsArr[i]){
array->pointsArray[i] = spPointCopy(pointsArr[i]);
}
}
BPQueueElement* sortingArr = malloc(arrSize * sizeof(BPQueueElement)); // initialize helper array
if (!sortingArr) {
spDestroyKDArrayWithDim(array, pointDim);
return NULL;
}
for (int i = 0; i < pointDim; ++i) { // sorting by all coordinates
for (int j = 0; j < arrSize; ++j) {
sortingArr[j].index = j;
sortingArr[j].value = spPointGetAxisCoor(array->pointsArray[j], i);
}
qsort(sortingArr, (size_t) arrSize, sizeof(BPQueueElement), elementByValueComparator);
for (int j = 0; j < arrSize; ++j) {
array->sortArray[i][j] = sortingArr[j].index;
}
}
free(sortingArr);
return array;
}
SPKDArray* spSplitSPKDArray(SPKDArray kdArr, int coor){
if(!kdArr || coor < 0 || coor >= kdArr->dimension){
return NULL;
}
int size = kdArr->numOfPoints;
int dim = kdArr->dimension;
int rightSize = size / 2;
int leftSize = size - rightSize;
SPKDArray kdLeft = spKDArrayConstructor(dim, leftSize);
if(!kdLeft){
return NULL;
}
SPKDArray kdRight = spKDArrayConstructor(dim, rightSize);
if(!kdRight){
spDestroyKDArrayWithDim(kdLeft, kdLeft->dimension);
return NULL;
}
bool* isInLeft = malloc(size * sizeof(bool));
if (!isInLeft){
spDestroyKDArrayWithDim(kdLeft, dim);
spDestroyKDArrayWithDim(kdRight, dim);
return NULL;
}
int* newIndices = malloc(size * sizeof(int));
if (!newIndices){
free(isInLeft);
spDestroyKDArrayWithDim(kdLeft, dim);
spDestroyKDArrayWithDim(kdRight, dim);
return NULL;
}
for (int i = 0; i < leftSize; ++i) {
int currIndex = kdArr->sortArray[coor][i];
kdLeft->pointsArray[i] = spPointCopy(kdArr->pointsArray[currIndex]);
isInLeft[kdArr->sortArray[coor][i]] = true;
newIndices[currIndex] = i;
}
for (int i = leftSize; i < size; ++i) {
int currIndex = kdArr->sortArray[coor][i];
kdRight->pointsArray[i - leftSize] = spPointCopy(kdArr->pointsArray[currIndex]);
isInLeft[kdArr->sortArray[coor][i]] = false;
newIndices[currIndex] = i - leftSize;
}
for (int i = 0; i < dim; ++i) {
int leftIndex = 0;
int rightIndex = 0;
for (int j = 0; j < size; ++j) {
int index = kdArr->sortArray[i][j];
if(isInLeft[index]){
kdLeft->sortArray[i][leftIndex] = newIndices[index];
leftIndex++;
}
else{
kdRight->sortArray[i][rightIndex] = newIndices[index];
rightIndex++;
}
}
}
free(newIndices);
free(isInLeft);
SPKDArray* twoKDArrays = malloc(2 * sizeof(SPKDArray));
if (!twoKDArrays){
spDestroyKDArrayWithDim(kdLeft, dim);
spDestroyKDArrayWithDim(kdRight, dim);
return NULL;
}
twoKDArrays[0] = kdLeft;
twoKDArrays[1] = kdRight;
return twoKDArrays;
}
SPPoint* spGetSPKDArrayPoint(SPKDArray kdArr, int index){
if(!kdArr || index < 0 || index >= kdArr->numOfPoints || !(kdArr->pointsArray)){
return NULL;
}
return spPointCopy(kdArr->pointsArray[index]);
}
int spGetSPKDArraySize(SPKDArray kdArr){
if(!kdArr){
return 0;
}
return kdArr->numOfPoints;
}
char* spPrintKDArrayDetails(SPKDArray kdArr){
if(kdArr){
int size = kdArr->numOfPoints;
int dim = kdArr->dimension;
char* msg = malloc(10000 * sizeof(char));
if(!msg){
return "Allocation Error";
}
sprintf(msg, "The size of the array is : %d\n"
"The dimension of the array is : %d\n"
"The points in the array are :\n", size, dim);
for (int i = 0; i < size; ++i) {
sprintf(msg, "%spoint %2d : (", msg,i);
for (int j = 0; j < dim; ++j) {
if(j > 0){
sprintf(msg, "%s, ", msg);
}
sprintf(msg, "%s%2lf", msg,spPointGetAxisCoor(kdArr->pointsArray[i], j));
}
sprintf(msg, "%s)\n", msg);
}
sprintf(msg, "%sThe sorted indices in the array are :\n", msg);
for (int i = 0; i < dim; ++i) {
sprintf(msg, "%sdimension %2d : (", msg, i);
for (int j = 0; j < size; ++j) {
if(j > 0){
sprintf(msg, "%s, ", msg);
}
sprintf(msg, "%s%2d", msg, kdArr->sortArray[i][j]);
}
sprintf(msg, "%s)\n", msg);
}
return msg;
}
return "Debug : The argument kdArr is NULL.";
}