states-tmpl.cc 922 字节
Newer Older
G
Grissiom 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

#include <string.h>

#include <iostream>

#include "states-code.hh"

static struct {
  char *name;
  char *abbr;
  int pop;
} states[$NK] = {
#include "states.dat.h"
};

static int T1[] = { $S1 };

static int T2[] = { $S2 };

static int G[] = { $G };

static int hash_g (const char *key, const int *T)
{
  int i, sum = 0;
  
  for (i = 0; key[i] != '\0'; i++) {
    sum += T[i] * key[i];
    sum %= $NG;
  }
  return G[sum];
}

static int perfect_hash (const char *key)
{
  if (strlen (key) > $NS)
    return 0;
  
  return (hash_g (key, T1) + hash_g (key, T2)) % $NG;
}

State::State (const string abbr)
{
  int hash_value = perfect_hash (abbr.c_str ());
  
  if (hash_value < $NK &&
      strcmp(abbr.c_str (), states[hash_value].abbr) == 0)
    {
      nam = states[hash_value].name;
      pop = states[hash_value].pop;
    }
  else
    {
      cerr << "'" << abbr << "' is not an abbreviation for a state\n";
    }
}