mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-22 16:18:28 -05:00
LCS -> STRALGO LCS.
STRALGO should be a container for mostly read-only string algorithms in Redis. The algorithms should have two main characteristics: 1. They should be non trivial to compute, and often not part of programming language standard libraries. 2. They should be fast enough that it is a good idea to have optimized C implementations. Next thing I would love to see? A small strings compression algorithm.
This commit is contained in:
parent
57a0c9c98d
commit
8a7f255cd0
@ -1006,7 +1006,7 @@ struct redisCommand redisCommandTable[] = {
|
|||||||
"admin no-script no-slowlog ok-loading ok-stale",
|
"admin no-script no-slowlog ok-loading ok-stale",
|
||||||
0,NULL,0,0,0,0,0,0},
|
0,NULL,0,0,0,0,0,0},
|
||||||
|
|
||||||
{"lcs",lcsCommand,-4,
|
{"stralgo",stralgoCommand,-2,
|
||||||
"write use-memory @string",
|
"write use-memory @string",
|
||||||
0,lcsGetKeys,0,0,0,0,0,0}
|
0,lcsGetKeys,0,0,0,0,0,0}
|
||||||
};
|
};
|
||||||
|
@ -2389,7 +2389,7 @@ void xdelCommand(client *c);
|
|||||||
void xtrimCommand(client *c);
|
void xtrimCommand(client *c);
|
||||||
void lolwutCommand(client *c);
|
void lolwutCommand(client *c);
|
||||||
void aclCommand(client *c);
|
void aclCommand(client *c);
|
||||||
void lcsCommand(client *c);
|
void stralgoCommand(client *c);
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
|
void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
|
||||||
|
@ -480,18 +480,31 @@ void strlenCommand(client *c) {
|
|||||||
addReplyLongLong(c,stringObjectLen(o));
|
addReplyLongLong(c,stringObjectLen(o));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* LCS -- Longest common subsequence.
|
|
||||||
|
/* STRALGO -- Implement complex algorithms on strings.
|
||||||
*
|
*
|
||||||
* LCS [IDX] [MINMATCHLEN <len>]
|
* STRALGO <algorithm> ... arguments ... */
|
||||||
* STRINGS <string> <string> | KEYS <keya> <keyb> */
|
void stralgoLCS(client *c); /* This implements the LCS algorithm. */
|
||||||
void lcsCommand(client *c) {
|
void stralgoCommand(client *c) {
|
||||||
|
/* Select the algorithm. */
|
||||||
|
if (!strcasecmp(c->argv[1]->ptr,"lcs")) {
|
||||||
|
stralgoLCS(c);
|
||||||
|
} else {
|
||||||
|
addReply(c,shared.syntaxerr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* STRALGO <algo> [IDX] [MINMATCHLEN <len>] [WITHMATCHLEN]
|
||||||
|
* STRINGS <string> <string> | KEYS <keya> <keyb>
|
||||||
|
*/
|
||||||
|
void stralgoLCS(client *c) {
|
||||||
uint32_t i, j;
|
uint32_t i, j;
|
||||||
long long minmatchlen = 0;
|
long long minmatchlen = 0;
|
||||||
sds a = NULL, b = NULL;
|
sds a = NULL, b = NULL;
|
||||||
int getlen = 0, getidx = 0, withmatchlen = 0;
|
int getlen = 0, getidx = 0, withmatchlen = 0;
|
||||||
robj *obja = NULL, *objb = NULL;
|
robj *obja = NULL, *objb = NULL;
|
||||||
|
|
||||||
for (j = 1; j < (uint32_t)c->argc; j++) {
|
for (j = 2; j < (uint32_t)c->argc; j++) {
|
||||||
char *opt = c->argv[j]->ptr;
|
char *opt = c->argv[j]->ptr;
|
||||||
int moreargs = (c->argc-1) - j;
|
int moreargs = (c->argc-1) - j;
|
||||||
|
|
||||||
|
@ -424,29 +424,29 @@ start_server {tags {"string"}} {
|
|||||||
set rna2 {ATTAAAGGTTTATACCTTCCCAGGTAACAAACCAACCAACTTTCGATCTCTTGTAGATCTGTTCTCTAAACGAACTTTAAAATCTGTGTGGCTGTCACTCGGCTGCATGCTTAGTGCACTCACGCAGTATAATTAATAACTAATTACTGTCGTTGACAGGACACGAGTAACTCGTCTATCTTCTGCAGGCTGCTTACGGTTTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAGGTTT}
|
set rna2 {ATTAAAGGTTTATACCTTCCCAGGTAACAAACCAACCAACTTTCGATCTCTTGTAGATCTGTTCTCTAAACGAACTTTAAAATCTGTGTGGCTGTCACTCGGCTGCATGCTTAGTGCACTCACGCAGTATAATTAATAACTAATTACTGTCGTTGACAGGACACGAGTAACTCGTCTATCTTCTGCAGGCTGCTTACGGTTTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAGGTTT}
|
||||||
set rnalcs {ACCTTCCCAGGTAACAAACCAACCAACTTTCGATCTCTTGTAGATCTGTTCTCTAAACGAACTTTAAAATCTGTGTGGCTGTCACTCGGCTGCATGCTTAGTGCACTCACGCAGTATAATTAATAACTAATTACTGTCGTTGACAGGACACGAGTAACTCGTCTATCTTCTGCAGGCTGCTTACGGTTTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAGGTTT}
|
set rnalcs {ACCTTCCCAGGTAACAAACCAACCAACTTTCGATCTCTTGTAGATCTGTTCTCTAAACGAACTTTAAAATCTGTGTGGCTGTCACTCGGCTGCATGCTTAGTGCACTCACGCAGTATAATTAATAACTAATTACTGTCGTTGACAGGACACGAGTAACTCGTCTATCTTCTGCAGGCTGCTTACGGTTTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAGGTTT}
|
||||||
|
|
||||||
test {LCS string output with STRINGS option} {
|
test {STRALGO LCS string output with STRINGS option} {
|
||||||
r LCS STRINGS $rna1 $rna2
|
r STRALGO LCS STRINGS $rna1 $rna2
|
||||||
} $rnalcs
|
} $rnalcs
|
||||||
|
|
||||||
test {LCS len} {
|
test {STRALGO LCS len} {
|
||||||
r LCS LEN STRINGS $rna1 $rna2
|
r STRALGO LCS LEN STRINGS $rna1 $rna2
|
||||||
} [string length $rnalcs]
|
} [string length $rnalcs]
|
||||||
|
|
||||||
test {LCS with KEYS option} {
|
test {LCS with KEYS option} {
|
||||||
r set virus1 $rna1
|
r set virus1 $rna1
|
||||||
r set virus2 $rna2
|
r set virus2 $rna2
|
||||||
r LCS KEYS virus1 virus2
|
r STRALGO LCS KEYS virus1 virus2
|
||||||
} $rnalcs
|
} $rnalcs
|
||||||
|
|
||||||
test {LCS indexes} {
|
test {LCS indexes} {
|
||||||
dict get [r LCS IDX KEYS virus1 virus2] matches
|
dict get [r STRALGO LCS IDX KEYS virus1 virus2] matches
|
||||||
} {{{238 238} {239 239}} {{236 236} {238 238}} {{229 230} {236 237}} {{224 224} {235 235}} {{1 222} {13 234}}}
|
} {{{238 238} {239 239}} {{236 236} {238 238}} {{229 230} {236 237}} {{224 224} {235 235}} {{1 222} {13 234}}}
|
||||||
|
|
||||||
test {LCS indexes with match len} {
|
test {LCS indexes with match len} {
|
||||||
dict get [r LCS IDX KEYS virus1 virus2 WITHMATCHLEN] matches
|
dict get [r STRALGO LCS IDX KEYS virus1 virus2 WITHMATCHLEN] matches
|
||||||
} {{{238 238} {239 239} 1} {{236 236} {238 238} 1} {{229 230} {236 237} 2} {{224 224} {235 235} 1} {{1 222} {13 234} 222}}
|
} {{{238 238} {239 239} 1} {{236 236} {238 238} 1} {{229 230} {236 237} 2} {{224 224} {235 235} 1} {{1 222} {13 234} 222}}
|
||||||
|
|
||||||
test {LCS indexes with match len and minimum match len} {
|
test {LCS indexes with match len and minimum match len} {
|
||||||
dict get [r LCS IDX KEYS virus1 virus2 WITHMATCHLEN MINMATCHLEN 5] matches
|
dict get [r STRALGO LCS IDX KEYS virus1 virus2 WITHMATCHLEN MINMATCHLEN 5] matches
|
||||||
} {{{1 222} {13 234} 222}}
|
} {{{1 222} {13 234} 222}}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user