about summary refs log tree commit diff stats
path: root/src/resarr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/resarr.c')
-rw-r--r--src/resarr.c50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/resarr.c b/src/resarr.c
index 4a72217..a597d86 100644
--- a/src/resarr.c
+++ b/src/resarr.c
@@ -60,7 +60,55 @@ size_t resarr_size(stype_resarr arr) {
 	return arr.elemcount;
 }
 
-// TODO: insert{,_single}, delete, swap, clear
+// TODO: insert{,_single}, delete
+
+int resarr_swap(stype_resarr *first, stype_resarr *second) {
+	if (first == NULL || second == NULL) return -1;
+	if (first->elemsize != second->elemsize) return -1;
+	size_t size = first->elemsize;
+	size_t largest = 0;
+	if (first->elemcount > second->elemcount) {
+		largest = first->elemcount;
+		resarr_reserve(second, first->elemcount);
+	} else {
+		largest = second->elemcount;
+		resarr_reserve(first, second->elemcount);
+	}
+	// There has got to be a better way to do this...
+	stype_resarr uwu = resarr_create(size);
+	resarr_reserve_exact(&uwu, largest);
+	uwu.elemcount = first->elemcount;
+	memcpy(
+		(uintptr_t *)uwu.array,
+		(uintptr_t *)first->array,
+		size
+	);
+	first->elemcount = second->elemcount;
+	memcpy(
+		(uintptr_t *)first->array,
+		(uintptr_t *)second->array,
+		size
+	);
+	second->elemcount = uwu.elemcount;
+	memcpy(
+		(uintptr_t *)second->array,
+		(uintptr_t *)uwu.array,
+		size
+	);
+	resarr_destroy(&uwu);
+	return 0;
+}
+
+int resarr_clear(stype_resarr *arr) {
+	if (arr == NULL) return -1;
+	memset(
+		((uintptr_t *)arr->array),
+		0,
+		arr->elemsize * arr->elemcount
+	);
+	arr->elemcount = 0;
+	return 0;
+}
 
 int resarr_destroy(stype_resarr *arr) {
 	if (arr == NULL) return -1;