【问题标题】:Convert an array to a hash table or hash map将数组转换为哈希表或哈希映射
【发布时间】:2020-04-04 13:30:49
【问题描述】:

我有 2 个函数:smalltalk 和 do_smalltalk。 smalltalk 将检查用户输入(意图)是否与数组中给定的单词匹配。如果匹配,程序将转到do_smalltalk。我的问题是,如何将其更改为哈希表或哈希映射数据结构,以便优化对某个单词的搜索? smalltalk word 是 key,response 是 value。

smalltalk

int smalltalk(const char *intent)
{
// An array to store 9 smalltalk words
char *smalltalk[]= {"hi","hello","it","it's","that","that's","this","bye","goodbye"};

// Loop through the smalltalk array. Each index is a word.
for (int word = 0; word < 9; word++)
{
    // If user input matches a small talk word, return 1.
    if (strcmp(intent, smalltalk[word]) == 0)
    {
            return 1;
        }
    }

    return 0;

}
}

do_smalltalk

int do_smalltalk(int argc, char *argv[], char *response, int n)
{

    if (strcmp("hi",argv[0])==0  || strcmp("hello", argv[0]) == 0)
    {
        snprintf(response,n,"Hello");
    }
    else if (strcmp("it's",argv[0])==0 || strcmp("it",argv[0])==0)
    {
        snprintf(response,n,"Indeed it is.");
    }
    else if (strcmp("that's",argv[0])==0 || strcmp("that",argv[0])==0 || strcmp("this",argv[0])==0)
    {
        snprintf(response,n,"Yes, you're right.");
    }
    else if (strcmp("bye",argv[0])==0 || strcmp("goodbye",argv[0])==0)
    {
        snprintf(response,n,"Bye");
        return 1;
    }


    return 0;
}

【问题讨论】:

  • 了解散列并实现它。这里不方便解释。
  • 所以.. [hashmap strings c] 似乎是研究的合理术语(包括)。

标签: c arrays hashmap hashtable


【解决方案1】:

我用哈希表(双哈希算法)写了你的代码,享受吧:

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define HTSZ 16 /* must be 2^N */
#define ROL(x, n) (x << n) | (x >> (32 - n))

/*----------------------------------------------------------------------*/
typedef struct {
  const char *str;
  int n;
} ht_entry;

ht_entry htab[HTSZ]; // Assume 0-filled

/*----------------------------------------------------------------------*/
ht_entry *ht_lookup(const char *s) {
  uint32_t pos = 0xdead, step = 0xbeef;
  for(const char *p = s; *p; p++) {
    pos = ROL(step, 3) + *p;
    step = ROL(pos, 5) ^ *p;
  }
  step |= 1; // Odd step in 2^n table
  ht_entry *rc;
  do 
    rc = htab + ((pos += step) & (HTSZ - 1));
  while(rc->str != NULL && strcmp(rc->str, s) != 0);
  return rc;
} // ht_lookup

/*----------------------------------------------------------------------*/
void ht_init() {
  // An array to store 9 smalltalk words
  char *smalltalk[]= {"hi","hello","it","it's","that","that's","this","bye","goodbye"};

  // Loop through the smalltalk array. Each index is a word.
  for (int word = 0; word < 9; word++) {
    ht_entry *e = ht_lookup(smalltalk[word]);
    e->str = smalltalk[word];
    e->n   = word;
  }
} // ht_init

int ht_search(const char *s) {
   *strchr(s, '\n') = 0; // Assume \n always presents
   ht_entry *e = ht_lookup(s);
   return e->str == NULL? -1 : e->n;
} // ht_search

/*----------------------------------------------------------------------*/
int main(int argc, char **argv) {
  ht_init();
  char word[100];
  for( ; ; ) {
    printf("\n> "); fflush(stdout);
    switch(ht_search(fgets(word, sizeof(word), stdin))) {
      case 0:
      case 1:
        printf("Hello!\n");
        break;
      case 2:
      case 3:
        printf("Indeed it is.\n");
        break;
      case 7:
      case 8:
        return 0;
      default:
        printf("What?!");
        break;
    } // switch
  } // for
  return 0;
} // main

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 2019-05-23
    • 2011-01-16
    • 2012-10-29
    • 2011-12-10
    • 1970-01-01
    • 2018-10-13
    • 2012-08-05
    • 1970-01-01
    相关资源
    最近更新 更多