dynamic_structures.rst 57.8 KB
Newer Older
1
Dynamic Structures
2
==================
3 4 5 6 7 8 9 10 11 12

.. highlight:: c

The section describes OpenCV 1.x API for creating growable sequences and other dynamic data structures allocated in ``CvMemStorage``. If you use the new C++, Python, Java etc interface, you will unlikely need this functionality. Use ``std::vector`` or other high-level data structures.

CvMemStorage
------------

.. ocv:struct:: CvMemStorage

13
  A storage for various OpenCV dynamic data structures, such as ``CvSeq``, ``CvSet`` etc.
14

15
  .. ocv:member:: CvMemBlock* bottom
16

17
     the first memory block in the double-linked list of blocks
18

19
  .. ocv:member:: CvMemBlock* top
20

21
     the current partially allocated memory block in the list of blocks
22

23
  .. ocv:member:: CvMemStorage* parent
24

25
     the parent storage (if any) from which the new memory blocks are borrowed.
26

27
  .. ocv:member:: int free_space
28

29
     number of free bytes in the ``top`` block
30

31
  .. ocv:member:: int block_size
32

33
     the total size of the memory blocks
34

35
Memory storage is a low-level structure used to store dynamically growing data structures such as sequences, contours, graphs, subdivisions, etc. It is organized as a list of memory blocks of equal size -
36
``bottom`` field is the beginning of the list of blocks and ``top`` is the currently used block, but not necessarily the last block of the list. All blocks between ``bottom`` and ``top``, not including the
37
latter, are considered fully occupied; all blocks between ``top`` and the last block, not including  ``top``, are considered free and ``top`` itself is partly occupied - ``free_space`` contains the number of free bytes left in the end of ``top``.
38

39
A new memory buffer that may be allocated explicitly by :ocv:cfunc:`MemStorageAlloc` function or implicitly by higher-level functions, such as :ocv:cfunc:`SeqPush`,  :ocv:cfunc:`GraphAddEdge` etc.
40

41
The buffer is put in the end of already allocated space in the ``top`` memory block, if there is enough free space. After allocation, ``free_space`` is decreased by the size of the allocated buffer plus some padding to keep the proper alignment. When the allocated buffer does not fit into the available portion of
42 43 44
``top``, the next storage block from the list is taken as ``top`` and  ``free_space`` is reset to the whole block size prior to the allocation.

If there are no more free blocks, a new block is allocated (or borrowed from the parent, see :ocv:cfunc:`CreateChildMemStorage`) and added to the end of list. Thus, the storage behaves as a stack with ``bottom`` indicating bottom of the stack and the pair (``top``, ``free_space``)
45
indicating top of the stack. The stack top may be saved via :ocv:cfunc:`SaveMemStoragePos`, restored via
46
:ocv:cfunc:`RestoreMemStoragePos`, or reset via :ocv:cfunc:`ClearMemStorage`.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

CvMemBlock
----------

.. ocv:struct:: CvMemBlock

The structure :ocv:struct:`CvMemBlock` represents a single block of memory storage. The actual data in the memory blocks follows the header.

CvMemStoragePos
---------------

.. ocv:struct:: CvMemStoragePos

The structure stores the position in the memory storage. It is used by :ocv:cfunc:`SaveMemStoragePos` and  :ocv:cfunc:`RestoreMemStoragePos`.

CvSeq
-----

.. ocv:struct:: CvSeq

67
  Dynamically growing sequence.
68

69
  .. ocv:member:: int flags
70

71
     sequence flags, including the sequence signature (CV_SEQ_MAGIC_VAL or CV_SET_MAGIC_VAL), type of the elements and some other information about the sequence.
72

73
  .. ocv:member:: int header_size
74

75
     size of the sequence header. It should be sizeof(CvSeq) at minimum. See :ocv:cfunc:`CreateSeq`.
76

77 78 79 80
  .. ocv:member:: CvSeq* h_prev
  .. ocv:member:: CvSeq* h_next
  .. ocv:member:: CvSeq* v_prev
  .. ocv:member:: CvSeq* v_next
81

82
     pointers to another sequences in a sequence tree. Sequence trees are used to store hierarchical contour structures, retrieved by :ocv:cfunc:`FindContours`
83

84
  .. ocv:member:: int total
85

86
     the number of sequence elements
87

88
  .. ocv:member:: int elem_size
89

90
     size of each sequence element in bytes
91

92
  .. ocv:member:: CvMemStorage* storage
93

94
     memory storage where the sequence resides. It can be a NULL pointer.
95

96
  .. ocv:member:: CvSeqBlock* first
97

98
     pointer to the first data block
99

100
The structure ``CvSeq`` is a base for all of OpenCV dynamic data structures.
101 102 103 104 105 106 107 108 109 110 111 112 113 114
There are two types of sequences - dense and sparse. The base type for dense
sequences is  :ocv:struct:`CvSeq` and such sequences are used to represent
growable 1d arrays - vectors, stacks, queues, and deques. They have no gaps
in the middle - if an element is removed from the middle or inserted
into the middle of the sequence, the elements from the closer end are
shifted. Sparse sequences have  :ocv:struct:`CvSet` as a base class and they are
discussed later in more detail. They are sequences of nodes; each may be either occupied or free as indicated by the node flag. Such sequences are used for unordered data structures such as sets of elements, graphs, hash tables and so forth.


CvSlice
-------

.. ocv:struct:: CvSlice

115
  A sequence slice. In C++ interface the class :ocv:class:`Range` should be used instead.
116

117
  .. ocv:member:: int start_index
118

119
    inclusive start index of the sequence slice
120

121
  .. ocv:member:: int end_index
122

123
    exclusive end index of the sequence slice
124

125 126 127 128
There are helper functions to construct the slice and to compute its length:

.. ocv:cfunction:: CvSlice cvSlice( int start, int end )

129 130 131 132
    :param start: Inclusive left boundary.

    :param end: Exclusive right boundary.

133
::
134 135 136 137

    #define CV_WHOLE_SEQ_END_INDEX 0x3fffffff
    #define CV_WHOLE_SEQ  cvSlice(0, CV_WHOLE_SEQ_END_INDEX)

138 139
.. ocv:cfunction:: int cvSliceLength( CvSlice slice, const CvSeq* seq )

140 141 142 143 144
    :param slice: The slice of sequence.

    :param seq: Source sequence.

Calculates the sequence slice length.
145

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
Some of functions that operate on sequences take a ``CvSlice slice`` parameter that is often set to the whole sequence (CV_WHOLE_SEQ) by default. Either of the ``start_index`` and  ``end_index`` may be negative or exceed the sequence length. If they are equal, the slice is considered empty (i.e., contains no elements). Because sequences are treated as circular structures, the slice may select a
few elements in the end of a sequence followed by a few elements at the beginning of the sequence. For example,  ``cvSlice(-2, 3)`` in the case of a 10-element sequence will select a 5-element slice, containing the pre-last (8th), last (9th), the very first (0th), second (1th) and third (2nd)
elements. The functions normalize the slice argument in the following way:

  #. :ocv:cfunc:`SliceLength` is called to determine the length of the slice,
  #. ``start_index`` of the slice is normalized similarly to the argument of :ocv:cfunc:`GetSeqElem` (i.e., negative indices are allowed). The actual slice to process starts at the normalized  ``start_index`` and lasts :ocv:cfunc:`SliceLength` elements (again, assuming the sequence is a circular structure).

If a function does not accept a slice argument, but you want to process only a part of the sequence, the sub-sequence may be extracted using the :ocv:cfunc:`SeqSlice` function, or stored into a continuous
buffer with :ocv:cfunc:`CvtSeqToArray` (optionally, followed by :ocv:cfunc:`MakeSeqHeaderForArray`).

CvSet
-----

.. ocv:struct:: CvSet

The structure ``CvSet`` is a base for OpenCV 1.x sparse data structures. It is derived from  :ocv:struct:`CvSeq` and includes an additional member ``free_elems`` - a list of free nodes. Every node of the set, whether free or not, is an element of the underlying sequence. While there are no restrictions on elements of dense sequences, the set (and derived structures) elements must start with an integer field and be able to fit CvSetElem structure, because these two fields (an integer followed by a pointer) are required for the organization of a node set with the list of free nodes. If a node is free, the ``flags``
field is negative (the most-significant bit, or MSB, of the field is set), and the ``next_free`` points to the next free node (the first free node is referenced by the ``free_elems`` field of :ocv:struct:`CvSet`). And if a node is occupied, the ``flags`` field is positive and contains the node index that may be retrieved using the (``set_elem->flags & CV_SET_ELEM_IDX_MASK``) expressions, the rest of the node content is determined by the user. In particular, the occupied nodes are not linked as the free nodes are, so the second field can be used for such a link as well as for some different purpose. The macro ``CV_IS_SET_ELEM(set_elem_ptr)`` can be used to determined whether the specified node is occupied or not.

Initially the set and the free node list are empty. When a new node is requested from the set, it is taken from the list of free nodes, which is then updated. If the list appears to be empty, a new sequence block is allocated and all the nodes within the block are joined in the list of free nodes. Thus, the ``total``
field of the set is the total number of nodes both occupied and free. When an occupied node is released, it is added to the list of free nodes. The node released last will be occupied first.

``CvSet`` is used to represent graphs (:ocv:struct:`CvGraph`), sparse multi-dimensional arrays (:ocv:struct:`CvSparseMat`), and planar subdivisions (:ocv:struct:`CvSubdiv2D`).

169 170 171 172 173 174
CvSetElem
---------

.. ocv:struct:: CvSetElem

The structure is represent single element of :ocv:struct:`CvSet`. It consists of two fields: element data pointer and flags.
175 176 177 178 179

CvGraph
-------
.. ocv:struct:: CvGraph

180
The structure ``CvGraph`` is a base for graphs used in OpenCV 1.x. It inherits from
181 182
:ocv:struct:`CvSet`, that is, it is considered as a set of vertices. Besides, it contains another set as a member, a set of graph edges. Graphs in OpenCV are represented using adjacency lists format.

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
CvGraphVtx
----------
.. ocv:struct:: CvGraphVtx

The structure represents single vertex in :ocv:struct:`CvGraph`. It consists of two filds: pointer to first edge and flags.

CvGraphEdge
-----------
.. ocv:struct:: CvGraphEdge

The structure represents edge in :ocv:struct:`CvGraph`.  Each edge consists of:

- Two pointers to the starting and ending vertices (vtx[0] and vtx[1] respectively);
- Two pointers to next edges for the starting and ending vertices, where
  next[0] points to the next edge in the vtx[0] adjacency list and
  next[1] points to the next edge in the vtx[1] adjacency list;
- Weight;
- Flags.
201 202 203 204 205 206 207 208 209 210 211 212

CvGraphScanner
--------------

.. ocv:struct:: CvGraphScanner

The structure ``CvGraphScanner`` is used for depth-first graph traversal. See discussion of the functions below.


CvTreeNodeIterator
------------------

213 214
.. ocv:struct:: CvTreeNodeIterator

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
The structure ``CvTreeNodeIterator`` is used to traverse trees of sequences.

ClearGraph
----------
Clears a graph.

.. ocv:cfunction:: void cvClearGraph( CvGraph* graph )

    :param graph: Graph

The function removes all vertices and edges from a graph. The function has O(1) time complexity.

ClearMemStorage
---------------
Clears memory storage.

.. ocv:cfunction:: void cvClearMemStorage( CvMemStorage* storage )

    :param storage: Memory storage

The function resets the top (free space boundary) of the storage to the very beginning. This function does not deallocate any memory. If the storage has a parent, the function returns
all blocks to the parent.

ClearSeq
--------
Clears a sequence.

.. ocv:cfunction:: void cvClearSeq( CvSeq* seq )

    :param seq: Sequence

The function removes all elements from a sequence. The function does not return the memory to the storage block, but this memory is reused later when new elements are added to the sequence. The function has
'O(1)' time complexity.

.. note:: It is impossible to deallocate a sequence, i.e. free space in the memory storage occupied by the sequence. Instead, call :ocv:cfunc:`ClearMemStorage` or :ocv:cfunc:`ReleaseMemStorage` from time to time somewhere in a top-level processing loop.

ClearSet
--------
Clears a set.

255
.. ocv:cfunction:: void cvClearSet( CvSet* set_header )
256

257
    :param set_header: Cleared set
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

The function removes all elements from set. It has O(1) time complexity.

CloneGraph
----------
Clones a graph.

.. ocv:cfunction:: CvGraph* cvCloneGraph(  const CvGraph* graph, CvMemStorage* storage )

    :param graph: The graph to copy

    :param storage: Container for the copy

The function creates a full copy of the specified graph. If the
graph vertices or edges have pointers to some external data, it can still be
shared between the copies. The vertex and edge indices in the new graph
may be different from the original because the function defragments
the vertex and edge sets.

CloneSeq
--------
Creates a copy of a sequence.

.. ocv:cfunction:: CvSeq* cvCloneSeq(  const CvSeq* seq, CvMemStorage* storage=NULL )

    :param seq: Sequence

    :param storage: The destination storage block to hold the new sequence header and the copied data, if any. If it is NULL, the function uses the storage block containing the input sequence.

The function makes a complete copy of the input sequence and returns it.

The call ``cvCloneSeq( seq, storage )`` is equivalent to ``cvSeqSlice( seq, CV_WHOLE_SEQ, storage, 1 )``.


CreateChildMemStorage
---------------------
Creates child memory storage.

.. ocv:cfunction:: CvMemStorage* cvCreateChildMemStorage(CvMemStorage* parent)

    :param parent: Parent memory storage

The function creates a child memory
storage that is similar to simple memory storage except for the
differences in the memory allocation/deallocation mechanism. When a
child storage needs a new block to add to the block list, it tries
to get this block from the parent. The first unoccupied parent block
available is taken and excluded from the parent block list. If no blocks
are available, the parent either allocates a block or borrows one from
its own parent, if any. In other words, the chain, or a more complex
structure, of memory storages where every storage is a child/parent of
another is possible. When a child storage is released or even cleared,
it returns all blocks to the parent. In other aspects, child storage
is the same as simple storage.

Child storage is useful in the following situation. Imagine
that the user needs to process dynamic data residing in a given storage area and
put the result back to that same storage area. With the simplest approach,
when temporary data is resided in the same storage area as the input and
output data, the storage area will look as follows after processing:

Dynamic data processing without using child storage

.. image:: pics/memstorage1.png

That is, garbage appears in the middle of the storage. However, if
one creates a child memory storage at the beginning of processing,
writes temporary data there, and releases the child storage at the end,
no garbage will appear in the source/destination storage:

