Nasal: add naVec_removelast. (Thanks to Philosopher)

This commit is contained in:
Thomas Geymayer 2013-04-12 12:32:03 +02:00
parent f6709e357f
commit 3da44d1215
2 changed files with 40 additions and 1 deletions

View File

@ -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);

View File

@ -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; i<v->size; 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;