diff --git a/CMakeLists.txt b/CMakeLists.txt index e3bf3b4..80378c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,6 @@ add_custom_command( set(SOURCES tkeyboard.c tpointer.c - hashtable.c toml.c config.c grid.c @@ -91,25 +90,7 @@ target_compile_options(wlgol PRIVATE -add_executable(test_hashtable tests/test_hashtable.c - ${SOURCES} -) -target_link_libraries(test_hashtable - wayland-client - wayland-cursor - ${XKBCOMMON_LIBRARIES} -) -target_include_directories(test_hashtable - PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include - PUBLIC ${XKBCOMMON_INCLUDE_DIRS} -) -target_compile_options(test_hashtable PRIVATE - ${XKBCOMMON_CFLAGS_OTHER} -) - enable_testing() -add_test(NAME Hashtable COMMAND test_hashtable) add_executable(test_config tests/test_config.c ${SOURCES} diff --git a/config.c b/config.c index 55371f2..49c18df 100644 --- a/config.c +++ b/config.c @@ -49,7 +49,6 @@ int config_parse_mods(const char *s, uint32_t *out_mods, char **out_key) { if (!copy) return -1; /* count tokens */ int n = 0; - char *p = copy; /* first pass count */ char *tmp = strdup(s); char *save = NULL; diff --git a/hashtable.c b/hashtable.c deleted file mode 100644 index c651fdb..0000000 --- a/hashtable.c +++ /dev/null @@ -1,203 +0,0 @@ -// implementation of a hashtable - -#include -#include -#include - -#include "hashtable.h" - - - -// entry in the hashtable -struct entry -{ - const char *key; - void *value; - struct entry *next; -}; - -struct htobj -{ - int size; - struct entry **table; -}; - -struct item { - struct entry *prev, *found; - int index; -}; - -static unsigned int hash(const char *key) -{ - unsigned int hash = 0; - - while (*key) - { - hash = (hash << 5) - hash + *key++; - } - return hash; -} - -hashtable hashtable_new(int size) -{ - // Allocate memory for the hashtable - hashtable h = malloc(sizeof(struct htobj)); - - // Set the size of the hashtable - h->size = size; - - // Allocate memory for the entry pointers - h->table = calloc(size, sizeof(struct entry *)); - - // Return the newly created hashtable - return h; -} - - -struct item hashtable_lookup_item(hashtable h, const char *key) -{ - struct item result = { - .found = NULL, - .index = -1, - }; - - int index = hash(key) % h->size; - struct entry *e = h->table[index]; - struct entry *prev = NULL; - - while (e != NULL) { - if (strcmp(e->key, key) == 0) { - result.index = index; - result.found = e; - result.prev = prev; - return result; - } - prev = e; - e = e->next; - } - return result; // empty in this case -} - -void *hashtable_set(hashtable h, const char *key, void *value) -{ - - struct item item = hashtable_lookup_item(h, key); - // not found - insert new - if (item.found == NULL) { - int index = hash(key) % h->size; - - // Create a new entry - struct entry *e = malloc(sizeof(struct entry)); - e->key = strdup(key); // key is the copy - e->value = value; - e->next = NULL; - - // Insert the entry at the beginning of the linked list - e->next = h->table[index]; - h->table[index] = e; - return NULL; - } - // found replace the value - void *old_value = item.found->value; - item.found->value = value; - - return old_value; -} - -void *hashtable_lookup(hashtable h, const char *key) -{ - struct item item = hashtable_lookup_item(h, key); - if (item.found != NULL) { - return item.found->value; - } - return NULL; -} - -void *hashtable_remove(hashtable h, const char *key) -{ - struct item item = hashtable_lookup_item(h, key); - if (item.found == NULL) { - return NULL; - } - // found, so detete it - if (item.prev != NULL) { - item.prev->next = item.found->next; - } else { - h->table[item.index] = item.found->next; - } - // free unused memory - void *value = item.found->value; // remember value - free((void*) item.found->key); // free key - free((void*) item.found); // free entry - - return value; -} - -void hashtable_destroy_bucket(hashtable h, int i) -{ - struct entry *e = h->table[i]; - while (e != NULL) { - struct entry *next = e->next; - free((void*)e->key); - free(e); - e = next; - } -} - -void hashtable_destroy(hashtable h) -{ - if (h == NULL) return; - for (int i = 0; i < h->size; i++) { - hashtable_destroy_bucket(h, i); - } - free(h->table); - free(h); -} - -ht_iterator hashtable_iter(hashtable h) -{ - ht_iterator iter = { - .h = h, - .index = -1, - .entry = NULL, - }; - - return iter; -} - -bool ht_iter_next(ht_iterator *iter) -{ - if (iter == NULL) { - return false; - } - hashtable h = iter->h; - - while (true) { - if (iter->index >= h->size) { - return false; - } - if (iter->entry == NULL) { - iter->index++; - if (iter->index >= h->size) { - return false; - } - iter->entry = h->table[iter->index]; - } else { - iter->entry = iter->entry->next; - } - if (iter->entry != NULL) return true; - } - -} - -const char *ht_iter_key(ht_iterator *iter) -{ - if (iter == NULL || iter->entry == NULL) return NULL; - return iter->entry->key; -} - -void *ht_iter_value(ht_iterator *iter) -{ - if (iter == NULL || iter->entry == NULL) return NULL; - return iter->entry->value; -} diff --git a/include/hashtable.h b/include/hashtable.h deleted file mode 100644 index 7e374a2..0000000 --- a/include/hashtable.h +++ /dev/null @@ -1,116 +0,0 @@ -#ifndef HASHTABLE_H -#define HASHTABLE_H - -#include - -struct htobj; -typedef struct htobj *hashtable; - -typedef struct { - struct htobj *h; - struct entry *entry; - int index; -} ht_iterator; - -/** - * @brief Create a new hashtable - * - * This function creates a new hashtable with the specified size. - * It initializes the table with NULL entries. - * - * @param size The size of the hashtable - * @return A pointer to the newly created hashtable - */ -hashtable hashtable_new(int size); - -/** - * @brief Insert a new key-value pair into the hashtable - * - * This function inserts a new key-value pair into the hashtable. - * It calculates the index using the hash function and inserts the - * key-value pair at the beginning of the linked list at that index. - * - * @param h Pointer to the hashtable - * @param key Pointer to the key - * @param value Pointer to the value - * @return null or pointer to the previous value of the key - */ -void *hashtable_set(hashtable h, const char *key, void *value); - -/** - * @brief Get the value associated with a key from the hashtable - * - * This function takes a hashtable and a key as input and returns the - * value associated with the key. If the key is not found in the - * hashtable, it returns NULL. - * - * @param h Pointer to the hashtable - * @param key Pointer to the key - * @return Pointer to the value associated with the key, or NULL if not found - */ -void *hashtable_lookup(hashtable h, const char *key); - -/** - * @brief Remove a key-value pair from the hashtable - * - * This function removes a key-value pair from the hashtable. - * It calculates the index using the hash function and traverses - * the linked list at that index. If the key is found, it removes - * the corresponding entry from the linked list and frees the memory. - * - * @param h Pointer to the hashtable - * @param key Pointer to the key to remove - * @return null or value associated with the removed key. - */ -void *hashtable_remove(hashtable h, const char *key); - -/** - * @brief Destroy hashtable. - * - * Values must be destroyed by the caller earlier. - * - * @param h Reference to the hastable. - */ -void hashtable_destroy(hashtable h); - -/** - * @brief Get the iterator of key-value pairs in the hashtable - * - * This function returns the iterator of key-value pairs in the hashtable. - * - * @param h Pointer to the hashtable - * @return The iterator of key-value pairs in the hashtable - */ -ht_iterator hashtable_iter(hashtable h); - -/** - * @brief Get the next key-value pair from the iterator - * - * This function returns the next key-value pair from the iterator. - * - * @param iter Pointer to the iterator - * @return true if there is a next key-value pair, false otherwise - */ -bool ht_iter_next(ht_iterator *iter); - -/** - * @brief Get the key from the iterator - * - * This function returns the key from the iterator. - * - * @param iter Pointer to the iterator - * @return The key from the iterator - */ -const char *ht_iter_key(ht_iterator *iter); - -/** - * @brief Get the value from the iterator - * - * This function returns the value from the iterator. - * - * @param iter Pointer to the iterator - * @return The value from the iterator - */ -void *ht_iter_value(ht_iterator *iter); - -#endif diff --git a/tests/test_hashtable.c b/tests/test_hashtable.c deleted file mode 100644 index 286bf67..0000000 --- a/tests/test_hashtable.c +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include -#include -#include - -#include - -void test_hashtable(int size) { - hashtable h = hashtable_new(size); - void *value1 = "to jest jakaś tam wartość"; - void *value2 = "I inna"; - void *value3 = "trzecia"; - void *value4 = "Czwarta"; - void *value5 = "Piąta"; - - void *retval = hashtable_set(h, "jeden", value1); - assert(retval == NULL); - - void *found = hashtable_lookup(h, "jeden"); - assert(found != NULL && !strcmp(value1, found)); - - retval = hashtable_set(h, "dwa", value2); - assert(retval == NULL); - - retval = hashtable_set(h, "trzy", value3); - assert(retval == NULL); - - retval = hashtable_set(h, "cztery", value4); - assert(retval == NULL); - - retval = hashtable_set(h, "jeden", value5); - assert(retval != NULL); - assert (retval == value1); - - retval = hashtable_lookup(h, "niema"); - assert(retval == NULL); - - retval = hashtable_lookup(h, "jeden"); - assert(retval == value5); - - ht_iterator iter = hashtable_iter(h); - - while (ht_iter_next(&iter)) { - printf("Key %s, value: %s\n", ht_iter_key(&iter), (const char*) ht_iter_value(&iter)); - } - - - retval = hashtable_remove(h, "cztery"); - assert (retval == value4); - retval = hashtable_remove(h, "trzy"); - assert(retval == value3); - retval = hashtable_remove(h, "dwa"); - assert(retval == value2); - retval = hashtable_remove(h, "jeden"); - assert(retval == value5); - - hashtable_destroy(h); -} - -int main() { - test_hashtable(2); - test_hashtable(10); -} diff --git a/wlgol.c b/wlgol.c index ad8fa85..cc4abaf 100644 --- a/wlgol.c +++ b/wlgol.c @@ -30,7 +30,6 @@ struct wl_shm *shm; struct xdg_wm_base *xdg_shell; struct wl_seat *wl_seat; struct wl_output *wl_output = NULL; -int output_scale_factor = 1; int output_width = 0; int output_height = 0; @@ -95,7 +94,6 @@ static void output_mode_handler(void *data, struct wl_output *output, uint32_t f static void output_done_handler(void *data, struct wl_output *output) { (void)data;(void)output; } static void output_scale_handler(void *data, struct wl_output *output, int32_t factor) { (void)data;(void)output; - output_scale_factor = factor; if (!fractional_manager) { if (factor > 0) current_scale = factor; need_resize = 1; @@ -502,8 +500,6 @@ int main(void) wl_display_disconnect(display); } -unsigned char mycolour = 0; -char direction = 1; int framecounter = 0; static void fill_rect(int x0, int y0, int w, int h, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {