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 a2d5858..67f70f2 100755 --- a/brute_force.c +++ b/brute_force.c @@ -1,18 +1,8 @@ #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 +#include "string_match.h" /* Checks at each point if the needle is substring of haystack */ @@ -26,7 +16,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 { @@ -40,14 +30,17 @@ 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; } 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 9bb6233..81b5293 100755 --- a/rabin_karp.c +++ b/rabin_karp.c @@ -1,21 +1,13 @@ #include #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 +66,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 { @@ -88,14 +80,17 @@ 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; } 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 new file mode 100644 index 0000000..e756d66 --- /dev/null +++ b/string_match.c @@ -0,0 +1,111 @@ +/* + * string_match.c + * + * 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) +{ + 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 + */ +int strstarts(const char *p, const char *q) +{ + while(*p && *p == *q){ + ++p; + ++q; + } + // 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; +} +