eccodes/src/grib_bufr_descriptors_array.cc

219 lines
6.0 KiB
C++
Raw Permalink 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,
2020-10-13 12:30:41 +00:00
"bufr_descriptors_array_new unable to allocate %ld 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,
2020-10-13 12:30:41 +00:00
"grib_bufr_descriptors_array_new unable to allocate %ld bytes\n", sizeof(bufr_descriptor) * size);
return NULL;
}
return v;
2014-09-01 12:20:22 +00:00
}
2023-08-21 16:41:25 +00:00
// bufr_descriptor* grib_bufr_descriptors_array_pop(bufr_descriptors_array* a)
// {
// a->n -= 1;
// return a->v[a->n];
// }
2014-09-01 12:20:22 +00:00
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) {
2023-06-26 20:21:07 +00:00
DEBUG_ASSERT(0);
}
2014-09-01 12:20:22 +00:00
a->n--;
a->v++;
a->number_of_pop_front++;
return v;
}
static 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,
2020-10-13 12:30:41 +00:00
"grib_bufr_descriptors_array_resize unable to allocate %ld 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
}
static bufr_descriptors_array* grib_bufr_descriptors_array_resize(bufr_descriptors_array* v)
2018-01-04 15:47:46 +00:00
{
const size_t newsize = v->incsize + v->size;
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);
/* ar = 0; */
2014-09-01 12:20:22 +00:00
return v;
2014-09-01 12:20:22 +00:00
}
2023-08-21 16:47:04 +00:00
// bufr_descriptors_array* grib_bufr_descriptors_array_push_front(bufr_descriptors_array* v, bufr_descriptor* val)
// {
// size_t i = 0;
// if (!v) {
// 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);
// }
// if (v->number_of_pop_front) {
// v->v--;
// v->number_of_pop_front--;
// }
// 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
}
2023-08-21 16:47:04 +00:00
// void grib_bufr_descriptors_array_set(bufr_descriptors_array* a, size_t i, bufr_descriptor* v)
// {
// 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 = NULL;
size_t i = 0;
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
}
2023-08-21 16:49:40 +00:00
// bufr_descriptor** grib_bufr_descriptors_array_get_array(bufr_descriptors_array* v)
// {
// bufr_descriptor** vv;
// size_t i;
// grib_context* c = grib_context_get_default();
// 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]);
// return vv;
// }
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
size_t grib_bufr_descriptors_array_used_size(bufr_descriptors_array* v)
{
return v->n;
}