Added rewind method.

This commit is contained in:
2021-08-24 10:07:04 -07:00
parent 178d972380
commit ee3c27b54b
7 changed files with 48 additions and 17 deletions

View File

@ -86,4 +86,22 @@ int32_t arrayFindString(char **array, int32_t arrayLength, char *value) {
if(strcmp(array[i], value) == 0) return i;
}
return -1;
}
void arrayRewind(size_t size, void *source, int32_t length, int32_t start,
int32_t count
) {
if(count == -1) count = length - start;
// Create a temporary new array to house the data.
void *temporary = malloc(size * count);
// Copy the data from the source to the temporary array.
arrayCopy(size, arrayGet(size, source, start), count, temporary);
// Now copy the data back to the source array.
arrayCopy(size, temporary, count, source);
// Cleanup the temporary array.
free(temporary);
}

View File

@ -99,4 +99,19 @@ int32_t _arraySorterUint8(const void* left, const void* right);
* @param value The value to search for.
* @return The index that the strings exists within the array.
*/
int32_t arrayFindString(char **array, int32_t arrayLength, char *value);
int32_t arrayFindString(char **array, int32_t arrayLength, char *value);
/**
* Rewinds an array backwards. This is used to take an array that has some data
* used towards the end of an array, but the data near the start is no longer
* used. This will take the end data and put it back at the start.
*
* @param size The size of each element within the array.
* @param source The array itself.
* @param length The total length of the array.
* @param start The first index within the array that you want to keep.
* @param count Count of items after start to use. Use -1 to take the remaining.
*/
void arrayRewind(size_t size, void *source, int32_t length, int32_t start,
int32_t end
);