eccodes/src/grib_bufr_descriptors_array.c

224 lines
5.9 KiB
C
Raw Normal View History

2014-09-01 12:20:22 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2014-09-01 12:20:22 +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"
#define DYN_ARRAY_SIZE_INIT 200 /* Initial size for grib_bufr_descriptors_array_new */
#define DYN_ARRAY_SIZE_INCR 400 /* Increment size for the above */
2020-01-22 13:10:59 +00:00
bufr_descriptors_array* grib_bufr_descriptors_array_new(grib_context* c, size_t size, size_t incsize)
2018-01-04 15:47:46 +00:00
{
2020-01-22 13:10:59 +00:00
bufr_descriptors_array* v = NULL;
2020-01-22 13:10:59 +00:00
if (!c)
c = grib_context_get_default();
2020-01-22 13:10:59 +00:00
v = (bufr_descriptors_array*)grib_context_malloc(c, sizeof(bufr_descriptors_array));
if (!v) {
2020-01-22 13:10:59 +00:00
grib_context_log(c, GRIB_LOG_ERROR,
"bufr_descriptors_array_new unable to allocate %d bytes\n", sizeof(bufr_descriptors_array));
return NULL;
}
2020-01-22 13:10:59 +00:00
v->context = c;
v->size = size;
v->n = 0;
v->incsize = incsize;
v->v = (bufr_descriptor**)grib_context_malloc(c, sizeof(bufr_descriptor*) * size);
v->number_of_pop_front = 0;
if (!v->v) {
2020-01-22 13:10:59 +00:00
grib_context_log(c, GRIB_LOG_ERROR,
"grib_bufr_descriptors_array_new unable to allocate %d bytes\n", sizeof(bufr_descriptor) * size);
return NULL;
}
return v;
2014-09-01 12:20:22 +00:00
}
2018-01-04 15:47:46 +00:00
bufr_descriptor* grib_bufr_descriptors_array_pop(bufr_descriptors_array* a)
{
2020-01-22 13:10:59 +00:00
a->n -= 1;
2014-09-01 12:20:22 +00:00
return a->v[a->n];
}
2018-01-04 15:47:46 +00:00
bufr_descriptor* grib_bufr_descriptors_array_pop_front(bufr_descriptors_array* a)
{
2020-01-22 13:10:59 +00:00
bufr_descriptor* v = a->v[0];
if (a->n == 0) {
DebugAssert(0);
}
2014-09-01 12:20:22 +00:00
a->n--;
a->v++;
a->number_of_pop_front++;
return v;
}
2020-01-22 13:10:59 +00:00
bufr_descriptors_array* grib_bufr_descriptors_array_resize_to(bufr_descriptors_array* v, size_t newsize)
2018-01-04 15:47:46 +00:00
{
bufr_descriptor** newv;
size_t i;
2020-01-22 13:10:59 +00:00
grib_context* c = v->context;
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
if (newsize < v->size)
return v;
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
if (!c)
c = grib_context_get_default();
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
newv = (bufr_descriptor**)grib_context_malloc_clear(c, newsize * sizeof(bufr_descriptor*));
if (!newv) {
2020-01-22 13:10:59 +00:00
grib_context_log(c, GRIB_LOG_ERROR,
"grib_bufr_descriptors_array_resize unable to allocate %d bytes\n", sizeof(bufr_descriptor*) * newsize);
return NULL;
}
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
for (i = 0; i < v->n; i++)
newv[i] = v->v[i];
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
v->v -= v->number_of_pop_front;
grib_context_free(c, v->v);
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
v->v = newv;
v->size = newsize;
v->number_of_pop_front = 0;
2014-09-01 12:20:22 +00:00
return v;
2014-09-01 12:20:22 +00:00
}
2018-01-04 15:47:46 +00:00
bufr_descriptors_array* grib_bufr_descriptors_array_resize(bufr_descriptors_array* v)
{
2020-01-22 13:10:59 +00:00
int newsize = v->incsize + v->size;
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
return grib_bufr_descriptors_array_resize_to(v, newsize);
2014-09-01 12:20:22 +00:00
}
2020-01-22 13:10:59 +00:00
bufr_descriptors_array* grib_bufr_descriptors_array_push(bufr_descriptors_array* v, bufr_descriptor* val)
2018-01-04 15:47:46 +00:00
{
if (!v) {
2020-01-22 13:10:59 +00:00
size_t start_size = DYN_ARRAY_SIZE_INIT;
size_t start_incsize = DYN_ARRAY_SIZE_INCR;
v = grib_bufr_descriptors_array_new(0, start_size, start_incsize);
2018-01-04 15:47:46 +00:00
}
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
if (v->n >= v->size - v->number_of_pop_front)
v = grib_bufr_descriptors_array_resize(v);
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
v->v[v->n] = val;
v->n++;
return v;
2014-09-01 12:20:22 +00:00
}
2020-01-22 13:10:59 +00:00
bufr_descriptors_array* grib_bufr_descriptors_array_append(bufr_descriptors_array* v, bufr_descriptors_array* ar)
2018-01-04 15:47:46 +00:00
{
size_t i;
2020-01-22 13:10:59 +00:00
bufr_descriptor* vv = 0;
2014-09-01 12:20:22 +00:00
2018-01-04 15:47:46 +00:00
if (!v) {
2020-01-22 13:10:59 +00:00
size_t start_size = DYN_ARRAY_SIZE_INIT;
size_t start_incsize = DYN_ARRAY_SIZE_INCR;
v = grib_bufr_descriptors_array_new(0, start_size, start_incsize);
2018-01-04 15:47:46 +00:00
}
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
for (i = 0; i < ar->n; i++) {
vv = grib_bufr_descriptor_clone(ar->v[i]);
grib_bufr_descriptors_array_push(v, vv);
}
2014-09-01 12:20:22 +00:00
grib_bufr_descriptors_array_delete(ar);
2020-01-22 13:10:59 +00:00
ar = 0;
2014-09-01 12:20:22 +00:00
return v;
2014-09-01 12:20:22 +00:00
}
2020-01-22 13:10:59 +00:00
bufr_descriptors_array* grib_bufr_descriptors_array_push_front(bufr_descriptors_array* v, bufr_descriptor* val)
2018-01-04 15:47:46 +00:00
{
int i;
2018-01-04 15:47:46 +00:00
if (!v) {
2020-01-22 13:10:59 +00:00
size_t start_size = DYN_ARRAY_SIZE_INIT;
size_t start_incsize = DYN_ARRAY_SIZE_INCR;
v = grib_bufr_descriptors_array_new(0, start_size, start_incsize);
2018-01-04 15:47:46 +00:00
}
if (v->number_of_pop_front) {
v->v--;
v->number_of_pop_front--;
}
2020-01-22 13:10:59 +00:00
else {
if (v->n >= v->size)
v = grib_bufr_descriptors_array_resize(v);
for (i = v->n; i > 0; i--)
v[i] = v[i - 1];
}
v->v[0] = val;
v->n++;
return v;
2014-09-01 12:20:22 +00:00
}
2020-01-22 13:10:59 +00:00
bufr_descriptor* grib_bufr_descriptors_array_get(bufr_descriptors_array* a, size_t i)
2018-01-04 15:47:46 +00:00
{
return a->v[i];
2014-09-01 12:20:22 +00:00
}
2020-01-22 13:10:59 +00:00
void grib_bufr_descriptors_array_set(bufr_descriptors_array* a, size_t i, bufr_descriptor* v)
2018-01-04 15:47:46 +00:00
{
2020-01-22 13:10:59 +00:00
a->v[i] = v;
2014-09-01 12:20:22 +00:00
}
2018-01-04 15:47:46 +00:00
void grib_bufr_descriptors_array_delete(bufr_descriptors_array* v)
{
grib_context* c;
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
if (!v)
return;
c = v->context;
2014-09-01 12:20:22 +00:00
grib_bufr_descriptors_array_delete_array(v);
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
grib_context_free(c, v);
2014-09-01 12:20:22 +00:00
}
2018-01-04 15:47:46 +00:00
void grib_bufr_descriptors_array_delete_array(bufr_descriptors_array* v)
{
grib_context* c;
int i;
2020-01-22 13:10:59 +00:00
bufr_descriptor** vv = NULL;
2020-01-22 13:10:59 +00:00
if (!v)
return;
c = v->context;
if (v->v) {
2020-01-22 13:10:59 +00:00
vv = v->v;
for (i = 0; i < v->n; i++) {
grib_bufr_descriptor_delete(vv[i]);
}
vv = v->v - v->number_of_pop_front;
2020-01-22 13:10:59 +00:00
grib_context_free(c, vv);
2015-03-18 16:23:00 +00:00
}
2014-09-01 12:20:22 +00:00
}
2018-01-04 15:47:46 +00:00
bufr_descriptor** grib_bufr_descriptors_array_get_array(bufr_descriptors_array* v)
{
2014-09-01 12:20:22 +00:00
bufr_descriptor** vv;
size_t i;
2020-01-22 13:10:59 +00:00
grib_context* c = grib_context_get_default();
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
vv = (bufr_descriptor**)grib_context_malloc_clear(c, sizeof(bufr_descriptor*) * v->n);
for (i = 0; i < v->n; i++)
vv[i] = grib_bufr_descriptor_clone(v->v[i]);
2014-09-01 12:20:22 +00:00
return vv;
}
2020-01-22 13:10:59 +00:00
size_t grib_bufr_descriptors_array_used_size(bufr_descriptors_array* v)
{
return v->n;
}