mvaganov

clist.h

May 10th, 2021 (edited)
768
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h> // used for malloc
  2.  
  3. #define MIN(a, b)   (a < b ? a : b)
  4. #define DEFAULT_SIZE 16
  5.  
  6. typedef struct {
  7.     int capacity, count;
  8.     float * get;
  9. } Listfloat;
  10.  
  11. typedef int (*SortPredicate)(void* a, void* b);
  12.  
  13. enum Listfloat_Result {
  14.     /// function executed without any problems
  15.     Listfloat_Success = 0,
  16.     /// an argument passed into the function was null
  17.     Listfloat_NullReference = -1,
  18.     /// the function has not been implemented
  19.     Listfloat_NotImplemented = -2,
  20.     /// the function arugments are out of bounds
  21.     Listfloat_IndexOutOfBounds = -3,
  22.     /// unable to allocate memory block
  23.     Listfloat_InsufficientMemory = -4,
  24. };
  25.  
  26. #define Listfloat_Nullcheck(self) if (self == NULL) return Listfloat_NullReference;
  27.  
  28. /// @param self will have all of it's values set to zero, indicating that it is unused, and has no memory allocated
  29. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
  30. int Listfloat_Init(Listfloat* self) {
  31.     Listfloat_Nullcheck(self)
  32.     self->count = 0;
  33.     self->capacity = 0;
  34.     self->get = 0;
  35.     return Listfloat_Success;
  36. }
  37.  
  38. /// @param dest where to write data
  39. /// @param src where to get data from (to write)
  40. void Listfloat_Copy(float * dest, float * src, int size) {
  41.     for(int i = 0; i < size; ++i) {
  42.         dest[i] = src[i];
  43.     }
  44. }
  45.  
  46. /// @param size amount of memory for self to allocate. if self already has list data, copy that into the new allocation
  47. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_InsufficientMemory}
  48. int Listfloat_SetCapacity(Listfloat* self, float size) {
  49.     return Listfloat_NotImplemented;
  50. }
  51.  
  52. /// @param self the list to deallocate, and set all values to zero for
  53. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
  54. int Listfloat_Release(Listfloat* self) {
  55.     return Listfloat_NotImplemented;
  56. }
  57.  
  58. // this function is optional to implement
  59. /// @param size if size is greater than the capacity, increase the capacity with SetCapacity
  60. /// @param nextCapacityIfNotEnough if size is greater than capacity, set capacity to this new size
  61. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_InsufficientMemory}
  62. int Listfloat_EnsureCapacity(Listfloat* self, int size, int nextCapacityIfNotEnough) {
  63.     return Listfloat_NotImplemented;
  64. }
  65.  
  66. /// @param value a number to add as the last element of the given list
  67. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_InsufficientMemory}
  68. int Listfloat_Add(Listfloat* self, float value) {
  69.     return Listfloat_NotImplemented;
  70. }
  71.  
  72. /// @param index where to start removing elements
  73. /// @param count how many elements to remove at the given index
  74. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
  75. int Listfloat_RemoveRange(Listfloat* self, int index, int count) {
  76.     return Listfloat_NotImplemented;
  77. }
  78.  
  79. /// @param index where to start remove an element
  80. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
  81. int Listfloat_Remove(Listfloat* self, int index) {
  82.     return Listfloat_NotImplemented;
  83. }
  84.  
  85. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
  86. int Listfloat_Clear(Listfloat* self) {
  87.     return Listfloat_NotImplemented;
  88. }
  89.  
  90. /// @param index where to insert a value. all other values should shift to the right
  91. /// @param value what to value to insert into the given index
  92. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_InsufficientMemory}
  93. int Listfloat_Insert(Listfloat* self, int index, float value) {
  94.     return Listfloat_NotImplemented;
  95. }
  96.  
  97. /// example usage: Listfloat_IndexOfStartingAt(&list, theNumber, 0, list.count)
  98. /// @param value what to value to look for
  99. /// @param startIndex where to start looking in the list
  100. /// @param limit where to stop looking
  101. /// @return the index where value was found, or -1 if not found. possible error codes from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
  102. int Listfloat_IndexOfStartingAt(Listfloat* self, float value, int startIndex, int limit) {
  103.     return Listfloat_NotImplemented;
  104. }
  105.  
  106. /// @param value what to value to look for
  107. /// @return the index where value was found, or -1 if not found. possible error codes from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
  108. int Listfloat_IndexOf(Listfloat* self, float value) {
  109.     return Listfloat_IndexOfStartingAt(self, value, 0, self->count);
  110. }
  111.  
  112. /// example usage: Listfloat_IndexOfLastEndingAt(&list, theNumber, list.count, 0)
  113. /// @param value what to value to look for, searching from right to left (reverse order)
  114. /// @param limitIndex the index after the right-most index to look in
  115. /// @param minIndex the left-most index to look in
  116. /// @return the index where value was found, or -1 if not found. possible error codes from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
  117. int Listfloat_IndexOfLastEndingAt(Listfloat* self, float value, int limitIndex, int minIndex) {
  118.     return Listfloat_NotImplemented;
  119. }
  120.  
  121. /// @param value what to value to look for, searching from right to left (reverse order)
  122. /// @return the index where value was found, or -1 if not found. possible error codes from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
  123. int Listfloat_IndexOfLast(Listfloat* self, float value) {
  124.     return Listfloat_IndexOfLastEndingAt(self, value, self->count, 0);
  125. }
  126.  
  127. int _SortPredicateLittleEndian(void* a, void* b) {
  128.     float fa = *((float*)a);
  129.     float fb = *((float*)b);
  130.     return fa < fb;
  131. }
  132.  
  133. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
  134. int Listfloat_SortPredicate(Listfloat* self, SortPredicate firstArgGoesFirst) {
  135.     return Listfloat_NotImplemented;
  136. }
  137.  
  138. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
  139. int Listfloat_Sort(Listfloat* self) {
  140.     Listfloat_Nullcheck(self)
  141.     return Listfloat_SortPredicate(self, _SortPredicateLittleEndian);
  142. }
  143.  
  144. /// @param X a pointer to a float
  145. /// @param Y a pointer to a float
  146. /// @returns negative integer value if *X < *Y, positive integer value if *X > *Y, zero if *X == *Y. NULL values should go last.
  147. int Listfloat_LittelEndianComparer(void* X, void* Y) {
  148.     if (X == NULL) {
  149.         if (Y == NULL) {
  150.             return 0;
  151.         }
  152.         return 1;
  153.     }
  154.     if (Y == NULL) {
  155.         return -1;
  156.     }
  157.     float r = *((float*)X) - *((float*)Y);
  158.     return (int)r;
  159. }
  160.  
  161. /// @param self a sorted list
  162. /// @param comparer(X,Y): returns negative if X < Y, positive if X > Y, zero if X == Y
  163. /// @returns if value found, return found index. if not found, returns negative value
  164. int Listfloat_BinarySearchComparer(Listfloat* self, float value, SortPredicate comparer) {
  165.     return Listfloat_NotImplemented;
  166. }
  167.  
  168. /// @param self a sorted list
  169. /// @returns if value found, return found index. if not found, returns negative value
  170. int Listfloat_BinarySearch(Listfloat* self, float value) {
  171.     Listfloat_Nullcheck(self)
  172.     return Listfloat_BinarySearchComparer(self, value, Listfloat_LittelEndianComparer);
  173. }
  174.  
  175. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
  176. int Listfloat_Reverse(Listfloat* self) {
  177.     return Listfloat_NotImplemented;
  178. }
  179.  
  180. /// @param start where in the given list to start copying from
  181. /// @param count how many values from the given list to copy
  182. /// @param destination where to copy values into
  183. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
  184. int Listfloat_GetSublist(Listfloat* self, int start, int count, float* destination) {
  185.     return Listfloat_NotImplemented;
  186. }
  187.  
  188. /// @param start where in the given list to start inserting values
  189. /// @param sublist where to get values to insert
  190. /// @param count how many values to insert at the given start index
  191. /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds, Listfloat_InsufficientMemory}
  192. int Listfloat_InsertRange(Listfloat* self, int start, float* sublist, int count) {
  193.     return Listfloat_NotImplemented;
  194. }
  195.  
  196. #undef MIN
  197. #undef DEFAULT_SIZE
RAW Paste Data