code comparison optimization
[compress.git] / code.c
diff --git a/code.c b/code.c
index 17ef4d0ef9b43f07d40d6d19bc6ae4ceba758a86..b8c8e78c7067cca3498dacda767185af1ed4fbe7 100644 (file)
--- a/code.c
+++ b/code.c
@@ -1,18 +1,21 @@
-#include <stddef.h>
-#include <stdio.h>
 #include "debug.h"
+#include "fdprintf.h"
 
 #include "code.h"
 
+/* constants */
+
+#define NB_LEAFS ((NB_BYTES + 1) * NB_BYTES / 2)
+
 /* concatenate code */
 
-int codcat (char *dst, size_t n, char *src)
+int codcat (char *dst, int n, char *src)
 {
     while ((*dst != 0) && (n > 0)) {
         n--;
         dst++;
     }
-    
+
     return (n > 0) ? codcpy (dst, n, src) : -1;
 }
 
@@ -20,23 +23,21 @@ int codcat (char *dst, size_t n, char *src)
 
 int codcmp (char *cod1, char *cod2)
 {
-    int i = -1;
-
-    do {
-        i++;
-        if (cod1[i] != cod2[i]) {
-            return (cod1[i] < cod2[i]) ? -1 : 1;
+    while ((*cod1 != 0) || (*cod2 != 0)) {
+        int test = *cod1++ - *cod2++;
+        if (test != 0) {
+            return (test < 0) ? -1 : 1;
         }
-    } while (cod1[i] != 0);
+    }
 
     return 0;
 }
 
 /* copy code */
 
-int codcpy (char *dst, size_t n, char *src)
+int codcpy (char *dst, int n, char *src)
 {
-    size_t i;
+    int i;
 
     for (i = 0; i < n; i++) {
         dst[i] = src[i];
@@ -44,7 +45,7 @@ int codcpy (char *dst, size_t n, char *src)
             return i;
         }
     }
-    VERBOSE (ERROR, printf ("Buffer too short\n"));
+    VERBOSE (ERROR, PRINTERR ("Buffer too short\n"));
 
     return -1;
 }
@@ -62,4 +63,22 @@ int codlen (char *code)
     return i;
 }
 
-// vim: ts=4 sw=4 et
+/* get leaf(s) */
+
+leaf_t *getleaf (int n)
+{
+    static leaf_t leafs[NB_LEAFS] = {0};
+    static int pos = 0;
+    leaf_t *pt = (void *)0;
+
+    if (pos + n < NB_LEAFS) {
+        pt = leafs + pos;
+        pos += n;
+    } else {
+        VERBOSE (ERROR, PRINTERR ("No more leaf avaliable\n"));
+    }
+
+    return pt;
+}
+
+/* vim: set ts=4 sw=4 et: */