Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .cproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?>

<cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="0.1058516244">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="0.1058516244" moduleId="org.eclipse.cdt.core.settings" name="Default">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.VCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration buildProperties="" description="" id="0.1058516244" name="Default" parent="org.eclipse.cdt.build.core.prefbase.cfg">
<folderInfo id="0.1058516244." name="/" resourcePath="">
<toolChain id="org.eclipse.cdt.build.core.prefbase.toolchain.470909688" name="No ToolChain" resourceTypeBasedDiscovery="false" superClass="org.eclipse.cdt.build.core.prefbase.toolchain">
<targetPlatform id="org.eclipse.cdt.build.core.prefbase.toolchain.470909688.1368504229" name=""/>
<builder id="org.eclipse.cdt.build.core.settings.default.builder.1033929255" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="org.eclipse.cdt.build.core.settings.default.builder"/>
<tool id="org.eclipse.cdt.build.core.settings.holder.libs.913940441" name="holder for library settings" superClass="org.eclipse.cdt.build.core.settings.holder.libs"/>
<tool id="org.eclipse.cdt.build.core.settings.holder.1642021164" name="Assembly" superClass="org.eclipse.cdt.build.core.settings.holder">
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.747230842" languageId="org.eclipse.cdt.core.assembly" languageName="Assembly" sourceContentType="org.eclipse.cdt.core.asmSource" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool>
<tool id="org.eclipse.cdt.build.core.settings.holder.1979926564" name="GNU C++" superClass="org.eclipse.cdt.build.core.settings.holder">
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1913423411" languageId="org.eclipse.cdt.core.g++" languageName="GNU C++" sourceContentType="org.eclipse.cdt.core.cxxSource,org.eclipse.cdt.core.cxxHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool>
<tool id="org.eclipse.cdt.build.core.settings.holder.124573241" name="GNU C" superClass="org.eclipse.cdt.build.core.settings.holder">
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1307115393" languageId="org.eclipse.cdt.core.gcc" languageName="GNU C" sourceContentType="org.eclipse.cdt.core.cSource,org.eclipse.cdt.core.cHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool>
</toolChain>
</folderInfo>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="string_match.null.882191851" name="string_match"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="0.1058516244">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
</cproject>
27 changes: 27 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>string_match</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
53 changes: 53 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
string match
============

1. Learning the github co-operate development.
2. Learning the rabin_karp algorithm.


23 changes: 8 additions & 15 deletions brute_force.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
#include<stdio.h>

/*
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 <sys/time.h>

#include "string_match.h"
/*
Checks at each point if the needle is substring of haystack
*/
Expand All @@ -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
{
Expand All @@ -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 : <command> <pattern> <string>\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
Expand Down
27 changes: 11 additions & 16 deletions rabin_karp.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
#include<stdio.h>
#include<string.h>

#include <sys/time.h>

#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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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 : <command> <pattern> <string>\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
Expand Down
111 changes: 111 additions & 0 deletions string_match.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* string_match.c
*
* Created on: 2013-7-19
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <sys/time.h>
#include <unistd.h>

#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<sublen-2;i++)
{
sub[i]='a';
}
sub[sublen-2]='b';
sub[sublen-1]=0;


for(i=0;i<targetlen-2;i++)
{
target[i]='a';
}
target[targetlen-2]='b';
target[targetlen-1]=0;

return 0;
}
22 changes: 22 additions & 0 deletions string_match.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 getTimeDiffer(struct timeval start, struct timeval end);

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_ */
Loading