From 3da44d1215fe5c35c7650aa5815d1b2c5b67d569 Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Fri, 12 Apr 2013 12:32:03 +0200 Subject: [PATCH] Nasal: add naVec_removelast. (Thanks to Philosopher) --- simgear/nasal/nasal.h | 20 +++++++++++++++++++- simgear/nasal/vector.c | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) 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; -- 2.39.5