-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a basic keybinding collision checker. (ToDo list jarun#337)
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |