(三)CvSeq数据结构

来源:互联网 发布:速卖通打单软件 编辑:程序博客网 时间:2024/06/04 18:51

源代码定义

#define CV_TREE_NODE_FIELDS(node_type)                               /
    int       flags;             /* Miscellaneous flags.     */      /
    int       header_size;       /* Size of sequence header. */      /
    struct    node_type* h_prev; /* Previous sequence.       */      /
    struct    node_type* h_next; /* Next sequence.           */      /
    struct    node_type* v_prev; /* 2nd previous sequence.   */      /
    struct    node_type* v_next  /* 2nd next sequence.       */

/*
   Read/Write sequence.
   Elements can be dynamically inserted to or deleted from the sequence.
*/
#define CV_SEQUENCE_FIELDS()                                              /
    CV_TREE_NODE_FIELDS(CvSeq);                                           /
    int       total;          /* Total number of elements.            */  /
    int       elem_size;      /* Size of sequence element in bytes.   */  /
    schar*    block_max;      /* Maximal bound of the last block.     */  /
    schar*    ptr;            /* Current write pointer.               */  /
    int       delta_elems;    /* Grow seq this many at a time.        */  /
    CvMemStorage* storage;    /* Where the seq is stored.             */  /
    CvSeqBlock* free_blocks;  /* Free blocks list.                    */  /
    CvSeqBlock* first;        /* Pointer to the first sequence block. */

typedef struct CvSeq
{
    CV_SEQUENCE_FIELDS()
}
CvSeq;

 

还有一个结构序列块

typedef struct CvSeqBlock
{
    struct CvSeqBlock*  prev; /* Previous sequence block.                   */
    struct CvSeqBlock*  next; /* Next sequence block.                       */
  int    start_index;         /* Index of the first element in the block +  */
                              /* sequence->first->start_index.              */
    int    count;             /* Number of elements in the block.           */
    schar* data;              /* Pointer to the first element of the block. */
}
CvSeqBlock;

 

就我的理解是

一个序列里面有许多序列块,而每个序列块里面存储着多个元素。多个序列之间可以通过

宏CV_TREE_NODE_FIELDS(CvSeq)进行联系,进而形成各种数据结构。序列中的

多个序列块也是通过序列块中的prev,next指针连接起来形成双向链表。

 

序列的创建需要从动态存储器中申请分配空间,元素的增加插入等操作也需要向存储器申请空间。

 

比较难以理解的结构中变量

 schar*    block_max;最后一个序列块中已经分配空间的最远地址边界,主要用于与下一个指针进行比较,判断未用空间是否满足元素增加的分配。

 schar*    ptr;指示当前元素存储位置,依次存储增加的新元素,在地址空间[ptr,block_max)中可以用于存储新元素。

 int       delta_elems; 当[ptr,block_max)地址空间不够存储新元素时,重新向存储器申请的空间大小。

 CvSeqBlock* free_blocks;空的序列块列表

 

int start_index; 每个块中的第一个元素在整个序列元素中的索引。一般第一块第一个元素为0,插入删除操作后会变化。

原创粉丝点击