Reshuffled
This commit is contained in:
138
src/util/list.c
Normal file
138
src/util/list.c
Normal file
@ -0,0 +1,138 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Msters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "list.h"
|
||||
|
||||
list_t * listCreate() {
|
||||
list_t *list = malloc(sizeof(list_t));
|
||||
if(list == NULL) return NULL;
|
||||
|
||||
list->start = NULL;
|
||||
list->end = NULL;
|
||||
list->size = 0;
|
||||
return list;
|
||||
}
|
||||
|
||||
listentry_t * listAdd(list_t *list, void *data) {
|
||||
//Create the list entry
|
||||
listentry_t *entry = malloc(sizeof(listentry_t));
|
||||
if(entry == NULL) return NULL;
|
||||
|
||||
entry->data = data;
|
||||
entry->next = NULL;
|
||||
entry->prev = NULL;
|
||||
|
||||
//Add it to the list
|
||||
listAddEntry(list, entry);
|
||||
|
||||
//Return the entry
|
||||
return entry;
|
||||
}
|
||||
|
||||
void listAddEntry(list_t *list, listentry_t *entry) {
|
||||
//Is this the first / only thing in the list?
|
||||
if(list->start == NULL) {
|
||||
list->start = entry;
|
||||
list->end = entry;
|
||||
} else {
|
||||
//Make the end's next be this entry, and this entry's prev the end, then
|
||||
//make this the new end
|
||||
entry->prev = list->end;
|
||||
list->end->next = entry;
|
||||
list->end = entry;
|
||||
}
|
||||
list->size++;
|
||||
}
|
||||
|
||||
void listRemove(list_t *list, void *data) {
|
||||
uint32_t i = 0;
|
||||
listentry_t *previous = list->start;
|
||||
|
||||
while(previous != NULL) {
|
||||
if(previous->data == data) {
|
||||
listRemoveEntry(list, previous, false);
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
previous = previous->next;
|
||||
}
|
||||
}
|
||||
|
||||
void listRemoveEntry(list_t *list, listentry_t *entry, bool freeData) {
|
||||
//Update next and prev
|
||||
if(entry->prev != NULL) entry->prev->next = entry->next;
|
||||
if(entry->next != NULL) entry->next->prev = entry->prev;
|
||||
|
||||
//Was this at the end?
|
||||
if(list->start == entry) list->start = entry->next;
|
||||
if(list->end == entry) list->end = entry->prev;
|
||||
|
||||
if(freeData) free(entry->data);
|
||||
free(entry);
|
||||
|
||||
list->size--;
|
||||
}
|
||||
|
||||
|
||||
listentry_t * listGetByIndex(list_t *list, uint32_t index) {
|
||||
if(index >= list->size) return NULL;
|
||||
|
||||
//TODO: We can probably make this more efficient by deciding which way we
|
||||
//should loop from based on the list size.
|
||||
|
||||
uint32_t i = 0;
|
||||
listentry_t *previous = list->start;
|
||||
|
||||
while(previous != NULL) {
|
||||
if(i == index) return previous;
|
||||
i++;
|
||||
previous = previous->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t listGetIndex(list_t *list, void* data) {
|
||||
uint32_t i = 0;
|
||||
listentry_t *previous = list->start;
|
||||
|
||||
while(previous != NULL) {
|
||||
if(previous->data == data) return i;
|
||||
i++;
|
||||
previous = previous->next;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t listGetEntryIndex(list_t *list, listentry_t *entry) {
|
||||
uint32_t i = 0;
|
||||
listentry_t *previous = list->start;
|
||||
|
||||
while(previous != NULL) {
|
||||
if(previous == entry) return i;
|
||||
i++;
|
||||
previous = previous->next;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void listDispose(list_t *list, bool freeData) {
|
||||
//Free all of the entries
|
||||
listentry_t *next = list->start;
|
||||
listentry_t *current;
|
||||
|
||||
while(next != NULL) {
|
||||
current = next;
|
||||
next = current->next;
|
||||
if(freeData) free(current->data);
|
||||
free(current);
|
||||
}
|
||||
|
||||
free(list);
|
||||
}
|
106
src/util/list.h
Normal file
106
src/util/list.h
Normal file
@ -0,0 +1,106 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <malloc.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* Entry within a given linked list.
|
||||
* @param data* The pointer to the data that is within the entry.
|
||||
* @param prev* Pointer to the previous entry in the list.
|
||||
* @param next* Pointer to the next entry in the list.
|
||||
*/
|
||||
typedef struct listentry_t {
|
||||
void *data;
|
||||
struct listentry_t *prev;
|
||||
struct listentry_t *next;
|
||||
} listentry_t;
|
||||
|
||||
/**
|
||||
* Linked List of elements, Doubly Linked.
|
||||
* @param size The count of elements currently within the list
|
||||
* @param start* First element within the list.
|
||||
* @param end* Last element within the list.
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t size;
|
||||
listentry_t *start;
|
||||
listentry_t *end;
|
||||
} list_t;
|
||||
|
||||
//Method definitions
|
||||
|
||||
/**
|
||||
* Creates a new linked list
|
||||
* @return Pointer to a new linked list.
|
||||
*/
|
||||
list_t * listCreate();
|
||||
|
||||
/**
|
||||
* Adds data to a linked list
|
||||
*
|
||||
* @param list* Pointer to a previously created linked list
|
||||
* @param data* Pointer to your data
|
||||
* @return A pointer to the new entry in the linked list that was created
|
||||
*/
|
||||
listentry_t * listAdd(list_t *list, void *data);
|
||||
|
||||
/**
|
||||
* Internal function
|
||||
*/
|
||||
void listAddEntry(list_t *list, listentry_t *entry);
|
||||
|
||||
/**
|
||||
* Remove data from a linked list.
|
||||
*
|
||||
* @param list Pointer to a previously created linked list.
|
||||
* @param data* Pointer to your data.
|
||||
*/
|
||||
void listRemove(list_t *list, void *data);
|
||||
|
||||
/**
|
||||
* Remove a list entry from the linked list.
|
||||
*
|
||||
* @param list* Pointer to a previously created linked list.
|
||||
* @param entry* Pointer to the entry within the linked list.
|
||||
* @param freeData If true the data of the listentry_t will be free()'d
|
||||
*/
|
||||
void listRemoveEntry(list_t *list, listentry_t *entry, bool freeData);
|
||||
|
||||
/**
|
||||
* Returns the entry at a given index. This method is costly.
|
||||
* @param list* Pointer to a previously created linked list
|
||||
* @param index Index of element within the linked list to remove
|
||||
* @return The entry at the index or NULL if outside the bounds.
|
||||
*/
|
||||
listentry_t * listGetByIndex(list_t *list, uint32_t index);
|
||||
|
||||
/**
|
||||
* Returns the index of data within the linked list. This method is costly.
|
||||
*
|
||||
* @param list* Pointer to a previously created linked list
|
||||
* @param data* Pointer to your data.
|
||||
* @return The index within the list the entry is in, or -1 if not found.
|
||||
*/
|
||||
uint32_t listGetIndex(list_t *list, void* data);
|
||||
|
||||
/**
|
||||
* Returns the index of an entry within the linked list. This method is costly.
|
||||
*
|
||||
* @param list* Pointer to a previously created linked list
|
||||
* @param entry* Pointer to the entry within the linked list.
|
||||
* @return The index within the list the entry is in, or -1 if not found.
|
||||
*/
|
||||
uint32_t listGetEntryIndex(list_t *list, listentry_t *entry);
|
||||
|
||||
/**
|
||||
* Dispose a previously created link list.
|
||||
* @param list* Pointer to a previously created linked list
|
||||
* @param freeData If true the data within each listentry_t will be free()'d
|
||||
*/
|
||||
void listDispose(list_t *list, bool freeData);
|
10
src/util/math.h
Normal file
10
src/util/math.h
Normal file
@ -0,0 +1,10 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
|
||||
#define mathMod(a,b) (a%b+b)%b
|
||||
#define mathMax(a,b) (a<b?b:a)
|
||||
#define mathMin(a,b) (a>b?b:a)
|
13
src/util/string.h
Normal file
13
src/util/string.h
Normal file
@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
# define strtok_r strtok_s
|
||||
#endif
|
Reference in New Issue
Block a user