Dynamic data processing using a child storage

.. image:: pics/memstorage2.png

CreateGraph
-----------
Creates an empty graph.

.. ocv:cfunction:: CvGraph* cvCreateGraph(  int graph_flags, int header_size, int vtx_size, int edge_size, CvMemStorage* storage )


    :param graph_flags: Type of the created graph. Usually, it is either  ``CV_SEQ_KIND_GRAPH``  for generic unoriented graphs and ``CV_SEQ_KIND_GRAPH | CV_GRAPH_FLAG_ORIENTED``  for generic oriented graphs.

    :param header_size: Graph header size; may not be less than  ``sizeof(CvGraph)``

    :param vtx_size: Graph vertex size; the custom vertex structure must start with  :ocv:struct:`CvGraphVtx`  (use  ``CV_GRAPH_VERTEX_FIELDS()`` )

    :param edge_size: Graph edge size; the custom edge structure must start with  :ocv:struct:`CvGraphEdge`  (use  ``CV_GRAPH_EDGE_FIELDS()`` )

    :param storage: The graph container

The function creates an empty graph and returns a pointer to it.

CreateGraphScanner
------------------
Creates structure for depth-first graph traversal.

.. ocv:cfunction:: CvGraphScanner*  cvCreateGraphScanner(  CvGraph* graph, CvGraphVtx* vtx=NULL, int mask=CV_GRAPH_ALL_ITEMS )
356

357 358 359 360 361 362 363

    :param graph: Graph

    :param vtx: Initial vertex to start from. If NULL, the traversal starts from the first vertex (a vertex with the minimal index in the sequence of vertices).

    :param mask: Event mask indicating which events are of interest to the user (where  :ocv:cfunc:`NextGraphItem`  function returns control to the user) It can be  ``CV_GRAPH_ALL_ITEMS``  (all events are of interest) or a combination of the following flags:

364 365 366 367 368 369
            * **CV_GRAPH_VERTEX** stop at the graph vertices visited for the first time

            * **CV_GRAPH_TREE_EDGE** stop at tree edges ( ``tree edge``  is the edge connecting the last visited vertex and the vertex to be visited next)

            * **CV_GRAPH_BACK_EDGE** stop at back edges ( ``back edge``  is an edge connecting the last visited vertex with some of its ancestors in the search tree)

370
            * **CV_GRAPH_FORWARD_EDGE** stop at forward edges ( ``forward edge``  is an edge connecting the last visited vertex with some of its descendants in the search tree. The forward edges are only possible during oriented graph traversal)
371 372 373 374 375 376 377

            * **CV_GRAPH_CROSS_EDGE** stop at cross edges ( ``cross edge``  is an edge connecting different search trees or branches of the same tree. The  ``cross edges``  are only possible during oriented graph traversal)

            * **CV_GRAPH_ANY_EDGE** stop at any edge ( ``tree, back, forward`` , and  ``cross edges`` )

            * **CV_GRAPH_NEW_TREE** stop in the beginning of every new search tree. When the traversal procedure visits all vertices and edges reachable from the initial vertex (the visited vertices together with tree edges make up a tree), it searches for some unvisited vertex in the graph and resumes the traversal process from that vertex. Before starting a new tree (including the very first tree when  ``cvNextGraphItem``  is called for the first time) it generates a  ``CV_GRAPH_NEW_TREE``  event. For unoriented graphs, each search tree corresponds to a connected component of the graph.

378 379
            * **CV_GRAPH_BACKTRACKING** stop at every already visited vertex during backtracking - returning to already visited vertexes of the traversal tree.

380
The function creates a structure for depth-first graph traversal/search. The initialized structure is used in the
381 382 383 384 385 386 387
:ocv:cfunc:`NextGraphItem`
function - the incremental traversal procedure.

CreateMemStorage
----------------
Creates memory storage.

388 389 390
.. ocv:cfunction:: CvMemStorage* cvCreateMemStorage( int block_size=0 )

    :param block_size: Size of the storage blocks in bytes. If it is 0, the block size is set to a default value - currently it is  about 64K.
391

392
The function creates an empty memory storage. See
393 394 395 396 397 398 399
:ocv:struct:`CvMemStorage`
description.

CreateSeq
---------
Creates a sequence.

400
.. ocv:cfunction:: CvSeq* cvCreateSeq( int seq_flags, size_t header_size, size_t elem_size, CvMemStorage* storage )
401

402

403
    :param seq_flags: Flags of the created sequence. If the sequence is not passed to any function working with a specific type of sequences, the sequence value may be set to 0, otherwise the appropriate type must be selected from the list of predefined sequence types.
404

405
    :param header_size: Size of the sequence header; must be greater than or equal to  ``sizeof(CvSeq)`` . If a specific type or its extension is indicated, this type must fit the base type header.
406

407
    :param elem_size: Size of the sequence elements in bytes. The size must be consistent with the sequence type. For example, for a sequence of points to be created, the element type    ``CV_SEQ_ELTYPE_POINT``  should be specified and the parameter  ``elem_size``  must be equal to  ``sizeof(CvPoint)`` .
408 409 410 411 412 413

    :param storage: Sequence location

The function creates a sequence and returns
the pointer to it. The function allocates the sequence header in
the storage block as one continuous chunk and sets the structure
414
fields
415
``flags``
416
,
417
``elemSize``
418
,
419 420 421
``headerSize``
, and
``storage``
422
to passed values, sets
423 424
``delta_elems``
to the
425
default value (that may be reassigned using the
426 427
:ocv:cfunc:`SetSeqBlockSize`
function), and clears other header fields, including the space following
428
the first
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
``sizeof(CvSeq)``
bytes.

CreateSet
---------
Creates an empty set.

.. ocv:cfunction:: CvSet* cvCreateSet(  int set_flags, int header_size, int elem_size, CvMemStorage* storage )

    :param set_flags: Type of the created set

    :param header_size: Set header size; may not be less than  ``sizeof(CvSet)``

    :param elem_size: Set element size; may not be less than  :ocv:struct:`CvSetElem`

    :param storage: Container for the set

446
The function creates an empty set with a specified header size and element size, and returns the pointer to the set. This function is just a thin layer on top of
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
:ocv:cfunc:`CreateSeq`.

CvtSeqToArray
-------------
Copies a sequence to one continuous block of memory.

.. ocv:cfunction:: void* cvCvtSeqToArray(  const CvSeq* seq, void* elements, CvSlice slice=CV_WHOLE_SEQ )

    :param seq: Sequence

    :param elements: Pointer to the destination array that must be large enough. It should be a pointer to data, not a matrix header.

    :param slice: The sequence portion to copy to the array

The function copies the entire sequence or subsequence to the specified buffer and returns the pointer to the buffer.

EndWriteSeq
-----------
Finishes the process of writing a sequence.

.. ocv:cfunction:: CvSeq* cvEndWriteSeq( CvSeqWriter* writer )

    :param writer: Writer state

The function finishes the writing process and
returns the pointer to the written sequence. The function also truncates
the last incomplete sequence block to return the remaining part of the
block to memory storage. After that, the sequence can be read and
475
modified safely. See
476
:ocv:cfunc:`StartWriteSeq`
477
and
478 479 480 481 482 483 484 485 486 487 488 489 490
:ocv:cfunc:`StartAppendToSeq`

FindGraphEdge
-------------
Finds an edge in a graph.

