-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackend.c
100 lines (81 loc) · 1.66 KB
/
backend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "backend.h"
#include <assert.h>
#include <string.h>
#include "dict.h"
/* search buffer */
static size_t g_forward_window = 8 * 1024;
void set_forward_window(size_t n)
{
g_forward_window = n;
}
size_t get_forward_window()
{
return g_forward_window;
}
/* found empirically */
static int g_max_match_count = 15;
void set_max_match_count(int n)
{
g_max_match_count = n;
}
int get_max_match_count()
{
return g_max_match_count;
}
static size_t g_factor1 = 4;
static size_t g_factor2 = 0;
size_t get_magic_factor1()
{
return g_factor1;
}
void set_magic_factor1(size_t factor)
{
g_factor1 = factor;
}
size_t get_magic_factor2()
{
return g_factor2;
}
void set_magic_factor2(size_t factor)
{
g_factor2 = factor;
}
size_t find_best_match(char *p)
{
size_t count[MAX_MATCH_LEN];
char *end = p + g_forward_window;
for (int i = 0; i < MAX_MATCH_LEN; ++i) {
count[i] = 0;
}
for (char *s = p + 1; s < end - MAX_MATCH_LEN; ++s) {
for (int i = 0; i < MAX_MATCH_LEN; ++i) {
if (p[i] == s[i]) {
count[i]++;
} else {
break;
}
}
}
for (int tc = g_max_match_count; tc > 0; --tc) {
for (int i = MAX_MATCH_LEN - 1; i >= 0; --i) {
if (count[i] > (size_t)tc) {
if (i >= 2 && g_factor1 > 0) {
if (dict_find_match(p + i) != (size_t)-1 && dict_get_len_by_index(dict_find_match(p + i)) * g_factor1 > (size_t)(i + 1)) {
goto next;
}
}
if (i >= 1 && g_factor2 > 0) {
for (int o = 1; o <= i; ++o) {
if (dict_find_match(p + o) != (size_t)-1 && ((int)dict_get_len_by_index(dict_find_match(p + o)) - o) * (int)g_factor2 > i + 1) {
goto next;
}
}
}
return i + 1;
}
next:
;
}
}
return 1;
}