eccodes/src/grib_iarray.c

237 lines
5.4 KiB
C
Raw Normal View History

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 */
void grib_iarray_print(const char* title, const grib_iarray* iarray)
{
size_t i;
2017-10-24 16:27:14 +00:00
Assert(iarray);
2017-08-25 16:32:07 +00:00
printf("%s: iarray.n=%lu \t", title, (unsigned long)iarray->n);
2020-01-22 13:10:59 +00:00
for (i = 0; i < iarray->n; i++) {
2017-08-25 16:32:07 +00:00
printf("iarray[%lu]=%ld\t", (unsigned long)i, iarray->v[i]);
}
printf("\n");
}
2013-03-25 12:04:10 +00:00
2020-01-22 13:10:59 +00:00
grib_iarray* grib_iarray_new_from_array(grib_context* c, long* a, size_t size)
2015-12-22 17:55:45 +00:00
{
size_t i;
grib_iarray* v;
2020-01-22 13:10:59 +00:00
if (!c)
c = grib_context_get_default();
2020-01-22 13:10:59 +00:00
v = grib_iarray_new(c, size, 100);
for (i = 0; i < size; i++)
v->v[i] = a[i];
v->n = size;
v->number_of_pop_front = 0;
v->context = c;
return v;
}
2020-01-22 13:10:59 +00:00
grib_iarray* grib_iarray_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_iarray* v = NULL;
2015-12-22 17:55:45 +00:00
2020-01-22 13:10:59 +00:00
if (!c)
c = grib_context_get_default();
2015-12-22 17:55:45 +00:00
2020-01-22 13:10:59 +00:00
v = (grib_iarray*)grib_context_malloc(c, sizeof(grib_iarray));
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,
2020-10-13 12:30:41 +00:00
"grib_iarray_new unable to allocate %ld bytes\n", sizeof(grib_iarray));
2015-12-22 17:55:45 +00:00
return NULL;
}
2020-01-22 13:10:59 +00:00
v->context = c;
v->size = size;
v->n = 0;
v->incsize = incsize;
v->v = (long*)grib_context_malloc(c, sizeof(long) * size);
v->number_of_pop_front = 0;
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,
2020-10-13 12:30:41 +00:00
"grib_iarray_new unable to allocate %ld bytes\n", sizeof(long) * size);
2015-12-22 17:55:45 +00:00
return NULL;
}
return v;
2013-03-25 12:04:10 +00:00
}
2015-12-22 17:55:45 +00:00
long grib_iarray_pop(grib_iarray* a)
{
2020-01-22 13:10:59 +00:00
a->n -= 1;
return a->v[a->n];
}
2015-12-22 17:55:45 +00:00
long grib_iarray_pop_front(grib_iarray* a)
{
2020-01-22 13:10:59 +00:00
long v = a->v[0];
/* size_t i=0; */
2020-01-22 13:10:59 +00:00
if (a->n == 0)
Assert(0);
a->n--;
a->v++;
a->number_of_pop_front++;
/* for (i=0;i<a->n;i++) a->v[i]=a->v[i+1]; */
return v;
}
static grib_iarray* grib_iarray_resize_to(grib_iarray* v, size_t newsize)
2015-12-22 17:55:45 +00:00
{
long* newv;
size_t i;
2020-01-22 13:10:59 +00:00
grib_context* c = v->context;
2020-01-22 13:10:59 +00:00
if (newsize < v->size)
return v;
2013-03-25 12:04:10 +00:00
2020-01-22 13:10:59 +00:00
if (!c)
c = grib_context_get_default();
2013-03-25 12:04:10 +00:00
2020-01-22 13:10:59 +00:00
newv = (long*)grib_context_malloc_clear(c, newsize * sizeof(long));
2015-12-22 17:55:45 +00:00
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_iarray_resize unable to allocate %ld bytes\n", sizeof(long) * newsize);
2015-12-22 17:55:45 +00:00
return NULL;
}
2020-01-22 13:10:59 +00:00
for (i = 0; i < v->n; i++)
newv[i] = v->v[i];
2020-01-22 13:10:59 +00:00
v->v -= v->number_of_pop_front;
grib_context_free(c, v->v);
2020-01-22 13:10:59 +00:00
v->v = newv;
v->size = newsize;
v->number_of_pop_front = 0;
2015-12-22 17:55:45 +00:00
return v;
2013-03-25 12:04:10 +00:00
}
static grib_iarray* grib_iarray_resize(grib_iarray* v)
2015-12-22 17:55:45 +00:00
{
const int newsize = v->incsize + v->size;
2020-01-22 13:10:59 +00:00
return grib_iarray_resize_to(v, newsize);
}
2020-01-22 13:10:59 +00:00
grib_iarray* grib_iarray_push(grib_iarray* v, long 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;
2013-03-25 12:04:10 +00:00
2020-01-22 13:10:59 +00:00
if (!v)
v = grib_iarray_new(0, start_size, start_incsize);
2020-01-22 13:10:59 +00:00
if (v->n >= v->size - v->number_of_pop_front)
v = grib_iarray_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;
2013-03-25 12:04:10 +00:00
}
2020-01-22 13:10:59 +00:00
grib_iarray* grib_iarray_push_front(grib_iarray* v, long 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;
2015-12-22 17:55:45 +00:00
int i;
2020-01-22 13:10:59 +00:00
if (!v)
v = grib_iarray_new(0, start_size, start_incsize);
2015-12-22 17:55:45 +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_iarray_resize(v);
for (i = v->n; i > 0; i--)
v[i] = v[i - 1];
}
v->v[0] = val;
2015-12-22 17:55:45 +00:00
v->n++;
2015-12-22 17:55:45 +00:00
return v;
}
2020-01-22 13:10:59 +00:00
grib_iarray* grib_iarray_push_array(grib_iarray* v, long* val, size_t size)
2015-12-22 17:55:45 +00:00
{
2020-01-22 13:10:59 +00:00
size_t start_size = size;
size_t start_incsize = 100;
long* vp = 0;
long* valp = val;
if (!v)
v = grib_iarray_new(0, start_size, start_incsize);
v = grib_iarray_resize_to(v, size + v->n);
vp = v->v + v->n + v->number_of_pop_front;
v->n += size;
2015-12-22 17:55:45 +00:00
while (size) {
2020-01-22 13:10:59 +00:00
*(vp++) = *(valp++);
2015-12-22 17:55:45 +00:00
size--;
}
return v;
}
2015-12-22 17:55:45 +00:00
void grib_iarray_delete(grib_iarray* v)
{
grib_context* c;
2020-01-22 13:10:59 +00:00
if (!v)
return;
c = v->context;
2015-12-22 17:55:45 +00:00
grib_iarray_delete_array(v);
2020-01-22 13:10:59 +00:00
grib_context_free(c, v);
2013-03-25 12:04:10 +00:00
}
2015-12-22 17:55:45 +00:00
void grib_iarray_delete_array(grib_iarray* v)
{
grib_context* c;
2020-01-22 13:10:59 +00:00
if (!v)
return;
c = v->context;
2015-12-22 17:55:45 +00:00
if (v->v) {
long* vv = v->v - v->number_of_pop_front;
2020-01-22 13:10:59 +00:00
grib_context_free(c, vv);
2015-12-22 17:55:45 +00:00
}
}
2015-12-22 17:55:45 +00:00
long* grib_iarray_get_array(grib_iarray* v)
{
long* vv;
size_t i;
2020-05-21 17:34:18 +00:00
grib_context* c = v->context;
DebugAssert(c);
2020-01-22 13:10:59 +00:00
vv = (long*)grib_context_malloc_clear(c, sizeof(long) * v->n);
for (i = 0; i < v->n; i++)
vv[i] = v->v[i];
return vv;
}
2020-01-22 13:10:59 +00:00
size_t grib_iarray_used_size(grib_iarray* v)
{
return v == NULL ? 0 : v->n;
}