From: Thomas Geymayer Date: Fri, 12 Apr 2013 10:32:03 +0000 (+0200) Subject: Nasal: add naVec_removelast. (Thanks to Philosopher) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=3da44d1215fe5c35c7650aa5815d1b2c5b67d569;p=simgear.git Nasal: add naVec_removelast. (Thanks to Philosopher) --- diff --git a/simgear/nasal/nasal.h b/simgear/nasal/nasal.h index d3cb9167..db7354ea 100644 --- a/simgear/nasal/nasal.h +++ b/simgear/nasal/nasal.h @@ -185,9 +185,27 @@ int naVec_size(naRef v); naRef naVec_get(naRef v, int i); void naVec_set(naRef vec, int i, naRef o); int naVec_append(naRef vec, naRef o); -naRef naVec_removelast(naRef vec); void naVec_setsize(naContext c, naRef vec, int sz); +/** + * Remove and retrieve the first element of the vector. + * + * This operation reduces the size of the vector by one and moves all elements + * by one towards the begin of the vector. + * + * @return The element removed from the begin + */ +naRef naVec_removefirst(naRef vec); + +/** + * Remove and retrieve the last element of the vector. + * + * This operation reduces the size of the vector by one. + * + * @return The element removed from the end + */ +naRef naVec_removelast(naRef vec); + // Hash utilities: int naHash_size(naRef h); int naHash_get(naRef hash, naRef key, naRef* out); diff --git a/simgear/nasal/vector.c b/simgear/nasal/vector.c index 1a7546ae..fc160f0e 100644 --- a/simgear/nasal/vector.c +++ b/simgear/nasal/vector.c @@ -69,6 +69,7 @@ int naVec_append(naRef vec, naRef o) return 0; } +//------------------------------------------------------------------------------ void naVec_setsize(naContext c, naRef vec, int sz) { if (sz < 0) @@ -86,6 +87,26 @@ void naVec_setsize(naContext c, naRef vec, int sz) } } +//------------------------------------------------------------------------------ +naRef naVec_removefirst(naRef vec) +{ + naRef o; + int i; + if(IS_VEC(vec)) { + struct VecRec* v = PTR(vec).vec->rec; + if(!v || v->size == 0) return naNil(); + o = v->array[0]; + for (i=1; isize; i++) + v->array[i-1] = v->array[i]; + v->size--; + if(v->size < (v->alloced >> 1)) + resize(PTR(vec).vec); + return o; + } + return naNil(); +} + +//------------------------------------------------------------------------------ naRef naVec_removelast(naRef vec) { naRef o;