Search code examples
chashset

I am using klib library Can i use (khash) KHASH_SET_INIT_INT64() negative values for key. Because i saw in header file its using unsigned long int


#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "klib/khash.h"

KHASH_SET_INIT_INT64(bin_value);

int counter = 1;

int main() {
  khash_t(bin_value) *bin_value_pointer = kh_init(bin_value);

  FILE *file = fopen("read.txt", "r");
  char line[20];
  while (fgets(line, sizeof(line), file)) {
    int64_t value;
    int ret;
    if (sscanf(line, "%ld", &value) == 1) {
      kh_put(bin_value, bin_value_pointer, value, &ret);
    }
  }
  fclose(file);

  FILE *file1 = fopen("check.txt", "r");
  while (fgets(line, sizeof(line), file1)) {
    int64_t input;
    int ret;
    if (sscanf(line, "%ld", &input) == 1) {
      if (kh_get(bin_value, bin_value_pointer, input) != kh_end(bin_value_pointer)) {
        printf("matched counter= %d\n", counter++);
      }
    }
  }

  kh_destroy(bin_value, bin_value_pointer);
  fclose(file1);
  return 0;
}

I am trying to implement hashSet using khash can i use negative value for key because i saw in header file it using unsigned long int (uint64_t).. and also if i use negative value for key it will cause any problem.. sorry for asking these type question but i am new to C.


Solution

  • i saw in header file it using unsigned long int

    You are probably using an old version:

    #if ULONG_MAX == ULLONG_MAX
    typedef unsigned long khint64_t;
    #else
    typedef unsigned long long khint64_t;
    #endif
    

    On modern versions khint64_t is defined as:

    typedef uint64_t khint64_t;
    

    if i use negative value for key it will cause any problem

    Your code is safe because conversions from signed to unsigned types has well defined behaviour, but that's not true for the opposite, from the standard:

    C11 6.3.1.3

    6.3.1.3 Signed and unsigned integers

    When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

    Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

    Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

    In consequence

    int64_t value;
    int ret;
    if (sscanf(line, "%ld", &value) == 1) {
      kh_put(bin_value, bin_value_pointer, value, &ret);
    }
    

    and

    int64_t input;
    ...
       if (kh_get(bin_value, bin_value_pointer, input) !=  != kh_end(bin_value_pointer)) {
    

    are both correct (except that int64_t wants "%" SCNd64 instead of "%ld"), what would not be correct is:

    int64_t value = kh_key(bin_value, bin_value_pointer);
    

    In this case there is an unsigned to signed conversion.