Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h> // used for malloc
- #define MIN(a, b) (a < b ? a : b)
- #define DEFAULT_SIZE 16
- typedef struct {
- int capacity, count;
- float * get;
- } Listfloat;
- typedef int (*SortPredicate)(void* a, void* b);
- enum Listfloat_Result {
- /// function executed without any problems
- Listfloat_Success = 0,
- /// an argument passed into the function was null
- Listfloat_NullReference = -1,
- /// the function has not been implemented
- Listfloat_NotImplemented = -2,
- /// the function arugments are out of bounds
- Listfloat_IndexOutOfBounds = -3,
- /// unable to allocate memory block
- Listfloat_InsufficientMemory = -4,
- };
- #define Listfloat_Nullcheck(self) if (self == NULL) return Listfloat_NullReference;
- /// @param self will have all of it's values set to zero, indicating that it is unused, and has no memory allocated
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
- int Listfloat_Init(Listfloat* self) {
- Listfloat_Nullcheck(self)
- self->count = 0;
- self->capacity = 0;
- self->get = 0;
- return Listfloat_Success;
- }
- /// @param dest where to write data
- /// @param src where to get data from (to write)
- void Listfloat_Copy(float * dest, float * src, int size) {
- for(int i = 0; i < size; ++i) {
- dest[i] = src[i];
- }
- }
- /// @param size amount of memory for self to allocate. if self already has list data, copy that into the new allocation
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_InsufficientMemory}
- int Listfloat_SetCapacity(Listfloat* self, float size) {
- return Listfloat_NotImplemented;
- }
- /// @param self the list to deallocate, and set all values to zero for
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
- int Listfloat_Release(Listfloat* self) {
- return Listfloat_NotImplemented;
- }
- // this function is optional to implement
- /// @param size if size is greater than the capacity, increase the capacity with SetCapacity
- /// @param nextCapacityIfNotEnough if size is greater than capacity, set capacity to this new size
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_InsufficientMemory}
- int Listfloat_EnsureCapacity(Listfloat* self, int size, int nextCapacityIfNotEnough) {
- return Listfloat_NotImplemented;
- }
- /// @param value a number to add as the last element of the given list
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_InsufficientMemory}
- int Listfloat_Add(Listfloat* self, float value) {
- return Listfloat_NotImplemented;
- }
- /// @param index where to start removing elements
- /// @param count how many elements to remove at the given index
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
- int Listfloat_RemoveRange(Listfloat* self, int index, int count) {
- return Listfloat_NotImplemented;
- }
- /// @param index where to start remove an element
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
- int Listfloat_Remove(Listfloat* self, int index) {
- return Listfloat_NotImplemented;
- }
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
- int Listfloat_Clear(Listfloat* self) {
- return Listfloat_NotImplemented;
- }
- /// @param index where to insert a value. all other values should shift to the right
- /// @param value what to value to insert into the given index
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_InsufficientMemory}
- int Listfloat_Insert(Listfloat* self, int index, float value) {
- return Listfloat_NotImplemented;
- }
- /// example usage: Listfloat_IndexOfStartingAt(&list, theNumber, 0, list.count)
- /// @param value what to value to look for
- /// @param startIndex where to start looking in the list
- /// @param limit where to stop looking
- /// @return the index where value was found, or -1 if not found. possible error codes from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
- int Listfloat_IndexOfStartingAt(Listfloat* self, float value, int startIndex, int limit) {
- return Listfloat_NotImplemented;
- }
- /// @param value what to value to look for
- /// @return the index where value was found, or -1 if not found. possible error codes from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
- int Listfloat_IndexOf(Listfloat* self, float value) {
- return Listfloat_IndexOfStartingAt(self, value, 0, self->count);
- }
- /// example usage: Listfloat_IndexOfLastEndingAt(&list, theNumber, list.count, 0)
- /// @param value what to value to look for, searching from right to left (reverse order)
- /// @param limitIndex the index after the right-most index to look in
- /// @param minIndex the left-most index to look in
- /// @return the index where value was found, or -1 if not found. possible error codes from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
- int Listfloat_IndexOfLastEndingAt(Listfloat* self, float value, int limitIndex, int minIndex) {
- return Listfloat_NotImplemented;
- }
- /// @param value what to value to look for, searching from right to left (reverse order)
- /// @return the index where value was found, or -1 if not found. possible error codes from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
- int Listfloat_IndexOfLast(Listfloat* self, float value) {
- return Listfloat_IndexOfLastEndingAt(self, value, self->count, 0);
- }
- int _SortPredicateLittleEndian(void* a, void* b) {
- float fa = *((float*)a);
- float fb = *((float*)b);
- return fa < fb;
- }
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
- int Listfloat_SortPredicate(Listfloat* self, SortPredicate firstArgGoesFirst) {
- return Listfloat_NotImplemented;
- }
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
- int Listfloat_Sort(Listfloat* self) {
- Listfloat_Nullcheck(self)
- return Listfloat_SortPredicate(self, _SortPredicateLittleEndian);
- }
- /// @param X a pointer to a float
- /// @param Y a pointer to a float
- /// @returns negative integer value if *X < *Y, positive integer value if *X > *Y, zero if *X == *Y. NULL values should go last.
- int Listfloat_LittelEndianComparer(void* X, void* Y) {
- if (X == NULL) {
- if (Y == NULL) {
- return 0;
- }
- return 1;
- }
- if (Y == NULL) {
- return -1;
- }
- float r = *((float*)X) - *((float*)Y);
- return (int)r;
- }
- /// @param self a sorted list
- /// @param comparer(X,Y): returns negative if X < Y, positive if X > Y, zero if X == Y
- /// @returns if value found, return found index. if not found, returns negative value
- int Listfloat_BinarySearchComparer(Listfloat* self, float value, SortPredicate comparer) {
- return Listfloat_NotImplemented;
- }
- /// @param self a sorted list
- /// @returns if value found, return found index. if not found, returns negative value
- int Listfloat_BinarySearch(Listfloat* self, float value) {
- Listfloat_Nullcheck(self)
- return Listfloat_BinarySearchComparer(self, value, Listfloat_LittelEndianComparer);
- }
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference}
- int Listfloat_Reverse(Listfloat* self) {
- return Listfloat_NotImplemented;
- }
- /// @param start where in the given list to start copying from
- /// @param count how many values from the given list to copy
- /// @param destination where to copy values into
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds}
- int Listfloat_GetSublist(Listfloat* self, int start, int count, float* destination) {
- return Listfloat_NotImplemented;
- }
- /// @param start where in the given list to start inserting values
- /// @param sublist where to get values to insert
- /// @param count how many values to insert at the given start index
- /// @return an error code from Listfloat_Result {Listfloat_Success, Listfloat_NullReference, Listfloat_IndexOutOfBounds, Listfloat_InsufficientMemory}
- int Listfloat_InsertRange(Listfloat* self, int start, float* sublist, int count) {
- return Listfloat_NotImplemented;
- }
- #undef MIN
- #undef DEFAULT_SIZE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement