Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New binary search added #196

Merged
merged 3 commits into from
Nov 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions tardis/cmontecarlo.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ INLINE tardis_error_t line_search(double *nu, double nu_insert, int64_t number_o
}
else
{
ret_val = binary_search(nu, nu_insert, imin, imax, result);
ret_val = reverse_binary_search(nu, nu_insert, imin, imax, result);
*result = *result + 1;
}
return ret_val;
}

INLINE tardis_error_t binary_search(double *x, double x_insert, int64_t imin, int64_t imax, int64_t *result)
inline tardis_error_t reverse_binary_search(double *x, double x_insert,
int64_t imin, int64_t imax,
int64_t * result)
{
/*
Have in mind that *x points to a reverse sorted array.
Expand Down Expand Up @@ -59,9 +61,39 @@ INLINE tardis_error_t binary_search(double *x, double x_insert, int64_t imin, in
if (imax - imid == 2 && x_insert < x[imin + 1])
{
*result = imin + 1;
} else {
*result = imin;
}
else
}
return ret_val;
}

inline tardis_error_t binary_search(double *x, double x_insert, int64_t imin,
int64_t imax, int64_t * result)
{
/*
Have in mind that *x points to a sorted array.
Like [1,2,3,4,5,...]
*/
int imid;
tardis_error_t ret_val = TARDIS_ERROR_OK;
if (x_insert < x[imin] || x_insert > x[imax]) {
ret_val = TARDIS_ERROR_BOUNDS_ERROR;
} else {
while (imax >= imin) {
imid = (imin + imax) / 2;
if (x[imid] == x_insert) {
*result = imid;
break;
} else if (x[imid] < x_insert) {
imin = imid + 1;
} else {
imax = imid - 1;
}
}
if (imax - imid == 2 && x_insert < x[imin + 1]) {
*result = imin;
} else {
*result = imin;
}
}
Expand Down
16 changes: 15 additions & 1 deletion tardis/cmontecarlo.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,21 @@ typedef void (*montecarlo_event_handler_t)(rpacket_t *packet, storage_model_t *s
*
* @return index of the next boundary to the left
*/
inline tardis_error_t binary_search(double *x, double x_insert, int64_t imin, int64_t imax, int64_t *result);
inline tardis_error_t reverse_binary_search(double *x, double x_insert,
int64_t imin, int64_t imax,
int64_t * result);

/** Look for a place to insert a value in a sorted float array.
*
* @param x a (lowest to largest) sorted float array
* @param x_insert a value to insert
* @param imin lower bound
* @param imax upper bound
*
* @return index of the next boundary to the left
*/
inline tardis_error_t binary_search(double *x, double x_insert, int64_t imin,
int64_t imax, int64_t * result);

/** Insert a value in to an array of line frequencies
*
Expand Down