Source Code definition
# 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 variable 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;
There is also a structure sequence block
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;
In my understanding
A sequence contains many sequence blocks, and each sequence block stores multiple elements. You can use
The macro cv_tree_node_fields (cvseq) is connected to form various data structures. In the sequence
Multiple sequence blocks are connected by the prev and next pointers in the sequence blocks to form a two-way linked list.
To create a sequence, you need to apply for space allocation from the dynamic storage, and to apply for space from memory for operations such as adding and inserting elements.
Variable in a structure that is hard to understand
Schar * block_max; the farthest address boundary of the allocated space in the last sequence block. It is mainly used to compare it with the next pointer to determine whether the unused space meets the needs of the increased allocation of elements.
Schar * PTR: Specifies the storage location of the current element. It stores new elements in sequence and can be used to store new elements in the address space [PTR, block_max.
Int delta_elems; when the [PTR, block_max) address space is not enough to store new elements, the size of the space requested from the memory is re-applied.
Cvseqblock * free_blocks; empty sequence block list
Int start_index; the index of the first element in each block in the whole sequence element. Generally, the first element of the first block is 0, and the insert/delete operation changes.