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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iso646.h>
#include <string.h>
#include "elements.h"
const struct Element Elements[ 1+118 ] =
{
// This is our Periodic Table of Elements
// Indexed by element's atomic number
{0, "" },
{1, "H" },{2, "He"},{3, "Li"},{4, "Be"},{5, "B" },{6, "C" },{7, "N" },{8, "O" },
{9, "F" },{10, "Ne"},{11, "Na"},{12, "Mg"},{13, "Al"},{14, "Si"},{15, "P" },{16, "S" },
{17, "Cl"},{18, "Ar"},{19, "K" },{20, "Ca"},{21, "Sc"},{22, "Ti"},{23, "V" },{24, "Cr"},
{25, "Mn"},{26, "Fe"},{27, "Co"},{28, "Ni"},{29, "Cu"},{30, "Zn"},{31, "Ga"},{32, "Ge"},
{33, "As"},{34, "Se"},{35, "Br"},{36, "Kr"},{37, "Rb"},{38, "Sr"},{39, "Y" },{40, "Zr"},
{41, "Nb"},{42, "Mo"},{43, "Tc"},{44, "Ru"},{45, "Rh"},{46, "Pd"},{47, "Ag"},{48, "Cd"},
{49, "In"},{50, "Sn"},{51, "Sb"},{52, "Te"},{53, "I" },{54, "Xe"},{55, "Cs"},{56, "Ba"},
{57, "La"},{58, "Ce"},{59, "Pr"},{60, "Nd"},{61, "Pm"},{62, "Sm"},{63, "Eu"},{64, "Gd"},
{65, "Tb"},{66, "Dy"},{67, "Ho"},{68, "Er"},{69, "Tm"},{70, "Yb"},{71, "Lu"},{72, "Hf"},
{73, "Ta"},{74, "W" },{75, "Re"},{76, "Os"},{77, "Ir"},{78, "Pt"},{79, "Au"},{80, "Hg"},
{81, "Tl"},{82, "Pb"},{83, "Bi"},{84, "Po"},{85, "At"},{86, "Rn"},{87, "Fr"},{88, "Ra"},
{89, "Ac"},{90, "Th"},{91, "Pa"},{92, "U" },{93, "Np"},{94, "Pu"},{95, "Am"},{96, "Cm"},
{97, "Bk"},{98, "Cf"},{99, "Es"},{100,"Fm"},{101,"Md"},{102,"No"},{103,"Lr"},{104,"Rf"},
{105,"Db"},{106,"Sg"},{107,"Bh"},{108,"Hs"},{109,"Mt"},{110,"Ds"},{111,"Rg"},{112,"Cn"},
{113,"Nh"},{114,"Fl"},{115,"Mc"},{116,"Lv"},{117,"Ts"},{118,"Og"},
};
static
const unsigned char hash_to_element[ 216 ] =
{
// This is our hash table!
// (key, value) --> (hash, index into Elements[])
0,15,59,1,94,0,19,36,23,2,0,103,91,71,54,0,6,24,32,29,0,57,58,31,0,0,80,20,92,112,0,5,35,
0,84,0,67,4,72,97,0,7,56,0,25,0,115,10,0,27,0,98,11,0,12,0,16,38,0,93,0,68,34,63,42,0,78,
61,0,50,0,21,52,0,102,0,116,73,0,106,0,43,96,0,0,0,40,82,53,77,0,9,87,0,17,0,109,26,30,44,
0,49,75,0,108,0,8,88,0,86,0,39,18,0,79,0,3,55,0,111,0,74,62,0,46,0,89,41,0,118,0,104,69,0,
47,64,0,83,0,48,0,0,51,0,81,0,0,28,0,107,0,0,65,0,101,0,0,100,0,113,0,99,14,0,60,0,0,117,
0,114,0,0,22,0,0,0,85,95,0,0,0,0,37,0,90,0,0,105,0,13,0,0,66,0,0,0,0,70,0,0,0,0,110,0,0,0,
0,76,0,0,0,0,33,0,45
};
static
unsigned atomic_symbol_to_hash( const char * symbol )
{
// Returns a (potentially valid) hash value in 1..214 else a (definitely invalid) 0
static unsigned char xs[] =
{
215, 110, 30, 15, 100, 59, 90, 11, 2, 87, 215, 5, 9, 30, 40, 105,
0, 215, 95, 55, 65, 27, 7, 120, 7, 110, 84, 215, 215, 215, 215, 215,
215, 10, 85, 14, 122, 5, 34, 22, 117, 105, 215, 7, 77, 65, 12, 32,
17, 215, 0, 100, 64, 2, 65, 215, 215, 90, 215, 215, 215, 215, 215, 215,
};
unsigned hash = 0;
while (*symbol)
{
int x = (unsigned char) *symbol++;
hash += 1 + xs[ ((x & 192) == 64) ? (x & 63) : 0 ];
}
return hash < 215 ? hash : 0;
}
unsigned atomic_symbol_to_number( const char * symbol )
{
unsigned h = atomic_symbol_to_hash( symbol );
return h and strcmp( symbol, Elements[ hash_to_element[ h ] ].symbol ) == 0
? hash_to_element[ h ]
: 0;
}
const struct Element * atomic_symbol_to_element( const char * symbol )
{
unsigned h = atomic_symbol_to_hash( symbol );
return h and strcmp( symbol, Elements[ hash_to_element[ h ] ].symbol ) == 0
? (Elements + hash_to_element[ h ])
: NULL;
}
| |