.. ocv:cfunction:: CvGraphEdge* cvFindGraphEdge( const CvGraph* graph, int start_idx, int end_idx )

    :param graph: Graph

    :param start_idx: Index of the starting vertex of the edge

    :param end_idx: Index of the ending vertex of the edge. For an unoriented graph, the order of the vertex parameters does not matter.
491

492 493 494 495 496
::

    #define cvGraphFindEdge cvFindGraphEdge

..
497 498 499 500 501 502 503

The function finds the graph edge connecting two specified vertices and returns a pointer to it or NULL if the edge does not exist.

FindGraphEdgeByPtr
------------------
Finds an edge in a graph by using its pointer.

504
.. ocv:cfunction:: CvGraphEdge* cvFindGraphEdgeByPtr( const CvGraph* graph, const CvGraphVtx* start_vtx, const CvGraphVtx* end_vtx )
505 506 507

    :param graph: Graph

508
    :param start_vtx: Pointer to the starting vertex of the edge
509

510
    :param end_vtx: Pointer to the ending vertex of the edge. For an unoriented graph, the order of the vertex parameters does not matter.
511

512 513 514 515 516
::

    #define cvGraphFindEdgeByPtr cvFindGraphEdgeByPtr

..
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552

The function finds the graph edge connecting two specified vertices and returns pointer to it or NULL if the edge does not exists.

FlushSeqWriter
--------------
Updates sequence headers from the writer.

.. ocv:cfunction:: void cvFlushSeqWriter( CvSeqWriter* writer )

    :param writer: Writer state

The function is intended to enable the user to
read sequence elements, whenever required, during the writing process,
e.g., in order to check specific conditions. The function updates the
sequence headers to make reading from the sequence possible. The writer
is not closed, however, so that the writing process can be continued at
any time. If an algorithm requires frequent flushes, consider using
:ocv:cfunc:`SeqPush`
instead.

GetGraphVtx
-----------
Finds a graph vertex by using its index.

.. ocv:cfunction:: CvGraphVtx* cvGetGraphVtx(  CvGraph* graph, int vtx_idx )

    :param graph: Graph

    :param vtx_idx: Index of the vertex

The function finds the graph vertex by using its index and returns the pointer to it or NULL if the vertex does not belong to the graph.

GetSeqElem
----------
Returns a pointer to a sequence element according to its index.

553
.. ocv:cfunction:: schar* cvGetSeqElem( const CvSeq* seq, int index )
554

555 556 557
    :param seq: Sequence

    :param index: Index of element
558

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
::

    #define CV_GET_SEQ_ELEM( TYPE, seq, index )  (TYPE*)cvGetSeqElem( (CvSeq*)(seq), (index) )

..


The function finds the element with the given
index in the sequence and returns the pointer to it. If the element
is not found, the function returns 0. The function supports negative
indices, where -1 stands for the last sequence element, -2 stands for
the one before last, etc. If the sequence is most likely to consist of
a single sequence block or the desired element is likely to be located
in the first block, then the macro
``CV_GET_SEQ_ELEM( elemType, seq, index )``
574
should be used, where the parameter
575 576
``elemType``
is the
577
type of sequence elements (
578 579 580
:ocv:struct:`CvPoint`
for example), the parameter
``seq``
581
is a sequence, and the parameter
582 583 584 585
``index``
is the index
of the desired element. The macro checks first whether the desired element
belongs to the first block of the sequence and returns it if it does;
586
otherwise the macro calls the main function
587 588
``GetSeqElem``
. Negative
589
indices always cause the
590 591 592 593 594 595 596 597 598 599 600 601 602
:ocv:cfunc:`GetSeqElem`
call. The function has O(1)
time complexity assuming that the number of blocks is much smaller than the
number of elements.

GetSeqReaderPos
---------------
Returns the current reader position.

.. ocv:cfunction:: int cvGetSeqReaderPos( CvSeqReader* reader )

    :param reader: Reader state

603
The function returns the current reader position (within 0 ...
604 605 606 607 608 609 610
``reader->seq->total``
- 1).

GetSetElem
----------
Finds a set element by its index.

611
.. ocv:cfunction:: CvSetElem* cvGetSetElem( const CvSet* set_header, int idx )
612

613
    :param set_header: Set
614

615
    :param idx: Index of the set element within a sequence
616

617
The function finds a set element by its index. The function returns the pointer to it or 0 if the index is invalid or the corresponding node is free. The function supports negative indices as it uses
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
:ocv:cfunc:`GetSeqElem`
to locate the node.

GraphAddEdge
------------
Adds an edge to a graph.

.. ocv:cfunction:: int cvGraphAddEdge(  CvGraph* graph, int start_idx, int end_idx, const CvGraphEdge* edge=NULL, CvGraphEdge** inserted_edge=NULL )

    :param graph: Graph

    :param start_idx: Index of the starting vertex of the edge

    :param end_idx: Index of the ending vertex of the edge. For an unoriented graph, the order of the vertex parameters does not matter.

    :param edge: Optional input parameter, initialization data for the edge

    :param inserted_edge: Optional output parameter to contain the address of the inserted edge

The function connects two specified vertices. The function returns 1 if the edge has been added successfully, 0 if the edge connecting the two vertices exists already and -1 if either of the vertices was not found, the starting and the ending vertex are the same, or there is some other critical situation. In the latter case (i.e., when the result is negative), the function also reports an error by default.

GraphAddEdgeByPtr
-----------------
Adds an edge to a graph by using its pointer.

.. ocv:cfunction:: int cvGraphAddEdgeByPtr(  CvGraph* graph, CvGraphVtx* start_vtx, CvGraphVtx* end_vtx, const CvGraphEdge* edge=NULL, CvGraphEdge** inserted_edge=NULL )

    :param graph: Graph

    :param start_vtx: Pointer to the starting vertex of the edge

    :param end_vtx: Pointer to the ending vertex of the edge. For an unoriented graph, the order of the vertex parameters does not matter.

    :param edge: Optional input parameter, initialization data for the edge

    :param inserted_edge: Optional output parameter to contain the address of the inserted edge within the edge set

The function connects two specified vertices. The
function returns 1 if the edge has been added successfully, 0 if the
edge connecting the two vertices exists already, and -1 if either of the
vertices was not found, the starting and the ending vertex are the same
or there is some other critical situation. In the latter case (i.e., when
the result is negative), the function also reports an error by default.

GraphAddVtx
-----------
Adds a vertex to a graph.

.. ocv:cfunction:: int cvGraphAddVtx(  CvGraph* graph, const CvGraphVtx* vtx=NULL, CvGraphVtx** inserted_vtx=NULL )

    :param graph: Graph

    :param vtx: Optional input argument used to initialize the added vertex (only user-defined fields beyond  ``sizeof(CvGraphVtx)``  are copied)

672
    :param inserted_vtx: Optional output argument. If not  ``NULL`` , the address of the new vertex is written here.
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

The function adds a vertex to the graph and returns the vertex index.

GraphEdgeIdx
------------
Returns the index of a graph edge.

.. ocv:cfunction:: int cvGraphEdgeIdx(  CvGraph* graph, CvGraphEdge* edge )

    :param graph: Graph

    :param edge: Pointer to the graph edge

The function returns the index of a graph edge.

GraphRemoveEdge
---------------
Removes an edge from a graph.

.. ocv:cfunction:: void cvGraphRemoveEdge(  CvGraph* graph, int start_idx, int end_idx )

    :param graph: Graph

    :param start_idx: Index of the starting vertex of the edge

    :param end_idx: Index of the ending vertex of the edge. For an unoriented graph, the order of the vertex parameters does not matter.

The function removes the edge connecting two specified vertices. If the vertices are not connected [in that order], the function does nothing.

GraphRemoveEdgeByPtr
--------------------
Removes an edge from a graph by using its pointer.

.. ocv:cfunction:: void cvGraphRemoveEdgeByPtr(  CvGraph* graph, CvGraphVtx* start_vtx, CvGraphVtx* end_vtx )

    :param graph: Graph

    :param start_vtx: Pointer to the starting vertex of the edge

    :param end_vtx: Pointer to the ending vertex of the edge. For an unoriented graph, the order of the vertex parameters does not matter.

The function removes the edge connecting two specified vertices. If the vertices are not connected [in that order], the function does nothing.

GraphRemoveVtx
--------------
Removes a vertex from a graph.

.. ocv:cfunction:: int cvGraphRemoveVtx(  CvGraph* graph, int index )

    :param graph: Graph

724
    :param index: Index of the removed vertex
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

The function removes a vertex from a graph
together with all the edges incident to it. The function reports an error
if the input vertex does not belong to the graph. The return value is the
number of edges deleted, or -1 if the vertex does not belong to the graph.

GraphRemoveVtxByPtr
-------------------
Removes a vertex from a graph by using its pointer.

.. ocv:cfunction:: int cvGraphRemoveVtxByPtr(  CvGraph* graph, CvGraphVtx* vtx )

    :param graph: Graph

    :param vtx: Pointer to the removed vertex

The function removes a vertex from the graph by using its pointer together with all the edges incident to it. The function reports an error if the vertex does not belong to the graph. The return value is the number of edges deleted, or -1 if the vertex does not belong to the graph.

GraphVtxDegree
--------------
745
Counts the number of edges incident to the vertex.
746

747
.. ocv:cfunction:: int cvGraphVtxDegree( const CvGraph* graph, int vtx_idx )
748 749 750

    :param graph: Graph

751
    :param vtx_idx: Index of the graph vertex
752 753 754 755 756 757 758 759 760 761 762 763 764 765

The function returns the number of edges incident to the specified vertex, both incoming and outgoing. To count the edges, the following code is used:

::

    CvGraphEdge* edge = vertex->first; int count = 0;
    while( edge )
    {
        edge = CV_NEXT_GRAPH_EDGE( edge, vertex );
        count++;
    }

..

766
The macro
767
``CV_NEXT_GRAPH_EDGE( edge, vertex )``
768
returns the edge incident to
769
``vertex``
770
that follows after
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
``edge``
.

GraphVtxDegreeByPtr
-------------------
Finds an edge in a graph.

.. ocv:cfunction:: int cvGraphVtxDegreeByPtr(  const CvGraph* graph, const CvGraphVtx* vtx )

    :param graph: Graph

    :param vtx: Pointer to the graph vertex

The function returns the number of edges incident to the specified vertex, both incoming and outcoming.

GraphVtxIdx
-----------
Returns the index of a graph vertex.

.. ocv:cfunction:: int cvGraphVtxIdx(  CvGraph* graph, CvGraphVtx* vtx )

    :param graph: Graph

    :param vtx: Pointer to the graph vertex

The function returns the index of a graph vertex.

InitTreeNodeIterator
--------------------
Initializes the tree node iterator.

.. ocv:cfunction:: void cvInitTreeNodeIterator(  CvTreeNodeIterator* tree_iterator, const void* first, int max_level )

    :param tree_iterator: Tree iterator initialized by the function

    :param first: The initial node to start traversing from

    :param max_level: The maximal level of the tree ( ``first``  node assumed to be at the first level) to traverse up to. For example, 1 means that only nodes at the same level as  ``first``  should be visited, 2 means that the nodes on the same level as  ``first``  and their direct children should be visited, and so forth.

The function initializes the tree iterator. The tree is traversed in depth-first order.

InsertNodeIntoTree
------------------
Adds a new node to a tree.

.. ocv:cfunction:: void cvInsertNodeIntoTree(  void* node, void* parent, void* frame )

    :param node: The inserted node

    :param parent: The parent node that is already in the tree

    :param frame: The top level node. If  ``parent``  and  ``frame``  are the same, the  ``v_prev``  field of  ``node``  is set to NULL rather than  ``parent`` .

The function adds another node into tree. The function does not allocate any memory, it can only modify links of the tree nodes.

MakeSeqHeaderForArray
---------------------
Constructs a sequence header for an array.

.. ocv:cfunction:: CvSeq* cvMakeSeqHeaderForArray(  int seq_type, int header_size, int elem_size, void* elements, int total, CvSeq* seq, CvSeqBlock* block )

    :param seq_type: Type of the created sequence

    :param header_size: Size of the header of the sequence. Parameter sequence must point to the structure of that size or greater

    :param elem_size: Size of the sequence elements

    :param elements: Elements that will form a sequence

    :param total: Total number of elements in the sequence. The number of array elements must be equal to the value of this parameter.

    :param seq: Pointer to the local variable that is used as the sequence header

    :param block: Pointer to the local variable that is the header of the single sequence block

The function initializes a sequence
header for an array. The sequence header as well as the sequence block are
allocated by the user (for example, on stack). No data is copied by the
function. The resultant sequence will consists of a single block and
have NULL storage pointer; thus, it is possible to read its elements,
but the attempts to add elements to the sequence will raise an error in
most cases.

MemStorageAlloc
---------------
Allocates a memory buffer in a storage block.

.. ocv:cfunction:: void* cvMemStorageAlloc(  CvMemStorage* storage, size_t size )

    :param storage: Memory storage

    :param size: Buffer size

The function allocates a memory buffer in
a storage block. The buffer size must not exceed the storage block size,
otherwise a runtime error is raised. The buffer address is aligned by
``CV_STRUCT_ALIGN=sizeof(double)``
(for the moment) bytes.

MemStorageAllocString
---------------------
Allocates a text string in a storage block.

.. ocv:cfunction:: CvString cvMemStorageAllocString(CvMemStorage* storage, const char* ptr, int len=-1)

876 877 878 879 880
    :param storage: Memory storage

    :param ptr: The string

    :param len: Length of the string (not counting the ending  ``NUL`` ) . If the parameter is negative, the function computes the length.
881

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
::

    typedef struct CvString
    {
        int len;
        char* ptr;
    }
    CvString;

..

The function creates copy of the string
in memory storage. It returns the structure that contains user-passed
or computed length of the string and pointer to the copied string.

NextGraphItem
-------------
Executes one or more steps of the graph traversal procedure.

.. ocv:cfunction:: int cvNextGraphItem( CvGraphScanner* scanner )

    :param scanner: Graph traversal state. It is updated by this function.

The function traverses through the graph
until an event of interest to the user (that is, an event, specified
907
in the
908
``mask``
909
in the
910 911 912
:ocv:cfunc:`CreateGraphScanner`
call) is met or the
traversal is completed. In the first case, it returns one of the events
913
listed in the description of the
914 915 916 917
``mask``
parameter above and with
the next call it resumes the traversal. In the latter case, it returns
``CV_GRAPH_OVER``
918
(-1). When the event is
919 920 921
``CV_GRAPH_VERTEX``
,
``CV_GRAPH_BACKTRACKING``
922
, or
923 924
``CV_GRAPH_NEW_TREE``
,
925
the currently observed vertex is stored in
926 927
``scanner-:math:`>`vtx``
. And if the
928
event is edge-related, the edge itself is stored at
929 930
``scanner-:math:`>`edge``
,
931
the previously visited vertex - at
932 933
``scanner-:math:`>`vtx``
and the other ending
934
vertex of the edge - at
935 936 937 938 939 940 941 942 943 944 945 946 947
``scanner-:math:`>`dst``
.

NextTreeNode
------------
Returns the currently observed node and moves the iterator toward the next node.

.. ocv:cfunction:: void* cvNextTreeNode( CvTreeNodeIterator* tree_iterator )

    :param tree_iterator: Tree iterator initialized by the function

The function returns the currently observed node and then updates the
iterator - moving it toward the next node. In other words, the function
948
behavior is similar to the
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
``*p++``
expression on a typical C
pointer or C++ collection iterator. The function returns NULL if there
are no more nodes.

PrevTreeNode
------------
Returns the currently observed node and moves the iterator toward the previous node.

.. ocv:cfunction:: void* cvPrevTreeNode( CvTreeNodeIterator* tree_iterator )

    :param tree_iterator: Tree iterator initialized by the function

The function returns the currently observed node and then updates
the iterator - moving it toward the previous node. In other words,
964
the function behavior is similar to the
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
``*p--``
expression on a
typical C pointer or C++ collection iterator. The function returns NULL
if there are no more nodes.

ReleaseGraphScanner
-------------------
Completes the graph traversal procedure.

.. ocv:cfunction:: void cvReleaseGraphScanner( CvGraphScanner** scanner )

    :param scanner: Double pointer to graph traverser

The function completes the graph traversal procedure and releases the traverser state.

ReleaseMemStorage
-----------------
Releases memory storage.

.. ocv:cfunction:: void cvReleaseMemStorage( CvMemStorage** storage )

    :param storage: Pointer to the released storage

The function deallocates all storage memory
blocks or returns them to the parent, if any. Then it deallocates the
990 991
storage header and clears the pointer to the storage. All child storage
associated with a given parent storage block must be released before the
992 993 994 995 996 997 998 999 1000 1001 1002 1003
parent storage block is released.

RestoreMemStoragePos
--------------------
Restores memory storage position.

.. ocv:cfunction:: void cvRestoreMemStoragePos( CvMemStorage* storage, CvMemStoragePos* pos)

    :param storage: Memory storage

    :param pos: New storage top position

1004
The function restores the position of the storage top from the parameter
1005
``pos``
1006
. This function and the function
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
``cvClearMemStorage``
are the only methods to release memory occupied in memory blocks. Note again that there is no way to free memory in the middle of an occupied portion of a storage block.

SaveMemStoragePos
-----------------
Saves memory storage position.

.. ocv:cfunction:: void cvSaveMemStoragePos( const CvMemStorage* storage, CvMemStoragePos* pos)

    :param storage: Memory storage

    :param pos: The output position of the storage top

The function saves the current position
1021
of the storage top to the parameter
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
``pos``
. The function
``cvRestoreMemStoragePos``
can further retrieve this position.

SeqElemIdx
----------
Returns the index of a specific sequence element.

.. ocv:cfunction:: int cvSeqElemIdx(  const CvSeq* seq, const void* element, CvSeqBlock** block=NULL )

    :param seq: Sequence

    :param element: Pointer to the element within the sequence

    :param block: Optional argument. If the pointer is not  ``NULL`` , the address of the sequence block that contains the element is stored in this location.

The function returns the index of a sequence element or a negative number if the element is not found.

SeqInsert
---------
Inserts an element in the middle of a sequence.

1045
.. ocv:cfunction:: schar* cvSeqInsert( CvSeq* seq, int before_index, const void* element=NULL )
1046 1047 1048

    :param seq: Sequence

1049
    :param before_index: Index before which the element is inserted. Inserting before 0 (the minimal allowed value of the parameter) is equal to  :ocv:cfunc:`SeqPushFront`  and inserting before  ``seq->total``  (the maximal allowed value of the parameter) is equal to  :ocv:cfunc:`SeqPush` .
1050 1051 1052

    :param element: Inserted element

1053
The function shifts the sequence elements from the inserted position to the nearest end of the sequence and copies the
1054 1055 1056 1057 1058 1059 1060
``element``
content there if the pointer is not NULL. The function returns a pointer to the inserted element.

SeqInsertSlice
--------------
Inserts an array in the middle of a sequence.

1061
.. ocv:cfunction:: void cvSeqInsertSlice( CvSeq* seq, int before_index, const CvArr* from_arr )
1062

1063 1064
    :param seq: Sequence

1065
    :param before_index: Index before which the array is inserted
1066

1067
    :param from_arr: The array to take elements from
1068

1069
The function inserts all
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
``fromArr``
array elements at the specified position of the sequence. The array
``fromArr``
can be a matrix or another sequence.

SeqInvert
---------
Reverses the order of sequence elements.

.. ocv:cfunction:: void cvSeqInvert( CvSeq* seq )
1080 1081 1082

    :param seq: Sequence

1083 1084 1085 1086 1087 1088 1089
The function reverses the sequence in-place - the first element becomes the last one, the last element becomes the first one and so forth.

SeqPop
------
Removes an element from the end of a sequence.

.. ocv:cfunction:: void cvSeqPop(  CvSeq* seq, void* element=NULL )
1090 1091 1092 1093 1094

    :param seq: Sequence

    :param element: Optional parameter . If the pointer is not zero, the function copies the removed element to this location.

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
The function removes an element from a sequence. The function reports an error if the sequence is already empty. The function has O(1) complexity.

SeqPopFront
-----------
Removes an element from the beginning of a sequence.

.. ocv:cfunction:: void cvSeqPopFront(   CvSeq* seq, void* element=NULL )

    :param seq: Sequence

    :param element: Optional parameter. If the pointer is not zero, the function copies the removed element to this location.

The function removes an element from the beginning of a sequence. The function reports an error if the sequence is already empty. The function has O(1) complexity.

SeqPopMulti
-----------
Removes several elements from either end of a sequence.

.. ocv:cfunction:: void cvSeqPopMulti(  CvSeq* seq, void* elements, int count, int in_front=0 )

    :param seq: Sequence

    :param elements: Removed elements

    :param count: Number of elements to pop

1121 1122 1123 1124
    :param in_front: The flags specifying which end of the modified sequence.

            * **CV_BACK** the elements are added to the end of the sequence

1125 1126 1127 1128 1129 1130 1131 1132
            * **CV_FRONT** the elements are added to the beginning of the sequence

The function removes several elements from either end of the sequence. If the number of the elements to be removed exceeds the total number of elements in the sequence, the function removes as many elements as possible.

SeqPush
-------
Adds an element to the end of a sequence.

1133
.. ocv:cfunction:: schar* cvSeqPush( CvSeq* seq, const void* element=NULL )
1134 1135 1136 1137 1138

    :param seq: Sequence

    :param element: Added element

1139
The function adds an element to the end of a sequence and returns a pointer to the allocated element. If the input
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
``element``
is NULL, the function simply allocates a space for one more element.

The following code demonstrates how to create a new sequence using this function:

::

    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* seq = cvCreateSeq( CV_32SC1, /* sequence of integer elements */
                              sizeof(CvSeq), /* header size - no extra fields */
                              sizeof(int), /* element size */
                              storage /* the container storage */ );
    int i;
    for( i = 0; i < 100; i++ )
    {
        int* added = (int*)cvSeqPush( seq, &i );
        printf( "
    }
1158

1159 1160 1161 1162 1163 1164
    ...
    /* release memory storage in the end */
    cvReleaseMemStorage( &storage );

..

1165
The function has O(1) complexity, but there is a faster method for writing large sequences (see
1166 1167 1168 1169 1170 1171 1172
:ocv:cfunc:`StartWriteSeq`
and related functions).

SeqPushFront
------------
Adds an element to the beginning of a sequence.

1173
.. ocv:cfunction:: schar* cvSeqPushFront( CvSeq* seq, const void* element=NULL )
1174 1175 1176 1177 1178

    :param seq: Sequence

    :param element: Added element

1179
The function is similar to
1180 1181 1182 1183 1184 1185 1186
:ocv:cfunc:`SeqPush`
but it adds the new element to the beginning of the sequence. The function has O(1) complexity.

SeqPushMulti
------------
Pushes several elements to either end of a sequence.

1187
.. ocv:cfunction:: void cvSeqPushMulti( CvSeq* seq, const void* elements, int count, int in_front=0 )
1188 1189 1190 1191 1192 1193 1194

    :param seq: Sequence

    :param elements: Added elements

    :param count: Number of elements to push

1195 1196 1197 1198
    :param in_front: The flags specifying which end of the modified sequence.

            * **CV_BACK** the elements are added to the end of the sequence

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
            * **CV_FRONT** the elements are added to the beginning of the sequence

The function adds several elements to either
end of a sequence. The elements are added to the sequence in the same
order as they are arranged in the input array but they can fall into
different sequence blocks.

SeqRemove
---------
Removes an element from the middle of a sequence.

.. ocv:cfunction:: void cvSeqRemove(  CvSeq* seq, int index )

    :param seq: Sequence

    :param index: Index of removed element

The function removes elements with the given
index. If the index is out of range the function reports an error. An
attempt to remove an element from an empty sequence is a special
case of this situation. The function removes an element by shifting
the sequence elements between the nearest end of the sequence and the
``index``
-th position, not counting the latter.

SeqRemoveSlice
--------------
Removes a sequence slice.

.. ocv:cfunction:: void cvSeqRemoveSlice( CvSeq* seq, CvSlice slice )

    :param seq: Sequence

    :param slice: The part of the sequence to remove

The function removes a slice from the sequence.

SeqSearch
---------
Searches for an element in a sequence.

1240
.. ocv:cfunction:: schar* cvSeqSearch( CvSeq* seq, const void* elem, CvCmpFunc func, int is_sorted, int* elem_idx, void* userdata=NULL )
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

    :param seq: The sequence

    :param elem: The element to look for

    :param func: The comparison function that returns negative, zero or positive value depending on the relationships among the elements (see also  :ocv:cfunc:`SeqSort` )

    :param is_sorted: Whether the sequence is sorted or not

    :param elem_idx: Output parameter; index of the found element

1252
    :param userdata: The user parameter passed to the comparison function; helps to avoid global variables in some cases
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290

::

    /* a < b ? -1 : a > b ? 1 : 0 */
    typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata);

..

The function searches for the element in the sequence. If
the sequence is sorted, a binary O(log(N)) search is used; otherwise, a
simple linear search is used. If the element is not found, the function
returns a NULL pointer and the index is set to the number of sequence
elements if a linear search is used, or to the smallest index
``i, seq(i)>elem``
.

SeqSlice
--------
Makes a separate header for a sequence slice.

.. ocv:cfunction:: CvSeq* cvSeqSlice(  const CvSeq* seq, CvSlice slice, CvMemStorage* storage=NULL, int copy_data=0 )

    :param seq: Sequence

    :param slice: The part of the sequence to be extracted

    :param storage: The destination storage block to hold the new sequence header and the copied data, if any. If it is NULL, the function uses the storage block containing the input sequence.

    :param copy_data: The flag that indicates whether to copy the elements of the extracted slice ( ``copy_data!=0`` ) or not ( ``copy_data=0`` )

The function creates a sequence that represents the specified slice of the input sequence. The new sequence either shares the elements with the original sequence or has its own copy of the elements. So if one needs to process a part of sequence but the processing function does not have a slice parameter, the required sub-sequence may be extracted using this function.

SeqSort
-------
Sorts sequence element using the specified comparison function.

.. ocv:cfunction:: void cvSeqSort( CvSeq* seq, CvCmpFunc func, void* userdata=NULL )

1291 1292 1293 1294
    :param seq: The sequence to sort

    :param func: The comparison function that returns a negative, zero, or positive value depending on the relationships among the elements (see the above declaration and the example below) - a similar function is used by  ``qsort``  from C runline except that in the latter,  ``userdata``  is not used

1295
    :param userdata: The user parameter passed to the comparison function; helps to avoid global variables in some cases
1296

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
::

    /* a < b ? -1 : a > b ? 1 : 0 */
    typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata);

..

The function sorts the sequence in-place using the specified criteria. Below is an example of using this function:

::

    /* Sort 2d points in top-to-bottom left-to-right order */
    static int cmp_func( const void* _a, const void* _b, void* userdata )
    {
        CvPoint* a = (CvPoint*)_a;
        CvPoint* b = (CvPoint*)_b;
        int y_diff = a->y - b->y;
        int x_diff = a->x - b->x;
        return y_diff ? y_diff : x_diff;
    }
1317

1318
    ...
1319

1320 1321 1322
    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* seq = cvCreateSeq( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage );
    int i;
1323

1324 1325 1326
    for( i = 0; i < 10; i++ )
    {
        CvPoint pt;
1327 1328
        pt.x = rand()
        pt.y = rand()
1329 1330
        cvSeqPush( seq, &pt );
    }
1331

1332
    cvSeqSort( seq, cmp_func, 0 /* userdata is not used here */ );
1333

