Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fetching from array by index #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,14 @@ unqlite_value * unqlite_array_fetch(unqlite_value *pArray, const char *zKey, int
{
return jx9_array_fetch(pArray,zKey,nByte);
}
/*
* [CAPIREF: unqlite_array_fetch_by_index()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
unqlite_value * unqlite_array_fetch_by_index(unqlite_value *pArray, unqlite_int64 iIndex)
{
return jx9_array_fetch_by_index(pArray,iIndex);
}
/*
* [CAPIREF: unqlite_array_walk()]
* Please refer to the official documentation for function purpose and expected parameters.
Expand Down
1 change: 1 addition & 0 deletions src/jx9.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ JX9_PRIVATE int jx9_value_resource(jx9_value *pVal, void *pUserData);
JX9_PRIVATE int jx9_value_release(jx9_value *pVal);
/* JSON Array/Object Management Interfaces */
JX9_PRIVATE jx9_value * jx9_array_fetch(jx9_value *pArray, const char *zKey, int nByte);
JX9_PRIVATE jx9_value * jx9_array_fetch_by_index(jx9_value *pArray, jx9_int64 iIndex);
JX9_PRIVATE int jx9_array_walk(jx9_value *pArray, int (*xWalk)(jx9_value *, jx9_value *, void *), void *pUserData);
JX9_PRIVATE int jx9_array_add_elem(jx9_value *pArray, jx9_value *pKey, jx9_value *pValue);
JX9_PRIVATE int jx9_array_add_strkey_elem(jx9_value *pArray, const char *zKey, jx9_value *pValue);
Expand Down
1 change: 1 addition & 0 deletions src/jx9Int.h
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,7 @@ JX9_PRIVATE sxi32 jx9HashmapLoadBuiltin(jx9_vm *pVm);
JX9_PRIVATE sxi32 jx9HashmapRelease(jx9_hashmap *pMap, int FreeDS);
JX9_PRIVATE void jx9HashmapUnref(jx9_hashmap *pMap);
JX9_PRIVATE sxi32 jx9HashmapLookup(jx9_hashmap *pMap, jx9_value *pKey, jx9_hashmap_node **ppNode);
JX9_PRIVATE sxi32 jx9HashmapLookupIntKey(jx9_hashmap *pMap, sxi64 iKey, jx9_hashmap_node **ppNode);
JX9_PRIVATE sxi32 jx9HashmapInsert(jx9_hashmap *pMap, jx9_value *pKey, jx9_value *pVal);
JX9_PRIVATE sxi32 jx9HashmapUnion(jx9_hashmap *pLeft, jx9_hashmap *pRight);
JX9_PRIVATE sxi32 jx9HashmapDup(jx9_hashmap *pSrc, jx9_hashmap *pDest);
Expand Down
23 changes: 23 additions & 0 deletions src/jx9_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,29 @@ JX9_PRIVATE jx9_value * jx9_array_fetch(jx9_value *pArray, const char *zKey, int
pValue = (jx9_value *)SySetAt(&pArray->pVm->aMemObj, pNode->nValIdx);
return pValue;
}
/*
* [CAPIREF: jx9_array_fetch_by_index()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE jx9_value * jx9_array_fetch_by_index(jx9_value *pArray, jx9_int64 iIndex)
{
jx9_hashmap_node *pNode;
jx9_value *pValue;
int rc;
/* Make sure we are dealing with a valid hashmap */
if( (pArray->iFlags & MEMOBJ_HASHMAP) == 0 ){
return 0;
}
/* Perform the lookup */
rc = jx9HashmapLookupIntKey((jx9_hashmap *)pArray->x.pOther, iIndex, &pNode);
if( rc != JX9_OK ){
/* No such entry */
return 0;
}
/* Extract the target value */
pValue = (jx9_value *)SySetAt(&pArray->pVm->aMemObj, pNode->nValIdx);
return pValue;
}
/*
* [CAPIREF: jx9_array_walk()]
* Please refer to the official documentation for function purpose and expected parameters.
Expand Down
20 changes: 20 additions & 0 deletions src/jx9_hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,26 @@ JX9_PRIVATE sxi32 jx9HashmapLookup(
rc = HashmapLookup(&(*pMap), &(*pKey), ppNode);
return rc;
}
/*
* Check if a given key exists in the given hashmap.
* Write a pointer to the target node on success.
* Otherwise SXERR_NOTFOUND is returned on failure.
*/
JX9_PRIVATE sxi32 jx9HashmapLookupIntKey(
jx9_hashmap *pMap, /* Target hashmap */
sxi64 iKey, /* Lookup key */
jx9_hashmap_node **ppNode /* OUT: Target node on success */
)
{
sxi32 rc;
if( pMap->nEntry < 1 ){
/* TICKET 1433-25: Don't bother hashing, the hashmap is empty anyway.
*/
return SXERR_NOTFOUND;
}
rc = HashmapLookupIntKey(&(*pMap), iKey, ppNode);
return rc;
}
/*
* Insert a given key and it's associated value (if any) in the given
* hashmap.
Expand Down
1 change: 1 addition & 0 deletions src/unqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ UNQLITE_APIEXPORT int unqlite_value_is_empty(unqlite_value *pVal);

/* JSON Array/Object Management Interfaces */
UNQLITE_APIEXPORT unqlite_value * unqlite_array_fetch(unqlite_value *pArray, const char *zKey, int nByte);
UNQLITE_APIEXPORT unqlite_value * unqlite_array_fetch_by_index(unqlite_value *pArray, unqlite_int64 iIndex);
UNQLITE_APIEXPORT int unqlite_array_walk(unqlite_value *pArray, int (*xWalk)(unqlite_value *, unqlite_value *, void *), void *pUserData);
UNQLITE_APIEXPORT int unqlite_array_add_elem(unqlite_value *pArray, unqlite_value *pKey, unqlite_value *pValue);
UNQLITE_APIEXPORT int unqlite_array_add_strkey_elem(unqlite_value *pArray, const char *zKey, unqlite_value *pValue);
Expand Down
54 changes: 54 additions & 0 deletions unqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ UNQLITE_APIEXPORT int unqlite_value_is_empty(unqlite_value *pVal);

