-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
104 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Generated by macro 'mkheader'. | ||
// This file is automatically generated, please do not modify it. | ||
#pragma once | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
#include "algorithm/base.h" | ||
#include "algorithm/bm.h" | ||
#include "algorithm/kmp.h" | ||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Generated by macro 'mkheader'. | ||
// This file is automatically generated, please do not modify it. | ||
#pragma once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#pragma once | ||
#include "base.h" | ||
|
||
static ssize_t bm_nsearch(cstr text, i32 textlen, cstr pattern, i32 patternlen) { | ||
i32 bc[256]; | ||
for (i32 i = 0; i < 256; i++) { | ||
bc[i] = patternlen; | ||
} | ||
for (i32 i = 0; i < patternlen - 1; i++) { | ||
bc[(byte)pattern[i]] = patternlen - i - 1; | ||
} | ||
|
||
for (i32 i = patternlen - 1; i < textlen;) { | ||
for (i32 j = patternlen - 1; text[i] == pattern[j]; i--, j--) { | ||
if (j == 0) return i; | ||
} | ||
i += bc[(byte)text[i]]; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
static ssize_t bm_search(cstr text, cstr pattern) { | ||
return bm_nsearch(text, strlen(text), pattern, strlen(pattern)); | ||
} | ||
|
||
static void *bm_nprepare(cstr pattern, i32 patternlen) { | ||
i32 *data = (i32 *)calloc(1 + PADDING_UP(patternlen, 4) / 4 + 256, 4); | ||
data[0] = patternlen; | ||
memcpy(data + 1, pattern, patternlen); | ||
i32 *bc = data + 1 + PADDING_UP(patternlen, 4) / 4; | ||
|
||
for (i32 i = 0; i < 256; i++) { | ||
bc[i] = patternlen; | ||
} | ||
for (i32 i = 0; i < patternlen - 1; i++) { | ||
bc[(byte)pattern[i]] = patternlen - i - 1; | ||
} | ||
|
||
return data; | ||
} | ||
|
||
static void *bm_prepare(cstr pattern) { | ||
return bm_nprepare(pattern, strlen(pattern)); | ||
} | ||
|
||
static ssize_t bm_run(cstr text, i32 textlen, const void *data) { | ||
i32 patternlen = *(const i32 *)data; | ||
cstr pattern = (cstr)data + 4; | ||
var bc = (const i32 *)data + 1 + PADDING_UP(patternlen, 4) / 4; | ||
|
||
for (i32 i = patternlen - 1; i < textlen;) { | ||
for (i32 j = patternlen - 1; text[i] == pattern[j]; i--, j--) { | ||
if (j == 0) return i; | ||
} | ||
i += bc[(byte)text[i]]; | ||
} | ||
|
||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
benchmark(c-rbtree rbtree-bench.cpp) | ||
gtest(c-rbtree rbtree-test.cpp) | ||
benchmark(c-avltree avltree-bench.cpp) | ||
testexec(string-search string-search.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define NO_STD 0 | ||
#include <algorithm.h> | ||
|
||
int main() { | ||
assert(kmp_search("hello, world!", "world") == 7); | ||
assert(kmp_search("hello, world!", "hello") == 0); | ||
assert(kmp_search("hello, world!", "lo") == 3); | ||
assert(kmp_search("hello, world!", "wl") == -1); | ||
assert(bm_search("hello, world!", "world") == 7); | ||
assert(bm_search("hello, world!", "hello") == 0); | ||
assert(bm_search("hello, world!", "lo") == 3); | ||
assert(bm_search("hello, world!", "wl") == -1); | ||
return 0; | ||
} |