Advertisement
_fur

_fur | C Darius Ellert Klaus

Feb 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. /*
  2.  * dek.c: isSimilarElement implementation using Hash Table
  3.  */
  4.  
  5. #include "include/koeva-util-debug.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <uthash.h>
  9.  
  10. struct int_hash {
  11.         int id;
  12.         UT_hash_handle hh;
  13. };
  14.  
  15. struct int_hash * int_hash_create()
  16. {
  17.         struct int_hash * new_row = malloc(sizeof(*new_row));
  18.         ASSERT_MEM(new_row);
  19.  
  20.         return new_row;
  21.  
  22. error:
  23.         return NULL;
  24. }
  25.  
  26. #define int_hash_add(htable, __id) \
  27.         do { \
  28.                 typeof(htable) ____cursor = int_hash_create(); \
  29.                 ASSERT_MEM(____cursor); \
  30.                 ____cursor->id = __id; \
  31.                 HASH_ADD_INT(htable, id, ____cursor); \
  32.         } while(0)
  33.  
  34. #define int_hash_free(htable) \
  35.         do { \
  36.                 typeof(htable) ____cursor, ____temp; \
  37.                 HASH_ITER(hh, htable, ____cursor, ____temp) { \
  38.                         HASH_DEL(htable, ____cursor); \
  39.                         free(____cursor); \
  40.                 } \
  41.         } while(0)
  42.  
  43. int main(int argc, char **argv)
  44. {
  45.  
  46.         ASSERT(argc > 2, "USAGE : ./dek <input-file-arr1> <input-file-arr2>");
  47.  
  48.         int temp;
  49.         int ret_flag = 1;
  50.         FILE *in_arr1_file;
  51.         FILE *in_arr2_file;
  52.         struct int_hash *table = NULL;
  53.         struct int_hash *cursor;
  54.  
  55.         in_arr1_file = fopen(argv[1], "r");
  56.         ASSERT(in_arr1_file, "Could not locate the file: %s\n", argv[1]);
  57.  
  58.         in_arr2_file = fopen(argv[2], "r");
  59.         ASSERT(in_arr2_file, "Could not locate the file: %s\n", argv[2]);
  60.  
  61.         fscanf(in_arr1_file, "%d, ", &temp);
  62.         int_hash_add(table, temp);
  63.  
  64.         while (fscanf(in_arr1_file, "%d, ", &temp) != EOF) {
  65.                 HASH_FIND_INT(table, &temp, cursor);
  66.                 if(!cursor)
  67.                         int_hash_add(table, temp);
  68.         }
  69.  
  70.         while (fscanf(in_arr2_file, "%d, ", &temp) != EOF) {
  71.                 HASH_FIND_INT(table, &temp, cursor);
  72.                 if(cursor) {
  73.                         ret_flag = 0;
  74.                         break;
  75.                 }
  76.         }
  77.  
  78.         printf("COMMON VALUE : %d\n", temp);
  79.  
  80.         if (ret_flag)
  81.                 printf("false\n");
  82.         else
  83.                 printf("true\n");
  84.  
  85.         int_hash_free(table);
  86.         fclose(in_arr1_file);
  87.         fclose(in_arr2_file);
  88.  
  89.         return 0;
  90. error:
  91.         return -1;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement