KDArray.c
/**
 * sort array of points with respect to given axis
 * 
 * @pre   pts array contains points of the same dimension
 * @param ret - int array to be updated with the sorted indices
 * @param pts - opint array to be sorted
 * @param axis - axis to sort by
 * @param config - configuration struct
 * @param log_msg - logger message to be updated
 * @post ret contains the indices of the points sorted with respect to the given axis coordinate
 * @return 0 for success, 1 otherwise
 */
sort array of points with respect to given axis


sort array of points with respect to given axis
int sortByAxis(int *ret, const SPPoint* pts, int size, int axis,
		const SPConfig config, SP_LOGGER_MSG* log_msg) {
	if (!pts || axis < 0 || !config) {
		InvalidError()
		return 1;
	}
	PointAxis* ptsAx = (PointAxis*) malloc(size * sizeof(*ptsAx));
	if (!ptsAx) {
		MallocError()
		return 1;
	}
	for (int i = 0; i < size; i++) {
		ptsAx[i] = (PointAxis) malloc(sizeof(*ptsAx[i]));
		if (!ptsAx[i]) {
			MallocError()
			for (i--; i >= 0; i--) {
				free(ptsAx[i]);
			}
			free(ptsAx);
			return 1;
		}
		ptsAx[i]->pnt = pts[i];
		ptsAx[i]->axis = axis;
		ptsAx[i]->indexInPntArray = i;
	}
	qsort(ptsAx, size, sizeof(*ptsAx), pointComparator);
	for (int i = 0; i < size; i++)
		ret[i] = ptsAx[i]->indexInPntArray;
	for (int i = size - 1; i >= 0; i--) {
		free(ptsAx[i]);
	}
	free(ptsAx);
	return 0;
}
int sortByAxis ( int * ret , const SPPoint * pts , int size , int axis , const SPConfig config , SP_LOGGER_MSG * log_msg ) { if ( ! pts || axis < 0 || ! config ) { InvalidError ( ) return 1 ; } PointAxis * ptsAx = ( PointAxis * ) malloc ( size * sizeof ( * ptsAx ) ) ; if ( ! ptsAx ) { MallocError ( ) return 1 ; } for ( int i = 0 ; i < size ; i ++ ) { ptsAx [ i ] = ( PointAxis ) malloc ( sizeof ( * ptsAx [ i ] ) ) ; if ( ! ptsAx [ i ] ) { MallocError ( ) for ( i -- ; i >= 0 ; i -- ) { free ( ptsAx [ i ] ) ; } free ( ptsAx ) ; return 1 ; } ptsAx [ i ] -> pnt = pts [ i ] ; ptsAx [ i ] -> axis = axis ; ptsAx [ i ] -> indexInPntArray = i ; } qsort ( ptsAx , size , sizeof ( * ptsAx ) , pointComparator ) ; for ( int i = 0 ; i < size ; i ++ ) ret [ i ] = ptsAx [ i ] -> indexInPntArray ; for ( int i = size - 1 ; i >= 0 ; i -- ) { free ( ptsAx [ i ] ) ; } free ( ptsAx ) ; return 0 ; }
sort array of points with respect to given axis
sort array of points with respect to given axis
/*Malloc fail */