//this documents will be obsolete ,using dsm.h instead
#ifndef  __RV_IDSM_H_INCLUDED_
#define  __RV_IDSM_H_INCLUDED_

 

#define DSM_API WINAPI

 
#include <rvb\rvtypes.h>
#include <rvb\mpm.h>

  




//collect structure
#define RV_DS_FOREWARD       0
#define RV_DS_BACKWARD      -1

#define RV_DS_FIRST         0
#define RV_DS_LAST         -1

#define RV_WHOLE_SEQ_END_INDEX    0x3fffffff
#define RV_WHOLE_SEQ              RV_SLICE(0, RV_WHOLE_SEQ_END_INDEX)

#define RV_MAX_ELEM_SIZE  1024




/* a < b ? -1 : a > b ? 1 : 0 */
typedef int (* RvCmpFunc) (const void* pA, const void* pB, void* pUserData );

extern "C" void DSM_API  rvdBeginRead_(RvDynamic collect, void* pReader, BOOL bReverse=FALSE);
extern "C"  void DSM_API rvdEndRead_(RvDynamic collect, void* pReader);

extern "C" void DSM_API  rvdBeginWrite_(RvDynamic collect, void* pWriter);
extern "C" void DSM_API rvdEndWrite_(RvDynamic collect, void* pWriter);


extern "C" void DSM_API   rvdSetRecycler(RvDynamic collect, RvDestroyFunc fnDestroy    );
extern "C" void DSM_API   rvdSetComparison(RvDynamic collect, RvCompareFunc fnCompare  ,  void* pUserData=NULL );
 

extern "C" void DSM_API  rvdSetCriterion(RvDynamic collect, RvConditionFunc fnCondition  ,  void* pUserData=NULL );


extern "C" RvMemoryStore DSM_API  rvdSetMemoryStore(RvDynamic collect, RvMemoryStore memoryStore  , BOOL bDataStore =FALSE );
extern "C" RvMemoryStore DSM_API  rvdGetMemoryStore(RvDynamic collect,   BOOL bDataStore =FALSE );




/********************************* SEQUENCE ************************************************/
#define RV_SQ_PREV      1 
#define RV_SQ_NEXT      2  
 

/* Creates new empty sequence that will reside in the specified storage */
extern "C" RvSequence DSM_API  rvCreateSequence( /*int  dataType,*/   RvDestroyFunc fnDestroyElem=NULL);
extern "C" RvSequence DSM_API  rvCreateSequenceEx( /*UINT nElemSize,*/ int flag  , RvDestroyFunc fnDestroyElem=NULL);
extern "C" void     DSM_API    rvDestroySequence(RvSequence sequence);


/* Calculates length of sequence slice (with support of negative indices). */
extern "C" int DSM_API rsqGetSliceLength(const RvSequence sequence , RvSlice slice);
 

/* Adds new element to the end of sequence. Returns pointer to the element */
extern "C" HITEM DSM_API  rsqAddLast_( RvSequence sequence, void*   pVal);


/* Adds new element to the end of sequence. Returns pointer to the element */
extern "C" HITEM DSM_API  rsqAppend_( RvSequence sequence, void*   pVal);

/* Adds new element to the beginning of sequence. Returns pointer to it */
extern "C" HITEM DSM_API  rsqAddFirst_( RvSequence sequence, void*   pVal);

/* Removes the last element from sequence and optionally saves it */
extern "C" void*  DSM_API  rsqRemoveLast_( RvSequence sequence/*, void*   pVal=NULL*/);

/* Removes the first element from sequence and optioanally saves it */
extern "C" void*  DSM_API  rsqRemoveFirst_( RvSequence sequence/*, void*   &pVal*/);

//仅限数值或指针基本数据类型，且通道为1
extern "C" int DSM_API    rsqFindIndex_( RvSequence sequence, void* pVal);

extern "C"  HITEM DSM_API   rsqFindIndexEx_( RvSequence sequence, void* pVal);

/* Adds several new elements to the end of sequence */
//extern "C" void DSM_API  rsqAddMulti( RvSequence sequence, RvSlice slice,  const void*  pElemArray, int nElemCount, BOOL bReverse=FALSE);

/* Removes several elements from the end of sequence and optionally saves them */

//#define rsgRemoveBatch(seq, slice)  rsqRemoveMulti(seq, slice)

//下面两个函数待测#20240515
extern "C" int DSM_API  rsqRemoveMulti( RvSequence sequence,  RvSlice slice, void**  pElemArray , int nElemCount , BOOL bReverse=FALSE);
extern "C" int DSM_API   rsqExtractToArray( const RvSequence sequence, RvSlice slice, void** pElemArray, int nArraySize, BOOL bReverse);

// Inserts a new element in the middle of sequence.
//如果index 小于0,直接添加到序列的首部的下一个。如果sequence为空，则直接添加。
//如果index 超出了范围，直接添加到序列尾部.
//新的节点添加到索引号为index节点的后面，即新的索引号为index+1.
//如果需要直接添加到序列头部，请直接使用rsqAddFirst.
extern "C" HITEM DSM_API rsqInsertAt_( RvSequence sequence, int index,  void*   pVal );

extern "C"  HITEM DSM_API  rsqInsertBefore_( RvSequence sequence, int index,  void*   pVal );
extern "C" HITEM DSM_API  rsqInsertAfter_( RvSequence sequence, int index,  void*   pVal );

// Inserts a new element in the middle of sequence.
//默认情况下，产生一个新节点，并将该节点加入到hCurItem的前面，
//如果bAfter为TRUE，则加入到hCurItem后面。
//当hCurItem为NULL的时候，将节点加入到序列的最前面。
//如果bAfter为TRUE，将节点加入到序列的最后面。
extern "C" HITEM DSM_API rsqInsertEx_( RvSequence sequence, HITEM hCurItem,  void*   pVal, BOOL bAfter=FALSE );



/* Removes specified sequence element at postion index, then return next node item */
extern "C" HITEM DSM_API   rsqRemoveAt_( RvSequence sequence, int index);
 
 
/* Removes specified sequence element by element value, 比较回调函数来进行比较 */
extern "C" void DSM_API   rsqRemoveElem_( RvSequence sequence, void* pVal);

//remove specific element by its handle, return next item.
extern "C" HITEM  DSM_API  rsqRemoveItem_( RvSequence sequence, HITEM hItem);

extern "C" void* DSM_API  rsqGetFirst_( const RvSequence sequence);


/* Removes all the elements from the sequence. The freed memory
   can be reused later only by the same sequence unless rsqClearMemStorage
   or rsqRestoreMemStoragePos is called */
extern "C" void DSM_API   rsqClear(RvSequence sequence );

extern "C" BOOL DSM_API   rsqIsEmpty(RvSequence sequence );
extern "C" int  DSM_API   rsqGetCount(RvSequence sequence);


extern "C" void* DSM_API  rsqGetItemValue_( const RvSequence sequence, int index/*, void* pVal*/);
extern "C" HITEM DSM_API  rsqSetItemValue_( const RvSequence sequence, int index, void* pVal );
   
extern "C" void* DSM_API  rsqGetItemValueE1_( const RvSequence sequence, HITEM item/*, void* pVal */);
extern "C" void DSM_API   rsqSetItemValueE1_( const RvSequence sequence, HITEM item, void* pVal );
 


/* Copies sequence content to a continuous piece of memory */
//extern "C" int DSM_API   rsqRevertToArray( const RvSequence sequence, RvSlice slice, void* pElemArray, int nArraySize);

/* Extracts sequence slice */
//extern "C" RvSequence DSM_API rsqExtract( const RvSequence sequence, RvSlice slice, RvSequence pDest=NULL, BOOL bCopyData=FALSE);




extern "C"  HITEM  DSM_API rsqGetHead(const RvSequence sequence);   
extern "C" HITEM DSM_API   rsqGetTail(const RvSequence sequence);

extern "C"  HITEM DSM_API   rsqGetItemAt( const RvSequence sequence, int index );
 extern "C"  HITEM  DSM_API rsqGetNextItem(const RvSequence sequence, HITEM item, int nCode = RV_SQ_NEXT);   


/* Inserts a sequence or array into another sequence */
//extern "C" void  DSM_API  rsqInsertMatrix(RvSequence sequence,    int after, const RvMatrix matrix );
//extern "C" void DSM_API   rsqInsertSequence( RvSequence sequence, int after, const RvSequence other );


/* Sorts sequence in-place given element comparison function */
//extern "C" RvSequence DSM_API  rsqSort( RvSequence sequence );

/* Finds element in a  sequence */
//extern "C" HITEM DSM_API  rsqSearch( RvSequence sequence, RvSlice slice, const void*  pVal);

