Teacup rim text: Difference between revisions
Content added Content deleted
m (Minor edit to C code) |
(C - changed logic to match Rust) |
||
Line 119: | Line 119: | ||
} |
} |
||
char* dictionary_search(const GPtrArray* dictionary, const char* word) { |
|||
char** result = bsearch(&word, dictionary->pdata, dictionary->len, |
|||
sizeof(char*), string_compare); |
|||
return result != NULL ? *result : NULL; |
|||
} |
} |
||
void find_teacup_words(GPtrArray* dictionary) { |
void find_teacup_words(GPtrArray* dictionary) { |
||
GHashTable* found = |
GHashTable* found = g_hash_table_new(g_str_hash, g_str_equal); |
||
⚫ | |||
g_free, NULL); |
|||
⚫ | |||
GString* temp = g_string_sized_new(8); |
GString* temp = g_string_sized_new(8); |
||
for (size_t i = 0, n = dictionary->len; i < n; ++i) { |
for (size_t i = 0, n = dictionary->len; i < n; ++i) { |
||
char* word = g_ptr_array_index(dictionary, i); |
|||
size_t len = strlen(word); |
size_t len = strlen(word); |
||
if (len < 3 || g_hash_table_contains(found, word)) |
if (len < 3 || g_hash_table_contains(found, word)) |
||
Line 136: | Line 136: | ||
g_ptr_array_set_size(teacup_words, 0); |
g_ptr_array_set_size(teacup_words, 0); |
||
g_string_assign(temp, word); |
g_string_assign(temp, word); |
||
bool is_teacup_word = true; |
|||
for (size_t i = 0; i < len - 1; ++i) { |
for (size_t i = 0; i < len - 1; ++i) { |
||
rotate(temp->str, len); |
rotate(temp->str, len); |
||
char* w = dictionary_search(dictionary, temp->str); |
|||
if (w == NULL) { |
|||
is_teacup_word = false; |
|||
break; |
break; |
||
} |
|||
⚫ | |||
if (strcmp(word, w) != 0 && !g_ptr_array_find(teacup_words, w, NULL)) |
|||
⚫ | |||
} |
} |
||
if (teacup_words->len |
if (is_teacup_word && teacup_words->len > 0) { |
||
printf("%s", word); |
printf("%s", word); |
||
g_hash_table_add(found, |
g_hash_table_add(found, word); |
||
for (size_t i = 0; i < |
for (size_t i = 0; i < teacup_words->len; ++i) { |
||
char* teacup_word = g_ptr_array_index(teacup_words, i); |
|||
printf(" %s", teacup_word); |
printf(" %s", teacup_word); |
||
g_hash_table_add(found, |
g_hash_table_add(found, teacup_word); |
||
} |
} |
||
printf("\n"); |
printf("\n"); |