src/array_list.c

changeset 1026
ca20f9ffcb62
parent 1022
2911c1f4a570
--- a/src/array_list.c	Wed Dec 18 15:36:45 2024 +0100
+++ b/src/array_list.c	Wed Dec 18 15:40:13 2024 +0100
@@ -108,7 +108,7 @@
     size_t oldcap;
     size_t oldsize;
     size_t max_size;
-    if (width == 0 || width == __WORDSIZE) {
+    if (width == 0 || width == CX_WORDSIZE) {
         oldcap = *(size_t*) capacity;
         oldsize = *(size_t*) size;
         max_size = SIZE_MAX;
@@ -121,7 +121,7 @@
         oldsize = *(uint8_t*) size;
         max_size = UINT8_MAX;
     }
-#if __WORDSIZE == 64
+#if CX_WORDSIZE == 64
     else if (width == 32) {
         oldcap = *(uint32_t*) capacity;
         oldsize = *(uint32_t*) size;
@@ -162,16 +162,16 @@
         *array = newmem;
 
         // store new capacity
-        if (width == 0 || width == __WORDSIZE) {
+        if (width == 0 || width == CX_WORDSIZE) {
             *(size_t*) capacity = newcap;
         } else if (width == 16) {
-            *(uint16_t*) capacity = newcap;
+            *(uint16_t*) capacity = (uint16_t) newcap;
         } else if (width == 8) {
-            *(uint8_t*) capacity = newcap;
+            *(uint8_t*) capacity = (uint8_t) newcap;
         }
-#if __WORDSIZE == 64
+#if CX_WORDSIZE == 64
         else if (width == 32) {
-            *(uint32_t*) capacity = newcap;
+            *(uint32_t*) capacity = (uint32_t) newcap;
         }
 #endif
     }
@@ -201,7 +201,7 @@
     size_t oldcap;
     size_t oldsize;
     size_t max_size;
-    if (width == 0 || width == __WORDSIZE) {
+    if (width == 0 || width == CX_WORDSIZE) {
         oldcap = *(size_t*) capacity;
         oldsize = *(size_t*) size;
         max_size = SIZE_MAX;
@@ -214,7 +214,7 @@
         oldsize = *(uint8_t*) size;
         max_size = UINT8_MAX;
     }
-#if __WORDSIZE == 64
+#if CX_WORDSIZE == 64
     else if (width == 32) {
         oldcap = *(uint32_t*) capacity;
         oldsize = *(uint32_t*) size;
@@ -278,20 +278,20 @@
 
     // if any of size or capacity changed, store them back
     if (newsize != oldsize || newcap != oldcap) {
-        if (width == 0 || width == __WORDSIZE) {
+        if (width == 0 || width == CX_WORDSIZE) {
             *(size_t*) capacity = newcap;
             *(size_t*) size = newsize;
         } else if (width == 16) {
-            *(uint16_t*) capacity = newcap;
-            *(uint16_t*) size = newsize;
+            *(uint16_t*) capacity = (uint16_t) newcap;
+            *(uint16_t*) size = (uint16_t) newsize;
         } else if (width == 8) {
-            *(uint8_t*) capacity = newcap;
-            *(uint8_t*) size = newsize;
+            *(uint8_t*) capacity = (uint8_t) newcap;
+            *(uint8_t*) size = (uint8_t) newsize;
         }
-#if __WORDSIZE == 64
+#if CX_WORDSIZE == 64
         else if (width == 32) {
-            *(uint32_t*) capacity = newcap;
-            *(uint32_t*) size = newsize;
+            *(uint32_t*) capacity = (uint32_t) newcap;
+            *(uint32_t*) size = (uint32_t) newsize;
         }
 #endif
     }
@@ -435,6 +435,9 @@
         return 0;
     }
 
+    // special case: there is only one element and that is smaller
+    if (size == 1) return 0;
+
     // check the last array element
     result = cmp_func(elem, array + elem_size * (size - 1));
     if (result >= 0) {

mercurial