deds_vector.htm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
  2. <html><head>
  3. <title>vector</title>
  4. </head><body>
  5. <p>[<a href="mxdoc.htm">mainpage</a>]
  6. [<a href="tags.htm">tags</a>]<br>
  7. <h1><a name="vector">module: vector</a></h1><p><b>A 100% typesafe generic vector for C.</b><p>
  8. This module contains macros, types and functions for MX_VECTOR whic is a
  9. a 100% typesafe generic vector object for C. The underlying type must be
  10. bit-copy-correct which means that a binary copy retains proper functioning
  11. and takes over ownership of any internal buffers.
  12. <p>
  13. The interface of MX_VECTOR is done through macros and generic type-indenpendent
  14. functions perform the vector operations. In contrast to C++ vector types,
  15. the code size when using MX_VECTOR is constant regardless of how many vector
  16. types are instantiated.
  17. <p>
  18. The correct operation of MX_VECTOR depends on some things that not in the C
  19. standard:<ul>
  20. <li>Size of pointer to void and pointer to other types are the same size,
  21. representation and alignment</li>
  22. <li>Writing data in a element of a union and reading out of a different
  23. element works as expected</li>
  24. </ul>
  25. All of the compilers I have tested give no warnings on the
  26. MX_VECTOR code and seem to function properly. I have tested 16 and 32 bit
  27. compilers as well as compilers with various byte orders.
  28. <p>
  29. MX_VECTOR objects themselves are bit-copy-correct which means that memory
  30. containing a MX_VECTOR can be safely realloc'd and MX_VECTOR can be used
  31. inside another MXVECTOR.
  32. <p>
  33. Here is a simple example for doubles but the same concept applies to all
  34. bit-copy-correct types.
  35. <pre>
  36. size_t i;
  37. MX_VECTOR(double) dvect;
  38. mx_vector(&dvect);
  39. mx_vector_resize(&dvect, 100);
  40. for (i=0; i &lt; mx_vector_size(&dvect); i++)
  41. dvect.data[i] = i;
  42. mx_vector_free(&dvect);
  43. </pre>
  44. NOTE: All of the mx_vector* functions are in fact macros that may evaluate
  45. the arguments (the vector) more than once. This is especially true for the
  46. vector itself (the first argument) and any pointer arguments. Efforts
  47. have been made to evaluate other arguments only once. So modifiying
  48. arguments in the macro like this:
  49. <pre>
  50. mx_vector_resize(&dvect, n++);
  51. </pre>
  52. will work as expected. However code like this:
  53. <pre>
  54. mx_vector_resize(vectptr++, n);
  55. </pre>
  56. will not work as expected.
  57. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p>Part of the <a href="deds.htm">deds</a> library.<ul><li>
  58. <a name="define MX_VECTOR(type)"><code>#define MX_VECTOR(type)</code></a>
  59. <p>
  60. <p><b>A 100% typesafe generic vector for C</b><p>
  61. This macro functions as a type delaration for a vector of user-defined
  62. type.
  63. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  64. <li>
  65. <a name="define MX_VECTOR_ITERATOR(type)"><code>#define MX_VECTOR_ITERATOR(type)</code></a>
  66. <p>
  67. <p><b>An iterator for a MX_VECTOR</b><p>
  68. This macro functions as a type declaration for an iterator for a
  69. similarly typed MX_VECTOR.
  70. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  71. <li>
  72. <a name="define mx_vector(mxvect)"><code>#define mx_vector(mxvect)</code></a>
  73. <p>
  74. <p><b>Construct a MX_VECTOR</b><p>
  75. This macro constructs a MX_VECTOR object. This must be called first
  76. before any other mx_vector_* functions are called.
  77. <p>MX_VECTOR is correctly initialized with a 0 bit pattern. So static
  78. MX_VECTOR are correctly initialized and dont necessarily need a call to the
  79. constructor.
  80. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  81. <li>
  82. <a name="define mx_vector_free(mxvect)"><code>#define mx_vector_free(mxvect)</code></a>
  83. <p>
  84. <p><b>Destruct a MX_VECTOR</b><p>
  85. This macro destroys an MX_VECTOR object by releasing any memory
  86. that has been allocated. It should be the last call to any mx_vector_*
  87. functions. Access of the MX_VECTOR object after destruction should not be done.
  88. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  89. <li>
  90. <a name="define mx_vector_at(mxvect, mxn)"><code>#define mx_vector_at(mxvect, mxn)</code></a>
  91. <p>
  92. <p><b>Return an element from a MX_VECTOR at a specific index</b><p>
  93. This macro returns elements from a MX_VECTOR at a specific index.
  94. It is safer than accessing the underlying pointer because it wont allow
  95. access outside the correct range for the vector.
  96. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  97. <li>
  98. <a name="define mx_vector_back(mxvect)"><code>#define mx_vector_back(mxvect)</code></a>
  99. <p>
  100. <p><b>Return the last element from a MX_VECTOR</b><p>
  101. This macro returns the last element from a MX_VECTOR.
  102. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  103. <li>
  104. <a name="define mx_vector_begin(mxvect)"><code>#define mx_vector_begin(mxvect)</code></a>
  105. <p>
  106. <p><b>Pointer to first element</b><p>
  107. This macro returns a pointer to the first data element in the MX_VECTOR object.
  108. If the MX_VECTOR contains no data elements then 0 is returned.
  109. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  110. <li>
  111. <a name="define mx_vector_capacity(mxvect)"><code>#define mx_vector_capacity(mxvect)</code></a>
  112. <p>
  113. <p><b>Capacity</b><p>
  114. This macro returns the capacity of an MX_VECTOR object. This is the
  115. number of elements that the vector can contain without reqriring any internal
  116. memory management.
  117. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  118. <li>
  119. <a name="define mx_vector_clear(mxvect)"><code>#define mx_vector_clear(mxvect)</code></a>
  120. <p>
  121. <p><b>Clear</b><p>
  122. This macro deletes all the objects from a MX_VECTOR
  123. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  124. <li>
  125. <a name="define mx_vector_empty(mxvect)"><code>#define mx_vector_empty(mxvect)</code></a>
  126. <p>
  127. <p><b>Empty</b><p>
  128. This macro deletes all the objects from a MX_VECTOR
  129. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  130. <li>
  131. <a name="define mx_vector_end(mxvect)"><code>#define mx_vector_end(mxvect)</code></a>
  132. <p>
  133. <p><b>Pointer one-past last element</b><p>
  134. This macro returns a pointer one-past the last data element in the MX_VECTOR object.
  135. If the MX_VECTOR contains no data elements then 0 is returned.
  136. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  137. <li>
  138. <a name="define mx_vector_erase(mxvect, mxiter)"><code>#define mx_vector_erase(mxvect, mxiter)</code></a>
  139. <p>
  140. <p><b>Erase an element</b><p>
  141. This macro removes a data element from an MX_VECTOR object.
  142. If there are data elements at indicies higher than those removed then they
  143. are are moved to lower indicies.
  144. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  145. <li>
  146. <a name="define mx_vector_push_back(mxvect, mxdat)"><code>#define mx_vector_push_back(mxvect, mxdat)</code></a>
  147. <p>
  148. <p><b>Push an element onto the back of the MX_VECTOR</b><p>
  149. This macro makes a bitwise copy of a single data element and appends
  150. the copy onto an existing MX_VECTOR object. The size of the vector is expanded
  151. to hold the new data elements.
  152. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  153. <li>
  154. <a name="define mx_vector_size(mxvect)"><code>#define mx_vector_size(mxvect)</code></a>
  155. <p>
  156. <p><b>Number of elements</b><p>
  157. This macro returns the size of an MX_VECTOR object. This is the number
  158. of data elements that can be safely accessed.
  159. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  160. <li>
  161. <a name="define mx_vector_resize(mxvect, n)"><code>#define mx_vector_resize(mxvect, n)</code></a>
  162. <p>
  163. <p><b>Resize</b><p>
  164. This macro resizes a MX_VECTOR object. If the MX_VECTOR does not have
  165. sufficient capacity to hold the new number of elements then new space is
  166. reserved using mx_vector_reserve().
  167. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  168. <li>
  169. <a name="define mx_vector_reserve(mxvect, n)"><code>#define mx_vector_reserve(mxvect, n)</code></a>
  170. <p>
  171. <p><b>Set capacity</b><p>
  172. This macro reserves space in a MX_VECTOR object for later copying or
  173. appending of data elements. If enough space is reserved the insertion or appending
  174. of elements does not require memory management (realloc of internal buffer) and
  175. will be fast. If enough space is already reserved this macro does nothing.
  176. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  177. <li>
  178. <a name="define mx_vector_extend(mxvect, n)"><code>#define mx_vector_extend(mxvect, n)</code></a>
  179. <p>
  180. <p><b>Increase capacity</b><p>
  181. This macro reserves space for additional data elements in a MX_VECTOR object.
  182. If enough space is reserved then future insertion or appending may not require memory
  183. management (realloc of internal buffer) and will therefore be fast. If enough space
  184. is already reserved this macro does nothing.
  185. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  186. <li>
  187. <a name="define mx_vector_contract(mxvect)"><code>#define mx_vector_contract(mxvect)</code></a>
  188. <p>
  189. <p><b>Reduce capacity to minimum</b><p>
  190. This macro reduces the memory used by a MX_VECTOR object to the
  191. minimum required to safely contain the data elements contained. This function can
  192. cause memory management (realloc of internal buffer) to occur.
  193. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  194. <li>
  195. <a name="define mx_vector_append1(mxvect, dat)"><code>#define mx_vector_append1(mxvect, dat)</code></a>
  196. <p>
  197. <p><b>Append 1 element (bitwise copy)</b><p>
  198. This macro makes a bitwise copy of a single data element and appends
  199. the copy onto an existing MX_VECTOR object. The size of the vector is expanded
  200. to hold the new data elements.
  201. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  202. <li>
  203. <a name="define mx_vector_append(mxvect, dat, n)"><code>#define mx_vector_append(mxvect, dat, n)</code></a>
  204. <p>
  205. <p><b>Append a number of elements (bitwise copy)</b><p>
  206. This macro makes a bitwise copy of a number of data elements and appends the copies
  207. onto an existing MX_VECTOR object. More than one data element can be appended at once.
  208. If necessary the size of the vector is expanded to hold the new data elements.
  209. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  210. <li>
  211. <a name="define mx_vector_insert1(mxvect, dat, ind)"><code>#define mx_vector_insert1(mxvect, dat, ind)</code></a>
  212. <p>
  213. <p><b>Insert 1 element (bitwise copy)</b><p>
  214. This macro makes a bitwise copy of one data element and inserts the
  215. copy into an existing MX_VECTOR object. If necessary the size of the vector
  216. is expanded to hold the new data element.
  217. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  218. <li>
  219. <a name="define mx_vector_insert(mxvect, dat, n, ind)"><code>#define mx_vector_insert(mxvect, dat, n, ind)</code></a>
  220. <p>
  221. <p><b>Insert a number of elements (bitwise copy)</b><p>
  222. This macro makes a bitwise copy of a number of data elements and inserts the
  223. copies into an existing MX_VECTOR object at sepcific indicies. More than one data
  224. element can be inserted at once. If necessary the size of the vector is expanded
  225. to hold the new data elements.
  226. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  227. <li>
  228. <a name="define mx_vector_remove_last(mxvect)"><code>#define mx_vector_remove_last(mxvect)</code></a>
  229. <p>
  230. <p><b>Remove last element</b><p>
  231. This macro removes only the last object from a vector. The size of the MX_VECTOR
  232. is reduced by one.
  233. (<a href="../include/deds/vector.h">../include/deds/vector.h</a>)<p></li>
  234. <li>
  235. <a name="size_t mx__vector_makeroom(MX__VECTOR_DATA* vect, const size_t num, const size_t place, const size_t sizeofdata)"><code>size_t mx__vector_makeroom(MX__VECTOR_DATA* vect, const size_t num, const size_t place, const size_t sizeofdata)</code></a>
  236. <p>
  237. <p>During appending we expand vectors by doubling thier size with a
  238. minimum of 16 but a different value might be better in some applications.
  239. Can a reasonable API be made for this?
  240. (<a href="../include/deds/vector/makeroom.c">../include/deds/vector/makeroom.c</a>)<p></li>
  241. </ul><p>Generated by <a href="http://www.deleveld.dds.nl/mxdoc/index.htm">MXDOC</a> 2.2 on Sun Feb 4 15:16:26 2007
  242. </body></html>