/* Reverses order of sequence elements in-place, return the first item */
//extern "C" HITEM DSM_API  rsqInvert( RvSequence sequence );

/* Splits sequence into one or more equivalence classes using the specified criteria */
//extern "C" int DSM_API   rsqSplit(  RvSequence sequence,  RvSequence  outcome );

/* Splits sequence into one or more equivalence classes using the specified criteria */
 extern "C" int DSM_API   rsqMerge(RvSequence sequence,  RvSequence  income  );
 

/* Creates new empty cluster  */
//注意： cluster 与sequence不同的是，直接返回数据地址，不是节点地址.
//       CLUSTER不使用专门的内存分配节点。
extern "C" RvCluster DSM_API  rvCreateCluster(int dataType, RvDestroyFunc fnDestroyElem=NULL);
extern "C" RvCluster DSM_API  rvCreateClusterEx(UINT nElemSize, int flag=-1, RvDestroyFunc fnDestroyElem=NULL);

extern "C" void DSM_API    rvDestroyCluster(RvCluster pClust);

extern "C" void DSM_API    rclAppend(RvCluster cluster,  void* pVal);

//nArraySize 为数组元素的个数，不是内存大小
extern "C" void DSM_API    rclAppendEx(RvCluster cluster,  void* pElemArray, int nArraySize);
extern "C" void DSM_API    rclRemove(RvCluster cluster,  void* pVal);

extern "C" void DSM_API    rclClear(RvCluster cluster);
extern "C" BOOL DSM_API    rclIsEmpty(RvCluster cluster);
extern "C" int  DSM_API    rclGetCount(RvCluster cluster);

extern "C" void* DSM_API   rclGetAt(RvCluster cluster, int index);
 
extern "C" void  DSM_API   rclSetAt(RvCluster cluster, int index, void* pVal);
 
extern "C" void  DSM_API   rclRemoveAt(RvCluster cluster, int index);
extern "C" void DSM_API    rclRemoveAll(RvCluster cluster);

//将两个CLUSTER合并，合并的结果保存在dest, src里面数据自动清零。
extern "C" RvCluster DSM_API    rclMerge(RvCluster dest , RvCluster src );

//nArraySize 为数组元素的个数，不是内存大小
extern "C" int  DSM_API   rclRevertToArray(const RvCluster cluster, RvSlice slice, void* pElemArray, int nArraySize);
extern "C" void DSM_API   rclSetArray(RvCluster cluster,  void* pElemArray, int nArraySize);


#define RCL_GET_AT(type, cluster, index)    (*((type*)rclGetAt(cluster, index)))
 

/*************************RvList (thread safe)*****************************************/
/* Creates new empty list */
extern "C" RvList DSM_API   rvCreateList(RvDestroyFunc fnDestroyElem=NULL);
extern "C" void  DSM_API    rvDestroyList(RvList list);

extern "C" void* DSM_API     rlsGetHead(RvList list);
extern "C" void* DSM_API	 rlsGetTail(RvList list);
extern "C" void* DSM_API	 rlsGetCur(RvList list);	
extern "C" void* DSM_API	 rlsGetAt(RvList list, int index);
extern "C" void DSM_API	     rlsAddHead(RvList list, void* val  );
extern "C" void  DSM_API	  rlsAddTail(RvList list, void* val); 
extern "C" void DSM_API      rlsAdd(RvList list, void* val); 
extern "C" void   DSM_API    rlsRemoveAll(RvList list);
extern "C" void* DSM_API	  rlsRemoveHead(RvList list);
extern "C" void* DSM_API	  rlsRemoveTail(RvList list);
extern "C" void* DSM_API	  rlsRemoveCur(RvList list);

extern "C" void  DSM_API	   rlsRemove(RvList list, void* pVal);

extern "C" void* DSM_API  rlsRemoveAt(RvList list, int index);
extern "C" BOOL DSM_API	  rlsGoHead(RvList list);
extern "C" BOOL  DSM_API  rlsGoTail(RvList list);	 
extern "C" BOOL DSM_API	  rlsStep(RvList list, BOOL bBackward=FALSE);
extern "C" int  DSM_API	  rlsGetCount(RvList list);

extern "C" BOOL DSM_API	 rlsIsEmpty(RvList list);
extern "C" BOOL DSM_API	 rlsIsBegin(RvList list);
extern "C" BOOL DSM_API	 rlsIsEnd(RvList list);


