diff --git a/misc/keybind-collision-checker/collision.c b/misc/keybind-collision-checker/collision.c new file mode 100644 index 000000000..4d2eb709a --- /dev/null +++ b/misc/keybind-collision-checker/collision.c @@ -0,0 +1,71 @@ +#include +#include + +#include "../src/nnn.h" + +#define BUCKETS_ARRAY_SIZE 255 + +typedef struct bucket { + int sym; + struct bucket *next; +} bucket_t, *pbucket_t; + +static bool is_collision_in_bucket(pbucket_t buckets, int sym); +static void free_buckets(pbucket_t buckets); + +int main() { + bucket_t buckets[BUCKETS_ARRAY_SIZE] = {0}; + bool collision_detected = FALSE; + + for (int i = 0; i < sizeof(bindings) / sizeof(struct key); ++i) { + int curr_sym = bindings[i].sym; + pbucket_t curr_bucket = &buckets[curr_sym % BUCKETS_ARRAY_SIZE]; + + if (is_collision_in_bucket(curr_bucket, curr_sym)) { + printf("Collision of key %s detected\n", keyname(curr_sym)); + + collision_detected = TRUE; + } + } + + if (!collision_detected) + printf("No collisions detected\n"); + + free_buckets(buckets); +} + +bool is_collision_in_bucket(pbucket_t bucket, int sym) { + while (bucket) { + if (bucket->sym == sym) { + return TRUE; + } else if (bucket->sym == 0) { + bucket->sym = sym; + + break; + } + else if (!bucket->next) { + bucket->next = (pbucket_t)malloc(sizeof(bucket_t)); + bucket->next->sym = sym; + bucket->next->next = NULL; + + break; + } else { + bucket = bucket->next; + } + } + + return FALSE; +} + +void free_buckets(pbucket_t buckets) { + for (int i = 0; i < BUCKETS_ARRAY_SIZE; ++i) { + pbucket_t next = buckets[i].next; + + while (next) { + pbucket_t current = next; + next = current->next; + + free(current); + } + } +}