added hawk_rtx_scalearrval()

This commit is contained in:
2025-07-06 16:29:52 +09:00
parent acd32601a8
commit 45a22eb5a4
6 changed files with 86 additions and 28 deletions

View File

@ -865,7 +865,7 @@ int Hawk::Value::scaleArrayed (Run* r, hawk_ooi_t size)
{ {
// the previous value is not a arr. // the previous value is not a arr.
// a new arr value needs to be created first. // a new arr value needs to be created first.
hawk_val_t* arr = hawk_rtx_makearrval(r->rtx, size); // TODO: can we change this to accept the initial value in the constructor hawk_val_t* arr = hawk_rtx_makearrval(r->rtx, size);
if (arr == HAWK_NULL) if (arr == HAWK_NULL)
{ {
r->hawk->retrieveError(r); r->hawk->retrieveError(r);
@ -899,14 +899,12 @@ int Hawk::Value::scaleArrayed (Run* r, hawk_ooi_t size)
return -1; return -1;
} }
/* TODO: RESIZE THE EXISTING ARRAY... // update the capacity of an array
// update the arr with a given value if (hawk_rtx_scalearrval(r->rtx, this->val, size) <= -1)
if (hawk_rtx_setarrvalfld(r->rtx, val, idx, v) == HAWK_NULL)
{ {
r->hawk->retrieveError(r); r->hawk->retrieveError(r);
return -1; return -1;
} }
*/
} }
return 0; return 0;
@ -918,12 +916,16 @@ hawk_ooi_t Hawk::Value::getArrayedLength () const
return (hawk_ooi_t)HAWK_ARR_TALLY(((hawk_val_arr_t*)this->val)->arr); return (hawk_ooi_t)HAWK_ARR_TALLY(((hawk_val_arr_t*)this->val)->arr);
} }
hawk_ooi_t Hawk::Value::getArrayedSize () const hawk_ooi_t Hawk::Value::getArrayedSize () const
{ {
if (HAWK_RTX_GETVALTYPE(r->rtx, this->val) != HAWK_VAL_ARR) return -1; if (HAWK_RTX_GETVALTYPE(r->rtx, this->val) != HAWK_VAL_ARR) return -1;
return (hawk_ooi_t)HAWK_ARR_SIZE(((hawk_val_arr_t*)this->val)->arr); return (hawk_ooi_t)HAWK_ARR_SIZE(((hawk_val_arr_t*)this->val)->arr);
}
hawk_ooi_t Hawk::Value::getArrayedCapa () const
{
if (HAWK_RTX_GETVALTYPE(r->rtx, this->val) != HAWK_VAL_ARR) return -1;
return (hawk_ooi_t)HAWK_ARR_CAPA(((hawk_val_arr_t*)this->val)->arr);
} }
int Hawk::Value::setArrayedVal (hawk_ooi_t idx, hawk_val_t* v) int Hawk::Value::setArrayedVal (hawk_ooi_t idx, hawk_val_t* v)
@ -1027,6 +1029,24 @@ int Hawk::Value::getArrayed (hawk_ooi_t idx, Value* v) const
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
hawk_ooi_t Hawk::Value::getIndexedLength () const
{
if (HAWK_RTX_GETVALTYPE(r->rtx, this->val) != HAWK_VAL_MAP) return -1;
return (hawk_ooi_t)HAWK_MAP_SIZE(((hawk_val_map_t*)this->val)->map);
}
hawk_ooi_t Hawk::Value::getIndexedSize () const
{
if (HAWK_RTX_GETVALTYPE(r->rtx, this->val) != HAWK_VAL_MAP) return -1;
return (hawk_ooi_t)HAWK_MAP_SIZE(((hawk_val_map_t*)this->val)->map);
}
hawk_ooi_t Hawk::Value::getIndexedCapa () const
{
if (HAWK_RTX_GETVALTYPE(r->rtx, this->val) != HAWK_VAL_MAP) return -1;
return (hawk_ooi_t)hawk_map_getcapa(((hawk_val_map_t*)this->val)->map);
}
int Hawk::Value::setIndexedVal (const Index& idx, hawk_val_t* v) int Hawk::Value::setIndexedVal (const Index& idx, hawk_val_t* v)
{ {
if (HAWK_UNLIKELY(!this->run)) return -1; if (HAWK_UNLIKELY(!this->run)) return -1;

View File

@ -1111,6 +1111,7 @@ public:
hawk_ooi_t getArrayedSize () const; // same value as hawk::size() for an array hawk_ooi_t getArrayedSize () const; // same value as hawk::size() for an array
hawk_ooi_t getArrayedLength () const; // same value as hawk::length() for an array hawk_ooi_t getArrayedLength () const; // same value as hawk::length() for an array
hawk_ooi_t getArrayedCapa () const;
bool isArrayed () const; bool isArrayed () const;
int getArrayed ( int getArrayed (
@ -1140,6 +1141,10 @@ public:
int setIndexedMbs (const Index& idx, const hawk_bch_t* str); int setIndexedMbs (const Index& idx, const hawk_bch_t* str);
int setIndexedMbs (Run* r, const Index& idx, const hawk_bch_t* str); int setIndexedMbs (Run* r, const Index& idx, const hawk_bch_t* str);
hawk_ooi_t getIndexedSize () const; // same value as hawk::size() for a map
hawk_ooi_t getIndexedLength () const; // same value as hawk::length() for a map
hawk_ooi_t getIndexedCapa () const;
/// ///
/// The isIndexed() function determines if a value is a map. /// The isIndexed() function determines if a value is a map.
/// \return true if indexed, false if not. /// \return true if indexed, false if not.

View File

@ -3129,7 +3129,7 @@ HAWK_EXPORT hawk_val_map_itr_t* hawk_rtx_getfirstmapvalitr (
); );
/** /**
* The hawk_rtx_getnextmapvalitr() returns the iterator to the * The hawk_rtx_getnextmapvalitr() function returns the iterator to the
* next pair to \a itr in the map. It returns #HAWK_NULL and sets the pair * next pair to \a itr in the map. It returns #HAWK_NULL and sets the pair
* field of \a itr to #HAWK_NULL if \a itr points to the last pair. * field of \a itr to #HAWK_NULL if \a itr points to the last pair.
* Otherwise, it returns \a itr pointing to the next pair. * Otherwise, it returns \a itr pointing to the next pair.
@ -3140,6 +3140,14 @@ HAWK_EXPORT hawk_val_map_itr_t* hawk_rtx_getnextmapvalitr (
hawk_val_map_itr_t* itr hawk_val_map_itr_t* itr
); );
/**
* The hawk_rtx_scalearrval() function changes the capacity of an array value
*/
HAWK_EXPORT int hawk_rtx_scalearrval (
hawk_rtx_t* rtx,
hawk_val_t* arr,
hawk_ooi_t capa
);
HAWK_EXPORT hawk_val_t* hawk_rtx_setarrvalfld ( HAWK_EXPORT hawk_val_t* hawk_rtx_setarrvalfld (
hawk_rtx_t* rtx, hawk_rtx_t* rtx,

View File

@ -1412,6 +1412,13 @@ hawk_val_map_itr_t* hawk_rtx_getnextmapvalitr (hawk_rtx_t* rtx, hawk_val_t* map,
return itr->pair? itr: HAWK_NULL; return itr->pair? itr: HAWK_NULL;
} }
int hawk_rtx_scalearrval (hawk_rtx_t* rtx, hawk_val_t* arr, hawk_ooi_t capa)
{
HAWK_ASSERT (HAWK_RTX_GETVALTYPE(rtx, arr) == HAWK_VAL_ARR);
if (!hawk_arr_setcapa(((hawk_val_arr_t*)arr)->arr, capa)) return -1;
return 0;
}
hawk_val_t* hawk_rtx_setarrvalfld (hawk_rtx_t* rtx, hawk_val_t* arr, hawk_ooi_t index, hawk_val_t* v) hawk_val_t* hawk_rtx_setarrvalfld (hawk_rtx_t* rtx, hawk_val_t* arr, hawk_ooi_t index, hawk_val_t* v)
{ {
HAWK_ASSERT (HAWK_RTX_GETVALTYPE(rtx, arr) == HAWK_VAL_ARR); HAWK_ASSERT (HAWK_RTX_GETVALTYPE(rtx, arr) == HAWK_VAL_ARR);

View File

@ -19,6 +19,8 @@ static void test1()
HAWK::Hawk::Value v(rtx); HAWK::Hawk::Value v(rtx);
HAWK::Hawk::Value vi(rtx); HAWK::Hawk::Value vi(rtx);
HAWK::Hawk::Value vx(rtx);
hawk_int_t vvx;
vi.setInt(1010); vi.setInt(1010);
OK_X(vi.getType() == HAWK_VAL_INT); OK_X(vi.getType() == HAWK_VAL_INT);
@ -30,6 +32,22 @@ static void test1()
OK_X(v.getType() == HAWK_VAL_ARR); OK_X(v.getType() == HAWK_VAL_ARR);
OK_X(v.getArrayedSize() == 5); OK_X(v.getArrayedSize() == 5);
OK_X(v.getArrayedLength() == 1); OK_X(v.getArrayedLength() == 1);
// no way to know the capacity as it's internally set
OK_X(v.getArrayed(4, &vx) == 0);
OK_X(vx.getType() == HAWK_VAL_INT && vx.getInt(&vvx) == 0);
OK_X(vvx == 1010);
OK_X(v.scaleArrayed(10) == 0);
OK_X(v.getType() == HAWK_VAL_ARR);
OK_X(v.getArrayedSize() == 5);
OK_X(v.getArrayedLength() == 1);
OK_X(v.getArrayedCapa() == 10); // capacity must be known after it's set explicitly set
HAWK::Hawk::Value x(rtx);
OK_X(x.scaleArrayed(3) == 0);
OK_X(x.getArrayedSize() == 0);
OK_X(x.getArrayedLength() == 0);
OK_X(x.getArrayedCapa() == 3);
// i must not call hawk.close() to ensure that v is destroyed first. // i must not call hawk.close() to ensure that v is destroyed first.
//hawk.close(); //hawk.close();