1
0

List.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef List_H
  16. #define List_H
  17. #include "memory/Allocator.h"
  18. #include "benc/Object.h"
  19. #include "util/Linker.h"
  20. Linker_require("benc/List.c");
  21. struct List_Item;
  22. struct List_Item {
  23. struct List_Item* next;
  24. Object* elem;
  25. };
  26. List* List_new(struct Allocator* alloc);
  27. /**
  28. * Get the length of a list.
  29. *
  30. * @param a list
  31. * @return the length of the given list or -1 if the list argument is NULL.
  32. */
  33. int32_t List_size(const List* list);
  34. /**
  35. * Add an integer to a list, if the list does not exist then it is allocated.
  36. *
  37. * @param list the list to add the integer item to, if NULL then it is allocated.
  38. * @param toAdd the integer to add to the list. int64_t is an alias for int64_t
  39. * @param allocator the means of getting memory space for storing the list entry.
  40. */
  41. void List_addInt(List* list, int64_t toAdd, struct Allocator* allocator);
  42. /**
  43. * Add a string to a list, if the list does not exist then it is allocated.
  44. * NOTE: This will not copy the given string, only add a pointer to it in the list.
  45. *
  46. * @param list the list to add the string item to, if NULL then it is allocated.
  47. * @param toAdd the string to add to the list.
  48. * @param allocator the means of getting memory space for storing the list entry.
  49. */
  50. void List_addString(List* list, String* toAdd, struct Allocator* allocator);
  51. #define List_addStringC(l,s,a) List_addString(l, String_new(s, a), a)
  52. /**
  53. * Add a dictionary to a list, if the list does not exist then it is allocated.
  54. * NOTE: This will not copy the given dictionary, only add a pointer to it in the list.
  55. *
  56. * @param list the list to add the string item to, if NULL then it is allocated.
  57. * @param toAdd the dictionary to add to the list.
  58. * @param allocator the means of getting memory space for storing the list entry.
  59. */
  60. void List_addDict(List* list, Dict* toAdd, struct Allocator* allocator);
  61. /**
  62. * Add a list as an item to another list, if the list does not exist then it is allocated.
  63. * NOTE: This will not copy the list which it is adding, only add a pointer to it in the list.
  64. *
  65. * @param list the list to add the item to, if NULL then it is allocated.
  66. * @param toAdd the list to add as an item.
  67. * @param allocator the means of getting memory space for storing the list entry.
  68. */
  69. void List_addList(List* list, List* toAdd, struct Allocator* allocator);
  70. /**
  71. * Get an integer from a list.
  72. *
  73. * @param list the list to get the integer item from.
  74. * @param index the index of the item.
  75. * @return a pointer to the integer at that index or NULL if either the index
  76. * is out of range or the item at that index is not an integer.
  77. */
  78. int64_t* List_getInt(const List* list, uint32_t index);
  79. /**
  80. * Get a string from a list.
  81. *
  82. * @param list the list to get the string item from.
  83. * @param index the index of the item.
  84. * @return a pointer to the string at that index or NULL if either the index
  85. * is out of range or the item at that index is not a string.
  86. */
  87. String* List_getString(const List* list, uint32_t index);
  88. /**
  89. * Get a dictionary from a list.
  90. *
  91. * @param list the list to get the dictionary from.
  92. * @param index the index of the item.
  93. * @return a pointer to the string at that index or NULL if either the index
  94. * is out of range or the item at that index is not a dictionary.
  95. */
  96. Dict* List_getDict(const List* list, uint32_t index);
  97. /**
  98. * Get a list from another list.
  99. *
  100. * @param list the list to get the item from.
  101. * @param index the index of the item.
  102. * @return a pointer to the string at that index or NULL if either the index
  103. * is out of range or the item at that index is not a list.
  104. */
  105. List* List_getList(const List* list, uint32_t index);
  106. #define List_OBJ(x) (&(Object) { .type = Object_LIST, .as.list = x })
  107. #endif