/* JSON Array/Object Management Interfaces */
UNQLITE_APIEXPORT unqlite_value * unqlite_array_fetch(unqlite_value *pArray, const char *zKey, int nByte);
UNQLITE_APIEXPORT unqlite_value * unqlite_array_fetch_by_index(unqlite_value *pArray, unqlite_int64 iIndex);
UNQLITE_APIEXPORT int unqlite_array_walk(unqlite_value *pArray, int (*xWalk)(unqlite_value *, unqlite_value *, void *), void *pUserData);
UNQLITE_APIEXPORT int unqlite_array_add_elem(unqlite_value *pArray, unqlite_value *pKey, unqlite_value *pValue);
UNQLITE_APIEXPORT int unqlite_array_add_strkey_elem(unqlite_value *pArray, const char *zKey, unqlite_value *pValue);
Expand Down Expand Up @@ -1461,6 +1462,7 @@ JX9_PRIVATE int jx9_value_resource(jx9_value *pVal, void *pUserData);
JX9_PRIVATE int jx9_value_release(jx9_value *pVal);
/* JSON Array/Object Management Interfaces */
JX9_PRIVATE jx9_value * jx9_array_fetch(jx9_value *pArray, const char *zKey, int nByte);
JX9_PRIVATE jx9_value * jx9_array_fetch_by_index(jx9_value *pArray, jx9_int64 iIndex);
JX9_PRIVATE int jx9_array_walk(jx9_value *pArray, int (*xWalk)(jx9_value *, jx9_value *, void *), void *pUserData);
JX9_PRIVATE int jx9_array_add_elem(jx9_value *pArray, jx9_value *pKey, jx9_value *pValue);
JX9_PRIVATE int jx9_array_add_strkey_elem(jx9_value *pArray, const char *zKey, jx9_value *pValue);
Expand Down Expand Up @@ -3043,6 +3045,7 @@ JX9_PRIVATE sxi32 jx9HashmapLoadBuiltin(jx9_vm *pVm);
JX9_PRIVATE sxi32 jx9HashmapRelease(jx9_hashmap *pMap, int FreeDS);
JX9_PRIVATE void jx9HashmapUnref(jx9_hashmap *pMap);
JX9_PRIVATE sxi32 jx9HashmapLookup(jx9_hashmap *pMap, jx9_value *pKey, jx9_hashmap_node **ppNode);
JX9_PRIVATE sxi32 jx9HashmapLookupIntKey(jx9_hashmap *pMap, sxi64 iKey, jx9_hashmap_node **ppNode);
JX9_PRIVATE sxi32 jx9HashmapInsert(jx9_hashmap *pMap, jx9_value *pKey, jx9_value *pVal);
JX9_PRIVATE sxi32 jx9HashmapUnion(jx9_hashmap *pLeft, jx9_hashmap *pRight);
JX9_PRIVATE sxi32 jx9HashmapDup(jx9_hashmap *pSrc, jx9_hashmap *pDest);
Expand Down Expand Up @@ -5163,6 +5166,14 @@ unqlite_value * unqlite_array_fetch(unqlite_value *pArray, const char *zKey, int
{
return jx9_array_fetch(pArray,zKey,nByte);
}
/*
* [CAPIREF: unqlite_array_fetch_by_index()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
unqlite_value * unqlite_array_fetch_by_index(unqlite_value *pArray, unqlite_int64 iIndex)
{
return jx9_array_fetch_by_index(pArray, iIndex);
}
/*
* [CAPIREF: unqlite_array_walk()]
* Please refer to the official documentation for function purpose and expected parameters.
Expand Down Expand Up @@ -8270,6 +8281,29 @@ JX9_PRIVATE jx9_value * jx9_array_fetch(jx9_value *pArray, const char *zKey, int
pValue = (jx9_value *)SySetAt(&pArray->pVm->aMemObj, pNode->nValIdx);
return pValue;
}
/*
* [CAPIREF: jx9_array_fetch_by_index()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
JX9_PRIVATE jx9_value * jx9_array_fetch_by_index(jx9_value *pArray, jx9_int64 iIndex)
{
jx9_hashmap_node *pNode;
jx9_value *pValue;
int rc;
/* Make sure we are dealing with a valid hashmap */
if( (pArray->iFlags & MEMOBJ_HASHMAP) == 0 ){
return 0;
}
/* Perform the lookup */
rc = jx9HashmapLookupIntKey((jx9_hashmap *)pArray->x.pOther, iIndex, &pNode);
if( rc != JX9_OK ){
/* No such entry */
return 0;
}
/* Extract the target value */
pValue = (jx9_value *)SySetAt(&pArray->pVm->aMemObj, pNode->nValIdx);
return pValue;
}
/*
* [CAPIREF: jx9_array_walk()]
* Please refer to the official documentation for function purpose and expected parameters.
Expand Down Expand Up @@ -23324,6 +23358,26 @@ JX9_PRIVATE sxi32 jx9HashmapLookup(
rc = HashmapLookup(&(*pMap), &(*pKey), ppNode);
return rc;
}
/*
* Check if a given key exists in the given hashmap.
* Write a pointer to the target node on success.
* Otherwise SXERR_NOTFOUND is returned on failure.
*/
JX9_PRIVATE sxi32 jx9HashmapLookupIntKey(
jx9_hashmap *pMap, /* Target hashmap */
sxi64 iKey, /* Lookup key */
jx9_hashmap_node **ppNode /* OUT: Target node on success */
)
{
sxi32 rc;
if( pMap->nEntry < 1 ){
/* TICKET 1433-25: Don't bother hashing, the hashmap is empty anyway.
*/
return SXERR_NOTFOUND;
}
rc = HashmapLookupIntKey(&(*pMap), iKey, ppNode);
return rc;
}
/*
* Insert a given key and it's associated value (if any) in the given
* hashmap.
Expand Down
1 change: 1 addition & 0 deletions unqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,7 @@ UNQLITE_APIEXPORT int unqlite_value_is_empty(unqlite_value *pVal);

/* JSON Array/Object Management Interfaces */
UNQLITE_APIEXPORT unqlite_value * unqlite_array_fetch(unqlite_value *pArray, const char *zKey, int nByte);
UNQLITE_APIEXPORT unqlite_value * unqlite_array_fetch_by_index(unqlite_value *pArray, unqlite_int64 iIndex);
UNQLITE_APIEXPORT int unqlite_array_walk(unqlite_value *pArray, int (*xWalk)(unqlite_value *, unqlite_value *, void *), void *pUserData);
UNQLITE_APIEXPORT int unqlite_array_add_elem(unqlite_value *pArray, unqlite_value *pKey, unqlite_value *pValue);
UNQLITE_APIEXPORT int unqlite_array_add_strkey_elem(unqlite_value *pArray, const char *zKey, unqlite_value *pValue);
Expand Down