Add the ability to store values into the table.

This commit is contained in:
Danila Fedorin 2018-04-26 20:14:21 -07:00
parent 785a362f97
commit edcd5fc5ae
2 changed files with 29 additions and 0 deletions

View File

@ -52,6 +52,7 @@ struct libab_table_entry_s {
libab_operator op; libab_operator op;
libab_function function; libab_function function;
libab_basetype* basetype; libab_basetype* basetype;
libab_ref value;
} data_u; } data_u;
}; };
@ -111,6 +112,15 @@ libab_function* libab_table_search_function(libab_table* table,
*/ */
libab_basetype* libab_table_search_basetype(libab_table* table, libab_basetype* libab_table_search_basetype(libab_table* table,
const char* string); const char* string);
/**
* Searches for the given value in the table.
* @param table the table to search.
* @param string the table entry key.
* @param ref the reference to store the result into.
*/
void libab_table_search_value(libab_table* table,
const char* string,
libab_ref* ref);
/** /**
* Stores the given entry in the table under the given key. * Stores the given entry in the table under the given key.
* @param table the table to store the entry into. * @param table the table to store the entry into.

View File

@ -77,6 +77,23 @@ libab_basetype* libab_table_search_basetype(libab_table* table,
return entry ? entry->data_u.basetype : NULL; return entry ? entry->data_u.basetype : NULL;
} }
int _table_compare_value(const void* left, const void* right) {
const libab_table_entry* entry = right;
return entry->variant == ENTRY_VALUE;
}
void libab_table_search_value(libab_table* table,
const char* string,
libab_ref* ref) {
libab_table_entry* entry =
libab_table_search_filter(table, string, NULL, _table_compare_value);
if(entry) {
libab_ref_copy(&entry->data_u.value, ref);
} else {
libab_ref_null(ref);
}
}
libab_result libab_table_put(libab_table* table, const char* string, libab_result libab_table_put(libab_table* table, const char* string,
libab_table_entry* entry) { libab_table_entry* entry) {
return libab_trie_put(&table->trie, string, entry); return libab_trie_put(&table->trie, string, entry);
@ -103,5 +120,7 @@ void libab_table_entry_free(libab_table_entry* entry) {
libab_function_free(&entry->data_u.function); libab_function_free(&entry->data_u.function);
} else if (entry->variant == ENTRY_BASETYPE) { } else if (entry->variant == ENTRY_BASETYPE) {
libab_basetype_free(entry->data_u.basetype); libab_basetype_free(entry->data_u.basetype);
} else if (entry->variant == ENTRY_VALUE) {
libab_ref_free(&entry->data_u.value);
} }
} }