/************************* RvTree *****************************************/

//A flag indicating the type of relation to hItem. This flag can be one of the following values:
#define RTR_ROOT    0 //Retrieves the first child 
#define RTR_PARENT  1 // Retrieves the parent of the specified item.
#define RTR_CHILD   2 //Retrieves the first child item of the item specified by the hItem parameter. 
 
#define RTR_SIBLING 3 //Retrieves the first child item of the item specified by the hItem parameter. 
#define RTR_NEXT     RTR_SIBLING //Retrieves the previous sibling item.
#define RTR_PREV     4 //Retrieves the previous sibling item.
#define RTR_RIGHT     RTR_NEXT 
#define RTR_LEFT      RTR_PREV 
#define RTR_LAST      5 
#define RTR_FIRST     6




//树形结构为beta，没有充分测试。 on 20230806

extern "C" RvTree DSM_API     rvCreateTree(RvDestroyFunc fnDestroyElem=NULL/*int dataType*/);
extern "C" void   DSM_API     rvDestroyTree(RvTree tree);

extern "C" void   DSM_API     rtrClear(RvTree tree);
//总节点
extern "C" int   DSM_API     rtrGetCount(RvTree tree);



extern "C"  HITEM DSM_API  rtrGetRoot(RvTree tree);
extern "C"  HITEM DSM_API  rtrGetChildItem(HITEM hItem );
extern "C"  HITEM DSM_API  rtrGetNextItem( HITEM hItem);
extern "C"  HITEM DSM_API  rtrGetParentItem( HITEM hItem);

 
//删除本身节点和所有子节点
extern "C" void DSM_API   rtrRemove(RvTree tree, HITEM hItem); 


//添加失败的化会返回NULL.
//if ncode == ROOT, hItem maybe NULL
extern "C" HITEM DSM_API  rtrInsert(RvTree tree,  HITEM hItem, UINT nCode, void* pVal); 
 
//tree VALUE仅支持指针
extern "C" void* DSM_API rtrGetItemValue(HITEM hItem); 
extern "C" void  DSM_API rtrSetItemValue(HITEM hItem, void* pVal); 


extern "C" BOOL    DSM_API   rtrIsEmpty(RvTree tree); 

extern "C" void    DSM_API   rtrSetFlags(HITEM hItem, int flags); 
extern "C" int    DSM_API   rtrGetFlags(HITEM hItem ); 



//if sub=null, create new tree
extern "C" RvTree  DSM_API   rtrSplit(RvTree tree, HITEM hItem, RvTree sub); 




//
//#define RV_BEGIN_WRITE_TREE(tree, writer) \
//	(writer)->count = (tree)->total;  \
// 	(writer)->elemSize = (tree)->elemSize;  \
//	(writer)->nodeStore = (tree)->nodeStore; \
//	(writer)->dataStore = (tree)->dataStore; \
//	(writer)->current =  (tree)->root ;  
//
//
//#define RV_WRITE_TO_TREE(writer, val) \
//{ \
//    RvTreeItem*    pTemp =(RvTreeItem*)rmsAlloc((writer)->nodeStore); \
//	RV_ASSERT(pTemp);  \
//	memset(pTemp, 0, sizeof(RvTreeItem)); \
//	pTemp->data.ptr = rmsAlloc((writer)->dataStore); \
//    RV_ASSERT(pTemp->data.ptr);  \
//    memcpy(pTemp->data.ptr,  (val), (writer)->elemSize); \
//	(writer)->current->child = pTemp; pTemp->parent = (writer)->current; \
//	(writer)->current = pTemp;  \
//	(writer)->count++; \
//} 
//
//#define RV_END_WRITE_TREE(tree, writer )  (tree)->total = (writer)->count;		    
//
//
//#define RV_BEGIN_READ_TREE(tree, reader)  \
//	(reader)->elemSize = (tree)->elemSize; \
//	(reader)->current = (tree)->root;  
//
//#define RV_READ_FR_TREE(reader, val)  \
//{    \
//	RV_ASSERT((reader)->current);  \
//	memcpy((val), (reader)->current->data.ptr, (reader)->elemSize);  \
//	(reader)->current = (reader)->current->child;  \
//}
//
//#define RV_IS_TREE_END(reader)    (NULL == ((reader)->current))
//#define RV_END_READ_TREE(tree, reader ) (reader)->current=NULL;
// 




#endif