📄 pvaltree.c
字号:
/************************************************************************
*
* Public functions
*
************************************************************************/
/************************************************************************
* pvtConstruct
* purpose: Construct a pool of PVT nodes.
* input : stringBufferSize - Not used
* numOfNodes - The maximum number of nodes to be allowed
* in the tree
* output : none
* return : Handle to PVT constructed on success
* NULL on failure
************************************************************************/
RVAPI HPVT RVCALLCONV
pvtConstruct(
IN int stringBufferSize,
IN int numOfNodes)
{
vtStruct *vt;
if(stringBufferSize);
if(RvMemoryAlloc(NULL, (void**)&vt, sizeof(vtStruct)) != RV_OK)
return NULL;
memset(vt, 0, sizeof(vtStruct));
RvLogSourceConstruct(RvLogGet(), &vt->log, RV_LOG_LIBCODE_ASN1, "VT", "Value Tree");
RvLogSourceConstruct(RvLogGet(), &vt->unregLog, RV_LOG_LIBCODE_ASN1, "UNREG", "Any non-registered user log");
vt->vTree = rtConstruct(sizeof(vtNode), numOfNodes, "VT tree");
vt->sPool = (HRPOOL)vt->vTree;/*rpoolConstruct(stringBufferSize);*/
RvMutexConstruct(&vt->mutex);
if (!vt->vTree || !vt->sPool)
{
pvtDestruct((HPVT)vt);
return NULL;
}
rtSetCompareFunc(vt->vTree, pvtNodeCompare);
rtSetPrintFunc(vt->vTree, vtNodePrint);
return (HPVT)vt;
}
/************************************************************************
* pvtDestruct
* purpose: Destruct a pool of PVT nodes.
* input : valH - The PVT handle
* output : none
* return : Non-negative value on success
* Negative value on failure
************************************************************************/
RVAPI int RVCALLCONV
pvtDestruct(IN HPVT hVal)
{
vtStruct *vt = (vtStruct *)hVal;
if (vt == NULL)
return RV_ERROR_NULLPTR;
if (rtCurSize(vt->vTree) > 0)
{
RvLogWarning(&vt->log, (&vt->log,
"pvtDestruct: %d nodes left in database", rtCurSize(vt->vTree)));
}
rtDestruct(vt->vTree);
RvMutexDestruct(&vt->mutex);
RvLogSourceDestruct(RvLogGet(), &vt->log);
RvLogSourceDestruct(RvLogGet(), &vt->unregLog);
RvMemoryFree(vt);
return RV_OK;
}
/************************************************************************
* pvtGetRoot
* purpose: Returns the Node ID of the root of a Value Tree to which a
* specified node belongs
* input : valH - The PVT handle
* nodeId - The ID of the node inside a tree
* output : none
* return : Root node ID of the given node ID on success
* Negative value on failure
************************************************************************/
RVAPI RvPvtNodeId RVCALLCONV
pvtGetRoot(
IN HPVT hVal,
IN RvPvtNodeId nodeId)
{
RvPvtNodeId result;
vtStruct* vt = (vtStruct *)hVal;
if (!hVal)
return RV_PVT_ERROR_UNKNOWN;
RvMutexLock(&vt->mutex);
result = (RvPvtNodeId)rtGetRoot(vt->vTree, (int)nodeId);
RvMutexUnlock(&vt->mutex);
return result;
}
/************************************************************************
* pvtGetSynTree
* purpose: Returns a handle to the structure (Syntax Tree) definition of
* a specified node
* input : valH - The PVT handle
* nodeId - The ID of the node the syntax of which is returned
* output : none
* return : A handle to the structure (Syntax Tree) definition on success
* NULL on failure
************************************************************************/
RVAPI HPST RVCALLCONV
pvtGetSynTree(
IN HPVT hVal,
IN RvPvtNodeId nodeId)
{
HPST result = NULL;
vtNode* node;
vtStruct* vt = (vtStruct *)hVal;
if (!hVal) return NULL;
RvMutexLock(&vt->mutex);
if ( (node = (vtNode *)rtGetByPath(vt->vTree, (int)nodeId)) )
result = node->hSyn;
RvMutexUnlock(&vt->mutex);
return result;
}
/*** Statistics ***/
/************************************************************************
* pvtCurSize
* purpose: Gets the number of nodes currently used in the Value Tree forest
* input : valH - The PVT handle
* output : none
* return : The number of nodes in the Value Tree forest on success
* Negative value on failure
************************************************************************/
RVAPI int RVCALLCONV
pvtCurSize(IN HPVT hVal)
{
vtStruct *vt = (vtStruct *)hVal;
if (!vt) return RV_ERROR_UNKNOWN;
return rtCurSize(vt->vTree);
}
/************************************************************************
* pvtMaxUsage
* purpose: Gets the highest number of nodes used in the Value Tree forest
* since the cmInitialize() function was called.
* input : valH - The PVT handle
* output : none
* return : The maximum number of nodes used in the Value Tree forest on success
* Negative value on failure
************************************************************************/
RVAPI int RVCALLCONV
pvtMaxUsage(IN HPVT hVal)
{
vtStruct *vt = (vtStruct *)hVal;
if (!vt) return RV_ERROR_UNKNOWN;
return rtMaxUsage(vt->vTree);
}
/************************************************************************
* pvtMaxSize
* purpose: Gets the highest number of nodes that cab be used in the Value Tree forest
* input : valH - The PVT handle
* output : none
* return : The maximum number of nodes in the Value Tree forest on success
* Negative value on failure
************************************************************************/
RVAPI int RVCALLCONV
pvtMaxSize(IN HPVT hVal)
{
if (!hVal) return RV_ERROR_UNKNOWN;
return rtMaxSize(((vtStruct *)hVal)->vTree);
}
/************************************************************************
* pvtPoolStatistics
* purpose: Get pool statistics (space is in bytes)
* input : valH - The PVT handle
* output : poolSize - Maximum size of pool
* availableSpace - Current available space
* maxFreeChunk - Always returned as 0
* numOfChunks - Always returned as 0
* If any output parameter is set to NULL, it will be discarded
* return : Non-negative value on success
* Negative value on failure
************************************************************************/
RVAPI int RVCALLCONV
pvtPoolStatistics(
IN HPVT hVal,
OUT RvInt32* poolSize,
OUT RvInt32* availableSpace,
OUT RvInt32* maxFreeChunk,
OUT RvInt32* numOfChunks)
{
vtStruct *vt = (vtStruct *)hVal;
RvInt32 usedSize, allocSize;
RvStatus status;
if (vt == NULL)
return RV_ERROR_NULLPTR;
if (maxFreeChunk) *maxFreeChunk=0;
if (numOfChunks) *numOfChunks=0;
status = rpoolStatistics(vt->sPool, &allocSize, NULL, &usedSize);
if (status == RV_OK)
{
if (poolSize != NULL)
*poolSize = allocSize;
if (availableSpace != NULL)
*availableSpace = allocSize - usedSize;
}
return status;
}
/************************************************************************
* pvtTreeSize
* purpose: Returns the number of nodes included in a Value Tree root
* input : valH - The PVT handle
* parentId - The ID of any node. The function returns the
* number of nodes located under the specified parent.
* output : none
* return : The number of nodes included in a Value Tree on success
* Negative value on failure
************************************************************************/
RVAPI int RVCALLCONV
pvtTreeSize(
IN HPVT hVal,
IN RvPvtNodeId parentId)
{
int result;
vtStruct *vt = (vtStruct *)hVal;
if (!vt) return RV_ERROR_UNKNOWN;
RvMutexLock(&vt->mutex);
result = rtTreeSize(vt->vTree, (int)parentId);
RvMutexUnlock(&vt->mutex);
return result;
}
/************************************************************************
* pvtNumChilds
* purpose: Returns the number of the dependents (children) of a parent tree
* input : valH - The PVT handle
* parentId - The ID of any node
* output : none
* return : The number of immediate nodes under the given node on success
* Negative value on failure
************************************************************************/
RVAPI int RVCALLCONV
pvtNumChilds(
IN HPVT hVal,
IN RvPvtNodeId parentId)
{
int result;
vtStruct *vt = (vtStruct *)hVal;
if (!vt) return RV_ERROR_UNKNOWN;
RvMutexLock(&vt->mutex);
result = rtNumChilds(vt->vTree, (int)parentId);
RvMutexUnlock(&vt->mutex);
return result;
}
/************************************************************************
* pvtParent
* purpose: Returns the ID of the parent node of a specified node
* input : valH - The PVT handle
* nodeId - The ID of any node
* output : none
* return : The parent ID of the given node on success
* Negative value on failure
************************************************************************/
RVAPI RvPvtNodeId RVCALLCONV
pvtParent(
IN HPVT hVal,
IN RvPvtNodeId nodeId)
{
RvPvtNodeId result;
vtStruct *vt = (vtStruct *)hVal;
if (!vt)
return RV_PVT_ERROR_UNKNOWN;
RvMutexLock(&vt->mutex);
result = (RvPvtNodeId)rtParent(vt->vTree, (int)nodeId);
RvMutexUnlock(&vt->mutex);
return result;
}
/************************************************************************
* pvtBrother
* purpose: Returns the Node ID of a specified node's brother (right).
* input : valH - The PVT handle
* nodeId - The ID of any node
* output : none
* return : The node ID of the given node's brother on success
* Negative value on failure
* The function returns the next child (rightward). Use pvtChild() to get
* the first dependent, and then use pvtBrother() to get to the next brother.
************************************************************************/
RVAPI RvPvtNodeId RVCALLCONV
pvtBrother(
IN HPVT hVal,
IN RvPvtNodeId nodeId)
{
RvPvtNodeId result;
vtStruct *vt = (vtStruct *)hVal;
if (!vt)
return RV_PVT_ERROR_UNKNOWN;
RvMutexLock(&vt->mutex);
result = (RvPvtNodeId)rtBrother(vt->vTree, (int)nodeId);
RvMutexUnlock(&vt->mutex);
return result;
}
/************************************************************************
* pvtLBrother
* purpose: Gets the ID of the node before (left) a particular node
* input : valH - The PVT handle
* nodeId - The ID of any node
* output : none
* return : The node ID of the previous node on success
* Negative value on failure
************************************************************************/
RVAPI RvPvtNodeId RVCALLCONV
pvtLBrother(
IN HPVT hVal,
IN RvPvtNodeId nodeId)
{
vtStruct* vt = (vtStruct *)hVal;
RvPvtNodeId parentId;
RvPvtNodeId lbrotherId = RV_PVT_ERROR_UNKNOWN;
RvPvtNodeId currId;
RvMutexLock(&vt->mutex);
parentId = pvtParent(hVal, nodeId);
if (!RV_PVT_NODEID_IS_VALID(parentId))
{
RvMutexUnlock(&vt->mutex);
return RV_PVT_ERROR_UNKNOWN;
}
currId = pvtChild(hVal, parentId);
while (currId != nodeId)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -