utstack.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. utstack: intrusive stack macros for C
  2. =====================================
  3. Arthur O'Dwyer <[email protected]>
  4. v2.3.0, February 2021
  5. Here's a link back to the https://github.com/troydhanson/uthash[GitHub project page].
  6. Introduction
  7. ------------
  8. A set of very simple stack macros for C structures are included with
  9. uthash in `utstack.h`. To use these macros in your own C program, just
  10. copy `utstack.h` into your source directory and use it in your programs.
  11. #include "utstack.h"
  12. These macros support the basic operations of a stack, implemented as
  13. an intrusive linked list. A stack supports the "push", "pop", and "count"
  14. operations, as well as the trivial operation of getting the top element
  15. of the stack.
  16. Download
  17. ~~~~~~~~
  18. To download the `utstack.h` header file,
  19. follow the links on https://github.com/troydhanson/uthash to clone uthash or get a zip file,
  20. then look in the src/ sub-directory.
  21. BSD licensed
  22. ~~~~~~~~~~~~
  23. This software is made available under the
  24. link:license.html[revised BSD license].
  25. It is free and open source.
  26. Platforms
  27. ~~~~~~~~~
  28. The 'utstack' macros have been tested on:
  29. * Linux,
  30. * Mac OS X,
  31. * Windows, using Visual Studio 2008 and Visual Studio 2010
  32. Usage
  33. -----
  34. Stack (list) head
  35. ~~~~~~~~~~~~~~~~~
  36. The stack head is simply a pointer to your element structure. You can name it
  37. anything. *It must be initialized to `NULL`*. It doubles as a pointer to the
  38. top element of your stack.
  39. element *stack = NULL;
  40. Stack operations
  41. ~~~~~~~~~~~~~~~
  42. The only operations on a stack are O(1) pushing, O(1) popping, and
  43. O(n) counting the number of elements on the stack. None of the provided
  44. macros permit directly accessing stack elements other than the top element.
  45. To increase the readability of your code, you can use the macro
  46. `STACK_EMPTY(head)` as a more readable alternative to `head == NULL`,
  47. and `STACK_TOP(head)` as a more readable alternative to `head`.
  48. [width="100%",cols="50<m,40<",grid="none",options="none"]
  49. |===============================================================================
  50. |STACK_PUSH(stack,add); | push `add` onto `stack`
  51. |STACK_POP(stack,elt); | pop `stack` and save previous top as `elt`
  52. |STACK_COUNT(stack,tmp,count); | store number of elements into `count`
  53. |STACK_TOP(stack) | return `stack`
  54. |STACK_EMPTY(stack) | return `stack == NULL`
  55. |===============================================================================
  56. The parameters shown in the table above are explained here:
  57. stack::
  58. The stack head (a pointer to your element structure).
  59. add::
  60. A pointer to the element structure you are adding to the stack.
  61. elt::
  62. A pointer that will be assigned the address of the popped element. Need not be initialized.
  63. tmp::
  64. A pointer of the same type as `elt`. Used internally. Need not be initialized.
  65. count::
  66. An integer that will be assigned the size of the stack. Need not be initialized.
  67. Example
  68. ~~~~~~~
  69. This example program reads names from a text file (one name per line), and
  70. pushes each name on the stack; then pops and prints them in reverse order.
  71. .A stack of names
  72. --------------------------------------------------------------------------------
  73. #include <stdio.h>
  74. #include <stdlib.h>
  75. #include <string.h>
  76. #include "utstack.h"
  77. #define BUFLEN 20
  78. typedef struct el {
  79. char bname[BUFLEN];
  80. struct el *next;
  81. } el;
  82. el *head = NULL; /* important- initialize to NULL! */
  83. int main(int argc, char *argv[]) {
  84. el *elt, *tmp;
  85. char linebuf[sizeof el->bname];
  86. int count;
  87. FILE *file = fopen("test11.dat", "r");
  88. if (file == NULL) {
  89. perror("can't open: ");
  90. exit(-1);
  91. }
  92. while (fgets(linebuf, sizeof linebuf, file) != NULL) {
  93. el *name = malloc(sizeof *name);
  94. if (name == NULL) exit(-1);
  95. strcpy(name->bname, linebuf);
  96. STACK_PUSH(head, name);
  97. }
  98. fclose(file);
  99. STACK_COUNT(head, elt, count);
  100. printf("%d elements were read into the stack\n", count);
  101. /* now pop, print, and delete each element */
  102. while (!STACK_EMPTY(head)) {
  103. printf("%s\n", STACK_TOP(head)->bname);
  104. STACK_POP(head, elt);
  105. free(elt);
  106. }
  107. return 0;
  108. }
  109. --------------------------------------------------------------------------------
  110. [[flex_names]]
  111. Other names for next
  112. ~~~~~~~~~~~~~~~~~~~~
  113. If the element structure's `next` field is named something else, a separate group
  114. of macros must be used. These work the same as the regular macros, but take the
  115. field name as an extra parameter.
  116. These "flexible field name" macros are shown below. They all end with `2`. Each
  117. operates the same as its counterpart without the `2`, but they take the name of
  118. the `next` field as a trailing argument.
  119. [width="100%",cols="50<m,40<",grid="none",options="none"]
  120. |===============================================================================
  121. |STACK_PUSH2(stack,add,next); | push `add` onto `stack`
  122. |STACK_POP2(stack,elt,next); | pop `stack` and save previous top as `elt`
  123. |STACK_COUNT2(stack,tmp,count,next); | store number of elements into `count`
  124. |===============================================================================
  125. // vim: set nowrap syntax=asciidoc: