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:
}
}


bool dictionary_search(const GPtrArray* dictionary, const char* word) {
char* dictionary_search(const GPtrArray* dictionary, const char* word) {
return bsearch(&word, dictionary->pdata, dictionary->len, sizeof(char*),
char** result = bsearch(&word, dictionary->pdata, dictionary->len,
string_compare) != NULL;
sizeof(char*), string_compare);
return result != NULL ? *result : NULL;
}
}


void find_teacup_words(GPtrArray* dictionary) {
void find_teacup_words(GPtrArray* dictionary) {
GHashTable* found = g_hash_table_new_full(g_str_hash, g_str_equal,
GHashTable* found = g_hash_table_new(g_str_hash, g_str_equal);
GPtrArray* teacup_words = g_ptr_array_new();
g_free, NULL);
GPtrArray* teacup_words = g_ptr_array_new_full(8, g_free);
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) {
const char* word = g_ptr_array_index(dictionary, 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);
if (strcmp(word, temp->str) == 0
char* w = dictionary_search(dictionary, temp->str);
|| !dictionary_search(dictionary, temp->str))
if (w == NULL) {
is_teacup_word = false;
break;
break;
}
g_ptr_array_add(teacup_words, g_strdup(temp->str));
if (strcmp(word, w) != 0 && !g_ptr_array_find(teacup_words, w, NULL))
g_ptr_array_add(teacup_words, w);
}
}
if (teacup_words->len == len - 1) {
if (is_teacup_word && teacup_words->len > 0) {
printf("%s", word);
printf("%s", word);
g_hash_table_add(found, g_strdup(word));
g_hash_table_add(found, word);
for (size_t i = 0; i < len - 1; ++i) {
for (size_t i = 0; i < teacup_words->len; ++i) {
const char* teacup_word = g_ptr_array_index(teacup_words, 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_strdup(teacup_word));
g_hash_table_add(found, teacup_word);
}
}
printf("\n");
printf("\n");