2014-08-07 11:03:53 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2014-08-07 11:03:53 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "grib_api_internal.h"
|
|
|
|
|
2023-08-23 16:00:27 +00:00
|
|
|
// For debugging purposes
|
2018-04-12 11:36:39 +00:00
|
|
|
void grib_vdarray_print(const char* title, const grib_vdarray* vdarray)
|
|
|
|
{
|
2022-11-12 22:08:44 +00:00
|
|
|
size_t i = 0;
|
2020-01-22 14:48:06 +00:00
|
|
|
char text[100] = {0,};
|
2018-04-12 11:36:39 +00:00
|
|
|
Assert(vdarray);
|
2022-12-21 15:07:48 +00:00
|
|
|
printf("%s: vdarray.n=%zu\n", title, vdarray->n);
|
2020-01-22 13:10:59 +00:00
|
|
|
for (i = 0; i < vdarray->n; i++) {
|
2022-12-21 15:07:48 +00:00
|
|
|
snprintf(text, sizeof(text), " vdarray->v[%zu]", i);
|
2018-04-12 11:36:39 +00:00
|
|
|
grib_darray_print(text, vdarray->v[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_vdarray* grib_vdarray_new(grib_context* c, size_t size, size_t incsize)
|
2015-12-22 17:55:45 +00:00
|
|
|
{
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_vdarray* v = NULL;
|
|
|
|
if (!c)
|
|
|
|
c = grib_context_get_default();
|
|
|
|
v = (grib_vdarray*)grib_context_malloc_clear(c, sizeof(grib_vdarray));
|
2015-12-22 17:55:45 +00:00
|
|
|
if (!v) {
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_context_log(c, GRIB_LOG_ERROR,
|
2023-08-23 16:00:27 +00:00
|
|
|
"%s: Unable to allocate %zu bytes", __func__, sizeof(grib_vdarray));
|
2015-12-22 17:55:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-22 13:10:59 +00:00
|
|
|
v->size = size;
|
|
|
|
v->n = 0;
|
|
|
|
v->incsize = incsize;
|
2020-05-26 14:26:45 +00:00
|
|
|
v->context = c;
|
2020-01-22 13:10:59 +00:00
|
|
|
v->v = (grib_darray**)grib_context_malloc_clear(c, sizeof(grib_darray*) * size);
|
2015-12-22 17:55:45 +00:00
|
|
|
if (!v->v) {
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_context_log(c, GRIB_LOG_ERROR,
|
2023-08-23 16:00:27 +00:00
|
|
|
"%s: Unable to allocate %zu bytes", __func__, sizeof(grib_darray*) * size);
|
2015-12-22 17:55:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return v;
|
2014-08-07 11:03:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 14:26:45 +00:00
|
|
|
static grib_vdarray* grib_vdarray_resize(grib_vdarray* v)
|
2015-12-22 17:55:45 +00:00
|
|
|
{
|
2020-10-13 12:30:41 +00:00
|
|
|
const size_t newsize = v->incsize + v->size;
|
2020-05-26 14:26:45 +00:00
|
|
|
grib_context* c = v->context;
|
2020-01-22 13:10:59 +00:00
|
|
|
if (!c)
|
|
|
|
c = grib_context_get_default();
|
2014-08-07 11:03:53 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
v->v = (grib_darray**)grib_context_realloc(c, v->v, newsize * sizeof(grib_darray*));
|
|
|
|
v->size = newsize;
|
2015-12-22 17:55:45 +00:00
|
|
|
if (!v->v) {
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_context_log(c, GRIB_LOG_ERROR,
|
2023-08-23 16:00:27 +00:00
|
|
|
"%s: Unable to allocate %lu bytes\n", __func__, sizeof(grib_darray*) * newsize);
|
2015-12-22 17:55:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return v;
|
2014-08-07 11:03:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_vdarray* grib_vdarray_push(grib_context* c, grib_vdarray* v, grib_darray* val)
|
2015-12-22 17:55:45 +00:00
|
|
|
{
|
2020-01-22 13:10:59 +00:00
|
|
|
size_t start_size = 100;
|
|
|
|
size_t start_incsize = 100;
|
|
|
|
if (!v)
|
|
|
|
v = grib_vdarray_new(c, start_size, start_incsize);
|
2014-08-07 11:03:53 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
if (v->n >= v->size)
|
2020-05-26 14:26:45 +00:00
|
|
|
v = grib_vdarray_resize(v);
|
2020-01-22 13:10:59 +00:00
|
|
|
v->v[v->n] = val;
|
2015-12-22 17:55:45 +00:00
|
|
|
v->n++;
|
|
|
|
return v;
|
2014-08-07 11:03:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
void grib_vdarray_delete(grib_context* c, grib_vdarray* v)
|
2015-12-22 17:55:45 +00:00
|
|
|
{
|
2020-01-22 13:10:59 +00:00
|
|
|
if (!v)
|
|
|
|
return;
|
|
|
|
if (!c)
|
2020-05-21 17:34:18 +00:00
|
|
|
c = grib_context_get_default();
|
2022-11-08 12:33:16 +00:00
|
|
|
if (v->v) {
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_context_free(c, v->v);
|
2022-11-08 12:33:16 +00:00
|
|
|
}
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_context_free(c, v);
|
2014-08-07 11:03:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
void grib_vdarray_delete_content(grib_context* c, grib_vdarray* v)
|
2015-12-22 17:55:45 +00:00
|
|
|
{
|
2022-11-12 22:08:44 +00:00
|
|
|
size_t i = 0;
|
2020-01-22 13:10:59 +00:00
|
|
|
if (!v || !v->v)
|
|
|
|
return;
|
|
|
|
if (!c)
|
2020-05-21 17:34:18 +00:00
|
|
|
c = grib_context_get_default();
|
2020-01-22 13:10:59 +00:00
|
|
|
for (i = 0; i < v->n; i++) {
|
|
|
|
grib_darray_delete(c, v->v[i]);
|
|
|
|
v->v[i] = 0;
|
2015-12-22 17:55:45 +00:00
|
|
|
}
|
2020-01-22 13:10:59 +00:00
|
|
|
v->n = 0;
|
2014-08-07 11:03:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-23 15:50:34 +00:00
|
|
|
// grib_darray** grib_vdarray_get_array(grib_context* c, grib_vdarray* v)
|
|
|
|
// {
|
|
|
|
// grib_darray** ret;
|
|
|
|
// size_t i = 0;
|
|
|
|
// if (!v)
|
|
|
|
// return NULL;
|
|
|
|
// ret = (grib_darray**)grib_context_malloc_clear(c, sizeof(grib_darray*) * v->n);
|
|
|
|
// for (i = 0; i < v->n; i++)
|
|
|
|
// ret[i] = v->v[i];
|
|
|
|
// return ret;
|
|
|
|
// }
|
2014-08-07 11:03:53 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
size_t grib_vdarray_used_size(grib_vdarray* v)
|
|
|
|
{
|
|
|
|
return v->n;
|
|
|
|
}
|