]> mj.ucw.cz Git - libucw.git/commitdiff
small KMP fixes
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Tue, 18 Apr 2006 11:26:44 +0000 (13:26 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Tue, 18 Apr 2006 11:26:44 +0000 (13:26 +0200)
lib/kmp-new.h
lib/kmp-search.h
lib/kmp-test.c

index 15c15ae37fc43c38ec07e070db6597378c689ebe..e9ae2fad4ed2b9e98b4a0508bfbf76e310d2abe6 100644 (file)
@@ -114,7 +114,7 @@ P(hash_eq) (struct P(hash_table) *t UNUSED, struct P(state) *f1, P(char_t) c1, s
 static inline void
 P(hash_init_key) (struct P(hash_table) *t UNUSED, struct P(state) *s, struct P(state) *f, P(char_t) c)
 {
-  memset(s, 0, sizeof(*s));
+  bzero(s, sizeof(*s));
   s->from = f;
   s->c = c;
   s->next = f->back; /* the pointers hold the link-list of sons... change in build() */
@@ -157,9 +157,9 @@ typedef byte *P(source_t);
 
 #ifdef KMP_GET_CHAR
 static inline int
-P(get_char) (struct P(context) *ctx, P(source_t) *src, P(char_t) *c)
+P(get_char) (struct P(context) *ctx UNUSED, P(source_t) *src UNUSED, P(char_t) *c UNUSED)
 {
-  return KMP_GET_CHAR(ctx, *src, *c);
+  return KMP_GET_CHAR(ctx, (*src), (*c));
 }
 #else
 #  if defined(KMP_USE_UTF8)
@@ -179,7 +179,7 @@ P(get_char) (struct P(context) *ctx UNUSED, P(source_t) *src, P(char_t) *c)
   uns cc;
   *src = (byte *)utf8_get(*src, &cc);
 # ifdef KMP_ONLYALPHA
-  if (unlikely(!cc)) {}
+  if (!cc) {}
   else if (!Ualpha(cc))
     cc = P(control_char)();
   else
@@ -195,7 +195,7 @@ P(get_char) (struct P(context) *ctx UNUSED, P(source_t) *src, P(char_t) *c)
 # else
   uns cc = *(*src)++;
 # ifdef KMP_ONLYALPHA
-  if (unlikely(!cc)) {}
+  if (!cc) {}
   else if (!Calpha(cc))
     cc = P(control_char)();
   else
@@ -224,7 +224,7 @@ P(add) (struct P(context) *ctx, P(source_t) src
 # endif
 
   P(char_t) c;
-  if (unlikely(!P(get_char)(ctx, &src, &c)))
+  if (!P(get_char)(ctx, &src, &c))
     return NULL;
   struct P(state) *p = &ctx->null, *s;
   uns len = 0;
@@ -236,7 +236,7 @@ P(add) (struct P(context) *ctx, P(source_t) src
          {
            s = P(hash_new)(&ctx->hash, p, c);
            len++;
-           if (unlikely(!(P(get_char)(ctx, &src, &c))))
+           if (!(P(get_char)(ctx, &src, &c)))
              goto enter_new;
            p = s;
          }
@@ -266,7 +266,7 @@ enter_new:
 static void
 P(init) (struct P(context) *ctx)
 {
-  memset(ctx, 0, sizeof(*ctx));
+  bzero(ctx, sizeof(*ctx));
   P(hash_init)(&ctx->hash);
 }
 
index 3978c847ac6cb632dbe0707c1b21462a5f30c531..3452821d552b2bb3e3a52947264a8a3dd921b49f 100644 (file)
@@ -123,7 +123,7 @@ P(search) (struct KP(context) *ctx, P(search_source_t) src
 #     endif
 
 #   ifdef KMPS_ADD_CONTROLS    
-    if (unlikely(s.eof))
+    if (s.eof)
       break;
 #   endif    
 
@@ -136,7 +136,7 @@ start_read: ;
 
     do
       {
-       if (unlikely(!KMPS_GET_CHAR(ctx, src, s)))
+       if (!KMPS_GET_CHAR(ctx, src, s))
          {
 #           ifdef KMPS_ADD_CONTROLS
            if (s.c != KP(control_char)())
index fa07f06035ea7d6b780128fa0f809fea2d516023..bee8d3fadf81ccbb04167540977459f8238d3e0a 100644 (file)
@@ -8,13 +8,26 @@
 #define TRACE(x...) do{}while(0)
 #endif
 
+/* TEST1 - multiple searches */
+
 #define KMP_PREFIX(x) GLUE_(kmp1,x)
 #define KMP_WANT_CLEANUP
-#define KMP_WANT_SEARCH
+#include "lib/kmp-new.h"
+#define KMPS_PREFIX(x) GLUE_(kmp1s1,x)
+#define KMPS_KMP_PREFIX(x) GLUE_(kmp1,x)
 #define KMPS_WANT_BEST
 #define KMPS_T uns
 #define KMPS_EXIT(ctx,src,s) do{ return s.best->len; }while(0)
-#include "lib/kmp-new.h"
+#include "lib/kmp-search.h"
+#define KMPS_PREFIX(x) GLUE_(kmp1s2,x)
+#define KMPS_KMP_PREFIX(x) GLUE_(kmp1,x)
+#define KMPS_EXTRA_VAR uns
+#define KMPS_INIT(ctx,src,s) do{ s.v = 0; }while(0)
+#define KMPS_T uns
+#define KMPS_FOUND(ctx,src,s) do{ s.v++; }while(0)
+#define KMPS_EXIT(ctx,src,s) do{ return s.v; }while(0)
+#define KMPS_WANT_BEST
+#include "lib/kmp-search.h"
 
 static void
 test1(void)
@@ -26,12 +39,16 @@ test1(void)
   kmp1_add(&ctx, "hoj");
   kmp1_add(&ctx, "aho");
   kmp1_build(&ctx);
-  UNUSED uns best = kmp1_search(&ctx, "asjlahslhalahosjkjhojsas");
+  UNUSED uns best = kmp1s1_search(&ctx, "asjlahslhalahosjkjhojsas");
   TRACE("Best match has %d characters", best);
   ASSERT(best == 3);
+  UNUSED uns count = kmp1s2_search(&ctx, "asjlahslhalahojsjkjhojsas");
+  ASSERT(count == 4);
   kmp1_cleanup(&ctx);
 }
 
+/* TEST2 - various tracing */
+
 #define KMP_PREFIX(x) GLUE_(kmp2,x)
 #define KMP_USE_UTF8
 #define KMP_TOLOWER
@@ -71,6 +88,8 @@ test2(void)
   kmp2_cleanup(&ctx);
 }
 
+/* TEST3 - random tests */
+
 #define KMP_PREFIX(x) GLUE_(kmp3,x)
 #define KMP_NODE uns
 #define KMP_ADD_EXTRA_ARGS uns index
@@ -130,11 +149,38 @@ test3(void)
   mp_delete(pool);
 }
 
+/* TEST4 - user-defined character type
+ * FIXME: it would need custom compare and hash functions to be really valid */
+
+#define KMP_PREFIX(x) GLUE_(kmp4,x)
+#define KMP_CHAR byte *
+#define KMP_CONTROL_CHAR NULL
+#define KMP_GET_CHAR(ctx,src,c) ({ c = src++; !!*c; })
+#define KMP_WANT_CLEANUP
+#define KMP_WANT_SEARCH
+#define KMPS_FOUND(ctx,src,s) do{ ASSERT(0); }while(0)
+#define KMPS_ADD_CONTROLS
+#define KMPS_MERGE_CONTROLS
+#include "lib/kmp-new.h"
+
+static void
+test4(void)
+{
+  log(L_INFO, "Running test4");
+  struct kmp4_context ctx;
+  kmp4_init(&ctx);
+  kmp4_add(&ctx, "ahoj");
+  kmp4_build(&ctx);
+  kmp4_search(&ctx, "djdhaskjdahoahaahojojshdaksjahdahojskj");
+  kmp4_cleanup(&ctx);
+}
+
 int
 main(void)
 {
   test1();
   test2();
   test3();
+  test4();
   return 0;
 }