Skip to content

Commit

Permalink
Implement a basic keybinding collision checker. (ToDo list jarun#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
annagrram committed Oct 10, 2019
1 parent f72cf4c commit 462d1d3
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions misc/keybind-collision-checker/collision.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <stdio.h>
#include <stdlib.h>

#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);
}
}
}

0 comments on commit 462d1d3

Please sign in to comment.