2013-03-25 12:04:10 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2013-03-25 12:04:10 +00:00
|
|
|
*
|
|
|
|
* This software is licensed under the terms of the Apache Licence Version 2.0
|
|
|
|
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
|
|
|
|
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* Enrico Fucile
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "grib_api_internal.h"
|
2017-08-24 09:49:17 +00:00
|
|
|
|
|
|
|
/* For debugging purposes */
|
2017-08-23 12:46:01 +00:00
|
|
|
void grib_darray_print(const char* title, const grib_darray* darray)
|
|
|
|
{
|
|
|
|
size_t i;
|
2017-10-24 16:27:14 +00:00
|
|
|
Assert(darray);
|
2017-08-25 16:32:07 +00:00
|
|
|
printf("%s: darray.n=%lu \t", title, (unsigned long)darray->n);
|
2020-01-22 13:10:59 +00:00
|
|
|
for (i = 0; i < darray->n; i++) {
|
2017-08-25 16:32:07 +00:00
|
|
|
printf("darray[%lu]=%g\t", (unsigned long)i, darray->v[i]);
|
2017-08-23 12:46:01 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_darray* grib_darray_new_from_array(grib_context* c, double* a, size_t size)
|
2016-02-19 13:59:41 +00:00
|
|
|
{
|
2019-08-01 16:42:14 +00:00
|
|
|
size_t i;
|
|
|
|
grib_darray* v;
|
2016-02-19 13:59:41 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
if (!c)
|
|
|
|
c = grib_context_get_default();
|
2016-02-19 13:59:41 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
v = grib_darray_new(c, size, 100);
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
v->v[i] = a[i];
|
|
|
|
v->n = size;
|
|
|
|
v->context = c;
|
2019-08-01 16:42:14 +00:00
|
|
|
return v;
|
2016-02-19 13:59:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_darray* grib_darray_new(grib_context* c, size_t size, size_t incsize)
|
|
|
|
{
|
|
|
|
grib_darray* v = NULL;
|
|
|
|
if (!c)
|
|
|
|
c = grib_context_get_default();
|
|
|
|
v = (grib_darray*)grib_context_malloc_clear(c, sizeof(grib_darray));
|
2019-08-01 16:42:14 +00:00
|
|
|
if (!v) {
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_context_log(c, GRIB_LOG_ERROR,
|
|
|
|
"grib_darray_new unable to allocate %d bytes\n", sizeof(grib_darray));
|
2019-08-01 16:42:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-22 13:10:59 +00:00
|
|
|
v->size = size;
|
|
|
|
v->n = 0;
|
|
|
|
v->incsize = incsize;
|
|
|
|
v->v = (double*)grib_context_malloc_clear(c, sizeof(double) * size);
|
2019-08-01 16:42:14 +00:00
|
|
|
if (!v->v) {
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_context_log(c, GRIB_LOG_ERROR,
|
|
|
|
"grib_darray_new unable to allocate %d bytes\n", sizeof(double) * size);
|
2019-08-01 16:42:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return v;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_darray* grib_darray_resize(grib_context* c, grib_darray* v)
|
|
|
|
{
|
|
|
|
int newsize = v->incsize + v->size;
|
2019-08-01 16:42:14 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
if (!c)
|
|
|
|
c = grib_context_get_default();
|
2019-08-01 16:42:14 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
v->v = (double*)grib_context_realloc(c, v->v, newsize * sizeof(double));
|
|
|
|
v->size = newsize;
|
2019-08-01 16:42:14 +00:00
|
|
|
if (!v->v) {
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_context_log(c, GRIB_LOG_ERROR,
|
|
|
|
"grib_darray_resize unable to allocate %d bytes\n", sizeof(double) * newsize);
|
2019-08-01 16:42:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return v;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_darray* grib_darray_push(grib_context* c, grib_darray* v, double val)
|
|
|
|
{
|
|
|
|
size_t start_size = 100;
|
|
|
|
size_t start_incsize = 100;
|
|
|
|
if (!v)
|
|
|
|
v = grib_darray_new(c, start_size, start_incsize);
|
|
|
|
|
|
|
|
if (v->n >= v->size)
|
|
|
|
v = grib_darray_resize(c, v);
|
|
|
|
v->v[v->n] = val;
|
2019-08-01 16:42:14 +00:00
|
|
|
v->n++;
|
|
|
|
return v;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
void grib_darray_delete(grib_context* c, grib_darray* v)
|
|
|
|
{
|
|
|
|
if (!v)
|
|
|
|
return;
|
|
|
|
if (!c)
|
|
|
|
grib_context_get_default();
|
|
|
|
if (v->v)
|
|
|
|
grib_context_free(c, v->v);
|
|
|
|
grib_context_free(c, v);
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
double* grib_darray_get_array(grib_context* c, grib_darray* v)
|
|
|
|
{
|
2019-08-01 16:42:14 +00:00
|
|
|
double* ret;
|
|
|
|
int i;
|
2020-01-22 13:10:59 +00:00
|
|
|
if (!v)
|
|
|
|
return NULL;
|
|
|
|
ret = (double*)grib_context_malloc_clear(c, sizeof(double) * v->n);
|
|
|
|
for (i = 0; i < v->n; i++)
|
|
|
|
ret[i] = v->v[i];
|
2019-08-01 16:42:14 +00:00
|
|
|
return ret;
|
2014-08-07 11:03:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
int grib_darray_is_constant(grib_darray* v, double eps)
|
|
|
|
{
|
2019-08-01 16:42:14 +00:00
|
|
|
int i;
|
|
|
|
double val;
|
2020-01-22 13:10:59 +00:00
|
|
|
if (v->n == 1)
|
|
|
|
return 1;
|
2019-08-01 16:42:14 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
val = v->v[0];
|
|
|
|
for (i = 1; i < v->n; i++) {
|
|
|
|
if (fabs(val - v->v[i]) > eps)
|
|
|
|
return 0;
|
2019-08-01 16:42:14 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2016-10-21 13:03:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
size_t grib_darray_used_size(grib_darray* v)
|
|
|
|
{
|
|
|
|
return v->n;
|
|
|
|
}
|