From a42c56c82c87c552055431a028819eb4988337a6 Mon Sep 17 00:00:00 2001 From: "U-lee-Thinkpad\\lee" Date: Fri, 19 Jul 2013 11:08:20 +0800 Subject: [PATCH 1/3] pass compile --- brute_force.c | 2 +- rabin_karp.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/brute_force.c b/brute_force.c index a2d5858..544e4ee 100755 --- a/brute_force.c +++ b/brute_force.c @@ -40,7 +40,7 @@ int main(int argc, char **argv) printf("Failed to find %s in %s\n", needle, haystack); } } -#elif +#else if(argc < 3){ printf("Usage : \n"); return 0; diff --git a/rabin_karp.c b/rabin_karp.c index 9bb6233..757f6a3 100755 --- a/rabin_karp.c +++ b/rabin_karp.c @@ -88,7 +88,7 @@ int main(int argc, char **argv) printf("Failed to find %s in %s\n", needle, haystack); } } -#elif +#else if(argc < 3){ printf("Usage : \n"); return 0; From 471b9d5f84caa4384a6bea5ac1c4bb29b3eb502c Mon Sep 17 00:00:00 2001 From: "minye.li" Date: Fri, 19 Jul 2013 12:50:33 +0800 Subject: [PATCH 2/3] create a makefile for the test program --- .cproject | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ .project | 27 +++++++++++++++++++++++++ Makefile | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 7 +++++++ brute_force.c | 16 ++------------- rabin_karp.c | 18 ++++------------- string_match.c | 11 +++++++++++ string_match.h | 21 ++++++++++++++++++++ string_util.c | 22 +++++++++++++++++++++ 9 files changed, 198 insertions(+), 28 deletions(-) create mode 100644 .cproject create mode 100644 .project create mode 100644 Makefile create mode 100644 README.md create mode 100644 string_match.c create mode 100644 string_match.h create mode 100644 string_util.c diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..350b1fa --- /dev/null +++ b/.cproject @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..4ae0652 --- /dev/null +++ b/.project @@ -0,0 +1,27 @@ + + + string_match + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..722e462 --- /dev/null +++ b/Makefile @@ -0,0 +1,53 @@ +CFLAGS = -I. -Wall -g +LDFLAGS = +LIBS = + +ifeq ($(OSTYPE),linux) +EXE = +else +EXE = .exe +endif + +PRJECT_NAME=string_match + +all: $(PRJECT_NAME)$(EXE) + +CC=gcc + +.c.o: + @echo [CC]: $< + @$(CC) $(CFLAGS) -c $< -o $@ + +%.out: + @echo [OUT]: $^ + @$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@ + +%.exe: + @echo [EXE]: $^ + @$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(sources) $(LIBS) + +%.a: + @echo [AR]: $@ + @ar rcs $@ $^ + +%.d: %.c + @echo [DEP]: $< + @$(SHELL) -ec '$(CC) -MM -MQ $*.o $(CFLAGS) $< > $@' + + +sources = string_util.c brute_force.c rabin_karp.c string_match.c + +ifneq ($(MAKECMDGOALS),mrproper) +ifneq ($(MAKECMDGOALS),clean) +-include $(sources:.c=.d) +endif +endif + +$(PRJECT_NAME)$(EXE): $(sources:.c=.o) + +.PHONY: clean mrproper +clean: + rm -f *.o *.out, *$(EXE) + +mrproper: clean + rm -f *.d diff --git a/README.md b/README.md new file mode 100644 index 0000000..94b492b --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +string match +============ + +1. Learning the github co-operate development. +2. Learning the rabin_karp algorithm. + + diff --git a/brute_force.c b/brute_force.c index 544e4ee..66dec56 100755 --- a/brute_force.c +++ b/brute_force.c @@ -1,18 +1,6 @@ #include -/* - Checks if char* q starts with char* p - */ -int strstarts(const char *p, const char *q) -{ - while(*p && *p == *q){ - ++p; - ++q; - } - // if *p is null return true else return diff - return *p ? *p - *q: 0; -} - +#include "string_match.h" /* Checks at each point if the needle is substring of haystack */ @@ -26,7 +14,7 @@ int brute_force(const char *needle, const char *haystack) return -1; } -int main(int argc, char **argv) +int brute_force_test(int argc, char **argv) { #if DEBUG { diff --git a/rabin_karp.c b/rabin_karp.c index 757f6a3..e68302a 100755 --- a/rabin_karp.c +++ b/rabin_karp.c @@ -1,21 +1,11 @@ #include #include + +#include "string_match.h" + #define PRIME_BASE 39839 #define MOD 40000 -/* - Checks if char* q starts with char* p - */ -int strstarts(const char *p, const char *q) -{ - while(*p && *p == *q){ - ++p; - ++q; - } - // if *p is null return true else return diff - return *p ? *p - *q: 0; -} - int hash(const char *str, int len) { int i,h; @@ -74,7 +64,7 @@ int rabin_karp(const char *needle, const char *haystack) return -1; } -int main(int argc, char **argv) +int rabin_karp_test(int argc, char **argv) { #if DEBUG { diff --git a/string_match.c b/string_match.c new file mode 100644 index 0000000..d647648 --- /dev/null +++ b/string_match.c @@ -0,0 +1,11 @@ +/* + * string_match.c + * + * Created on: 2013-7-19 + */ +#include "string_match.h" + +int main(int argc, char **argv) +{ + return rabin_karp_test(argc, argv); +} diff --git a/string_match.h b/string_match.h new file mode 100644 index 0000000..01b8abc --- /dev/null +++ b/string_match.h @@ -0,0 +1,21 @@ +/* + * string_match.h + * + * Created on: 2013-7-19 + */ + +#ifndef STRING_MATCH_H_ +#define STRING_MATCH_H_ + +extern int strstarts(const char *p, const char *q); + +extern int brute_force(const char *needle, const char *haystack); +extern int brute_force_test(int argc, char **argv); + +extern int hash(const char *str, int len); +extern int rehash(int hash, char remove, char add, int maxpow); +extern int mod_pow(int base, int pow, int mod); +extern int rabin_karp(const char *needle, const char *haystack); +extern int rabin_karp_test(int argc, char **argv); + +#endif /* STRING_MATCH_H_ */ diff --git a/string_util.c b/string_util.c new file mode 100644 index 0000000..3d2e13b --- /dev/null +++ b/string_util.c @@ -0,0 +1,22 @@ +/* + * string_util.c + * + * Created on: 2013-7-19 + */ + +/* + Checks if char* q starts with char* p + */ +int strstarts(const char *p, const char *q) +{ + while(*p && *p == *q){ + ++p; + ++q; + } + // if *p is null return true else return diff + return *p ? *p - *q: 0; +} + + + + From 7616a69fd8775939b3ce87a68f791bd1379b74d6 Mon Sep 17 00:00:00 2001 From: "minye.li" Date: Fri, 19 Jul 2013 13:59:47 +0800 Subject: [PATCH 3/3] add test case --- brute_force.c | 7 +++- rabin_karp.c | 7 +++- string_match.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++- string_match.h | 1 + string_util.c | 17 ++++++++- 5 files changed, 130 insertions(+), 4 deletions(-) diff --git a/brute_force.c b/brute_force.c index 66dec56..67f70f2 100755 --- a/brute_force.c +++ b/brute_force.c @@ -1,5 +1,7 @@ #include +#include + #include "string_match.h" /* Checks at each point if the needle is substring of haystack @@ -35,7 +37,10 @@ int brute_force_test(int argc, char **argv) } int index = brute_force(argv[1], argv[2]); if(index >= 0) - printf("Found %s in %s at %d\n", argv[1], argv[2], index); + { + //printf("Found %s in %s at %d\n", argv[1], argv[2], index); + printf("Found in at %d\n", index); + } else printf("Failed to find %s in %s\n", argv[1], argv[2]); #endif diff --git a/rabin_karp.c b/rabin_karp.c index e68302a..81b5293 100755 --- a/rabin_karp.c +++ b/rabin_karp.c @@ -1,6 +1,8 @@ #include #include +#include + #include "string_match.h" #define PRIME_BASE 39839 @@ -85,7 +87,10 @@ int rabin_karp_test(int argc, char **argv) } int index = rabin_karp(argv[1], argv[2]); if(index >= 0) - printf("Found %s in %s at %d\n", argv[1], argv[2], index); + { + //printf("Found %s in %s at %d\n", argv[1], argv[2], index); + printf("Found in at %d\n", index); + } else printf("Failed to find %s in %s\n", argv[1], argv[2]); #endif diff --git a/string_match.c b/string_match.c index d647648..e756d66 100644 --- a/string_match.c +++ b/string_match.c @@ -3,9 +3,109 @@ * * Created on: 2013-7-19 */ + +#include +#include +#include + +#include +#include + #include "string_match.h" +int substring_testcase1(char* sub, int sublen, char* target, int targetlen); +int test_substringcase1(void); + int main(int argc, char **argv) { - return rabin_karp_test(argc, argv); + int rc; + +#if 1 + rc= test_substringcase1(); +#else + rc= rabin_karp_test(argc, argv); +#endif + + return rc; +} + +int test_substringcase1(void) +{ + char* sub; + int sublen; + char* target; + int targetlen; + int argc; + char* argv[3]; + + long mtime; + struct timeval start, end; + + /** + * string + */ + sublen = 1024; + sub = malloc (sublen); + if(sub == NULL) + return -1; + + targetlen = 1024*1024; + target = malloc (targetlen); + if(target == NULL) + { + free(sub); + return -2; + } + + substring_testcase1(sub, sublen, target, targetlen); + + argc=3; + argv[0]="brute_force"; + argv[1]=sub; + argv[2]=target; + + printf("test with %s\n", argv[0]); + gettimeofday(&start, NULL); + brute_force_test(argc, argv ); + gettimeofday(&end, NULL); + mtime=getTimeDiffer(start, end); + printf("done in %ld milliseconds\n", mtime); + + printf("test with %s\n", argv[0]); + gettimeofday(&start, NULL); + rabin_karp_test(argc, argv ); + gettimeofday(&end, NULL); + mtime=getTimeDiffer(start, end); + printf("done in %ld milliseconds\n", mtime); + + free(target); + free(sub); + + return 0; +} + +/** + * patten: aaab + * target: aaaaaaaaaaaaaaaaaab + */ +int substring_testcase1(char* sub, int sublen, char* target, int targetlen) +{ + int i; + + for(i=0;i +#include +#include + + /* Checks if char* q starts with char* p */ @@ -13,10 +18,20 @@ int strstarts(const char *p, const char *q) ++p; ++q; } - // if *p is null return true else return diff + // if *p is null return false else return diff return *p ? *p - *q: 0; } +int getTimeDiffer(struct timeval start, struct timeval end) +{ + long mtime, seconds, useconds; + + seconds = end.tv_sec - start.tv_sec; + useconds = end.tv_usec - start.tv_usec; + + mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; + return mtime; +}