Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix indexer finishing too soon #23

Merged
merged 3 commits into from
Jun 29, 2019
Merged
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
13 changes: 13 additions & 0 deletions fist/dstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <stdlib.h>
#include <string.h>

#include "utils.h"

int dequals(dstring s1, dstring s2) {
return !strcmp(dtext(s1), dtext(s2));
}
Expand Down Expand Up @@ -296,3 +298,14 @@ int dfree(dstring string) {
free(string.text);
return string.length;
}

static int cmpdstringp(const void *pa, const void *pb) {
const dstring a = *(const dstring *) pa;
const dstring b = *(const dstring *) pb;
return strncmp(dtext(a), dtext(b), MIN(a.length, b.length));
}

dstringa dsorta(dstringa array) {
qsort(array.values, array.length, sizeof(dstring), cmpdstringp);
return array;
}
1 change: 1 addition & 0 deletions fist/dstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dstringa dcreatea(); // Create empty array of dstrings
dstringa dpush(dstringa array, dstring input); // Push dstring to list of dstrings
dstringa dremove(dstringa array, dstring input); // Remove item from dstring array
dstringa dpop(dstringa array); // Pop from stack
dstringa dsorta(dstringa array); // sort an array of dstrings
int dindexofa(dstringa array, dstring input); // Find index of dstring
dstringa dset(dstringa array, unsigned int index, dstring with); // Replace index in dstring array with another dstring
dstringa drange(dstringa array, int start, int end); // Return new array between two ranges
Expand Down
24 changes: 10 additions & 14 deletions fist/indexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,20 @@ dstringa indexer(dstring text, int max_phrase_length) {
dstringa words = dsplit(text, ' ');
dstringa index = dcreatea();

int max_length = MIN(max_phrase_length, words.length);
max_phrase_length = MIN(max_phrase_length, words.length);

for(int text_size = 0; text_size < words.length; text_size += max_phrase_length) {
for(int ahead = 0; ahead < max_length; ahead++) {
for(int i = text_size; i < max_length + text_size; i++) {
int check_len = max_length + text_size;
if(check_len > words.length)
check_len = words.length;
if(i + ahead < check_len) {
dstringa range = drange(words, i, i + ahead);
dstring joined = djoin(range, ' ');
index = dpush(index, joined);
dfreea(range);
dfree(joined);
}
for (int i = 0; i < words.length; i += max_phrase_length) {
for (int j = i; j < i + max_phrase_length; j++) {
for (int k = 0; k < MIN(words.length - j, max_phrase_length); k++) {
dstringa range = drange(words, j, j + k);
dstring joined = djoin(range, ' ');
index = dpush(index, joined);
dfreea(range);
dfree(joined);
}
}
}

dfreea(words);

return index;
Expand Down
3 changes: 3 additions & 0 deletions fist/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ static char *test_indexer() {
// indexer(t2, 10);
dstringa index = indexer(test, 10);

answers = dsorta(answers);
index = dsorta(index);

for(int i = 0; i < answers.length; i++) {
char *buffer = malloc(sizeof(char) * 1024);
sprintf(buffer, "indexer: Expecting '%s' got '%s' at %d", dtext(answers.values[i]),
Expand Down