1334 1335 1336 1337 1338 1339
    /* print out the sorted sequence */
    for( i = 0; i < seq->total; i++ )
    {
        CvPoint* pt = (CvPoint*)cvSeqElem( seq, i );
        printf( "(
    }
1340

1341 1342 1343 1344 1345 1346 1347 1348
    cvReleaseMemStorage( &storage );

..

SetAdd
------
Occupies a node in the set.

1349
.. ocv:cfunction:: int cvSetAdd( CvSet* set_header, CvSetElem* elem=NULL, CvSetElem** inserted_elem=NULL )
1350

1351
    :param set_header: Set
1352 1353 1354 1355 1356 1357 1358

    :param elem: Optional input argument, an inserted element. If not NULL, the function copies the data to the allocated node (the MSB of the first integer field is cleared after copying).

    :param inserted_elem: Optional output argument; the pointer to the allocated cell

The function allocates a new node, optionally copies
input element data to it, and returns the pointer and the index to the
1359
node. The index value is taken from the lower bits of the
1360 1361
``flags``
field of the node. The function has O(1) complexity; however, there exists
1362
a faster function for allocating set nodes (see
1363 1364 1365 1366 1367 1368 1369
:ocv:cfunc:`SetNew`
).

SetNew
------
Adds an element to a set (fast variant).

1370
.. ocv:cfunction:: CvSetElem* cvSetNew( CvSet* set_header )
1371

1372
    :param set_header: Set
1373

1374
The function is an inline lightweight variant of
1375 1376 1377 1378 1379 1380 1381
:ocv:cfunc:`SetAdd`
. It occupies a new node and returns a pointer to it rather than an index.

SetRemove
---------
Removes an element from a set.

1382
.. ocv:cfunction:: void cvSetRemove( CvSet* set_header, int index )
1383

1384
    :param set_header: Set
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398

    :param index: Index of the removed element

The function removes an element with a specified
index from the set. If the node at the specified location is not occupied,
the function does nothing. The function has O(1) complexity; however,
:ocv:cfunc:`SetRemoveByPtr`
provides a quicker way to remove a set element
if it is located already.

SetRemoveByPtr
--------------
Removes a set element based on its pointer.

1399
.. ocv:cfunction:: void cvSetRemoveByPtr( CvSet* set_header, void* elem )
1400

1401
    :param set_header: Set
1402 1403 1404

    :param elem: Removed element

1405
The function is an inline lightweight variant of
1406 1407 1408 1409 1410 1411 1412
:ocv:cfunc:`SetRemove`
that requires an element pointer. The function does not check whether the node is occupied or not - the user should take care of that.

SetSeqBlockSize
---------------
Sets up sequence block size.

1413
.. ocv:cfunction:: void cvSetSeqBlockSize( CvSeq* seq, int delta_elems )
1414 1415 1416

    :param seq: Sequence

1417
    :param delta_elems: Desirable sequence block size for elements
1418 1419 1420

The function affects memory allocation
granularity. When the free space in the sequence buffers has run out,
1421
the function allocates the space for
1422
``delta_elems``
1423 1424 1425 1426 1427
sequence
elements. If this block immediately follows the one previously allocated,
the two blocks are concatenated; otherwise, a new sequence block is
created. Therefore, the bigger the parameter is, the lower the possible
sequence fragmentation, but the more space in the storage block is wasted. When
1428
the sequence is created, the parameter
1429
``delta_elems``
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
is set to
the default value of about 1K. The function can be called any time after
the sequence is created and affects future allocations. The function
can modify the passed value of the parameter to meet memory storage
constraints.

SetSeqReaderPos
---------------
Moves the reader to the specified position.

.. ocv:cfunction:: void cvSetSeqReaderPos(  CvSeqReader* reader, int index, int is_relative=0 )

    :param reader: Reader state

    :param index: The destination position. If the positioning mode is used (see the next parameter), the actual position will be  ``index``  mod  ``reader->seq->total`` .

    :param is_relative: If it is not zero, then  ``index``  is a relative to the current position

The function moves the read position to an absolute position or relative to the current position.

StartAppendToSeq
----------------
Initializes the process of writing data to a sequence.

.. ocv:cfunction:: void cvStartAppendToSeq(  CvSeq* seq, CvSeqWriter* writer )

    :param seq: Pointer to the sequence

    :param writer: Writer state; initialized by the function

The function initializes the process of
writing data to a sequence. Written elements are added to the end of the
sequence by using the
``CV_WRITE_SEQ_ELEM( written_elem, writer )``
macro. Note
that during the writing process, other operations on the sequence may
yield an incorrect result or even corrupt the sequence (see description of
:ocv:cfunc:`FlushSeqWriter`
, which helps to avoid some of these problems).

StartReadSeq
------------
Initializes the process of sequential reading from a sequence.

.. ocv:cfunction:: void cvStartReadSeq(  const CvSeq* seq, CvSeqReader* reader, int reverse=0 )

    :param seq: Sequence

    :param reader: Reader state; initialized by the function

    :param reverse: Determines the direction of the sequence traversal. If  ``reverse``  is 0, the reader is positioned at the first sequence element; otherwise it is positioned at the last element.

The function initializes the reader state. After
that, all the sequence elements from the first one down to the last one
can be read by subsequent calls of the macro
``CV_READ_SEQ_ELEM( read_elem, reader )``
in the case of forward reading and by using
``CV_REV_READ_SEQ_ELEM( read_elem, reader )``
in the case of reverse
1489
reading. Both macros put the sequence element to
1490 1491 1492 1493
``read_elem``
and
move the reading pointer toward the next element. A circular structure
of sequence blocks is used for the reading process, that is, after the
1494
last element has been read by the macro
1495 1496 1497 1498 1499 1500
``CV_READ_SEQ_ELEM``
, the
first element is read when the macro is called again. The same applies to
``CV_REV_READ_SEQ_ELEM``
. There is no function to finish the reading
process, since it neither changes the sequence nor creates any temporary
1501
buffers. The reader field
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
``ptr``
points to the current element of
the sequence that is to be read next. The code below demonstrates how
to use the sequence writer and reader.

::

    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* seq = cvCreateSeq( CV_32SC1, sizeof(CvSeq), sizeof(int), storage );
    CvSeqWriter writer;
    CvSeqReader reader;
    int i;
1514

1515 1516 1517 1518 1519 1520 1521 1522
    cvStartAppendToSeq( seq, &writer );
    for( i = 0; i < 10; i++ )
    {
        int val = rand()
        CV_WRITE_SEQ_ELEM( val, writer );
        printf("
    }
    cvEndWriteSeq( &writer );
1523

1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
    cvStartReadSeq( seq, &reader, 0 );
    for( i = 0; i < seq->total; i++ )
    {
        int val;
    #if 1
        CV_READ_SEQ_ELEM( val, reader );
        printf("
    #else /* alternative way, that is prefferable if sequence elements are large,
             or their size/type is unknown at compile time */
        printf("
        CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
    #endif
    }
    ...
1538

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
    cvReleaseStorage( &storage );

..

StartWriteSeq
-------------
Creates a new sequence and initializes a writer for it.

.. ocv:cfunction:: void cvStartWriteSeq(  int seq_flags, int header_size, int elem_size, CvMemStorage* storage, CvSeqWriter* writer )

    :param seq_flags: Flags of the created sequence. If the sequence is not passed to any function working with a specific type of sequences, the sequence value may be equal to 0; otherwise the appropriate type must be selected from the list of predefined sequence types.

    :param header_size: Size of the sequence header. The parameter value may not be less than  ``sizeof(CvSeq)`` . If a certain type or extension is specified, it must fit within the base type header.

    :param elem_size: Size of the sequence elements in bytes; must be consistent with the sequence type. For example, if a sequence of points is created (element type  ``CV_SEQ_ELTYPE_POINT``  ), then the parameter  ``elem_size``  must be equal to  ``sizeof(CvPoint)`` .

    :param storage: Sequence location

    :param writer: Writer state; initialized by the function

The function is a combination of
:ocv:cfunc:`CreateSeq`
1561
and
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
:ocv:cfunc:`StartAppendToSeq`
. The pointer to the
created sequence is stored at
``writer->seq``
and is also returned by the
:ocv:cfunc:`EndWriteSeq`
function that should be called at the end.

TreeToNodeSeq
-------------
Gathers all node pointers to a single sequence.

.. ocv:cfunction:: CvSeq* cvTreeToNodeSeq(  const void* first, int header_size, CvMemStorage* storage )

    :param first: The initial tree node

    :param header_size: Header size of the created sequence (sizeof(CvSeq) is the most frequently used value)

    :param storage: Container for the sequence

1582
The function puts pointers of all nodes reachable from  ``first`` into a single sequence. The pointers are written sequentially in the depth-first order.
1583