Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * dek.c: isSimilarElement implementation using Hash Table
- */
- #include "include/koeva-util-debug.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <uthash.h>
- struct int_hash {
- int id;
- UT_hash_handle hh;
- };
- struct int_hash * int_hash_create()
- {
- struct int_hash * new_row = malloc(sizeof(*new_row));
- ASSERT_MEM(new_row);
- return new_row;
- error:
- return NULL;
- }
- #define int_hash_add(htable, __id) \
- do { \
- typeof(htable) ____cursor = int_hash_create(); \
- ASSERT_MEM(____cursor); \
- ____cursor->id = __id; \
- HASH_ADD_INT(htable, id, ____cursor); \
- } while(0)
- #define int_hash_free(htable) \
- do { \
- typeof(htable) ____cursor, ____temp; \
- HASH_ITER(hh, htable, ____cursor, ____temp) { \
- HASH_DEL(htable, ____cursor); \
- free(____cursor); \
- } \
- } while(0)
- int main(int argc, char **argv)
- {
- ASSERT(argc > 2, "USAGE : ./dek <input-file-arr1> <input-file-arr2>");
- int temp;
- int ret_flag = 1;
- FILE *in_arr1_file;
- FILE *in_arr2_file;
- struct int_hash *table = NULL;
- struct int_hash *cursor;
- in_arr1_file = fopen(argv[1], "r");
- ASSERT(in_arr1_file, "Could not locate the file: %s\n", argv[1]);
- in_arr2_file = fopen(argv[2], "r");
- ASSERT(in_arr2_file, "Could not locate the file: %s\n", argv[2]);
- fscanf(in_arr1_file, "%d, ", &temp);
- int_hash_add(table, temp);
- while (fscanf(in_arr1_file, "%d, ", &temp) != EOF) {
- HASH_FIND_INT(table, &temp, cursor);
- if(!cursor)
- int_hash_add(table, temp);
- }
- while (fscanf(in_arr2_file, "%d, ", &temp) != EOF) {
- HASH_FIND_INT(table, &temp, cursor);
- if(cursor) {
- ret_flag = 0;
- break;
- }
- }
- printf("COMMON VALUE : %d\n", temp);
- if (ret_flag)
- printf("false\n");
- else
- printf("true\n");
- int_hash_free(table);
- fclose(in_arr1_file);
- fclose(in_arr2_file);
- return 0;
- error:
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement