codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
/* hash_switch.d Perfect hashing used to implement string switches in D. Version 1.1, Jan 27 2010 --------------- Benchmark constants: NDATA=10_000, NLOOPS=4_000, random seed=10 Compiled with: LDC based on DMD v1.051 and llvm 2.6 (Fri Nov 27 12:54:12 2009) DMD Digital Mars D Compiler v1.042 ldc -O3 -release -inline dmd -O -release -inline Timings, ldc, seconds: test1: 4.48 // normal string switch test2: 2.98 // perfect hash test3: 2.09 test4: 2.07 test5: 5.44 // AA. Tango AA opIn_r is bug-slow test6: 1.18 Timings, dmd, seconds: test1: 5.55 test2: 5.35 test3: 4.73 test4: 4.79 test5: 3.61 test6: 1.62 --------------- Original idea from: http://www.reddit.com/r/programming/comments/asqml/ Perfect hash computed with: http://www.ibiblio.org/pub/Linux/devel/lang/c/mph-1.2.tar.gz */ version (Tango) { import tango.stdc.stdio: printf; import tango.stdc.stdlib: rand, srand; } else { import std.c.stdio: printf; import std.c.stdlib: rand, srand; } int test1(char[][] strings) { int result; foreach (txt; strings) { switch (txt) { case "__alignof": result += 1; break; // do something case "__alignof__": result += 2; break; case "__asm": result += 3; break; case "__asm__": result += 4; break; case "__attribute": result += 5; break; case "__attribute__": result += 6; break; case "__const": result += 7; break; case "__const__": result += 8; break; case "__inline": result += 9; break; case "__inline__": result += 10; break; case "__signed": result += 11; break; case "__signed__": result += 12; break; case "__typeof": result += 13; break; case "__typeof__": result += 14; break; case "__volatile": result += 15; break; case "__volatile__": result += 16; break; case "all": result += 17; break; case "asm": result += 18; break; case "auto": result += 19; break; case "break": result += 20; break; case "case": result += 21; break; case "catch": result += 22; break; case "char": result += 23; break; case "class": result += 24; break; case "const": result += 25; break; case "continue": result += 26; break; case "default": result += 27; break; case "delete": result += 28; break; case "do": result += 29; break; case "double": result += 30; break; case "dynamic": result += 31; break; case "else": result += 32; break; case "enum": result += 33; break; case "except": result += 34; break; case "exception": result += 35; break; case "extern": result += 36; break; case "float": result += 37; break; case "for": result += 38; break; case "friend": result += 39; break; case "goto": result += 40; break; case "if": result += 41; break; case "inline": result += 42; break; case "int": result += 43; break; case "long": result += 44; break; case "new": result += 45; break; case "operator": result += 46; break; case "overload": result += 47; break; case "private": result += 48; break; case "protected": result += 49; break; case "public": result += 50; break; case "raise": result += 51; break; case "raises": result += 52; break; case "register": result += 53; break; case "reraise": result += 54; break; case "return": result += 55; break; case "short": result += 56; break; case "signed": result += 57; break; case "sizeof": result += 58; break; case "static": result += 59; break; case "struct": result += 60; break; case "switch": result += 61; break; case "this": result += 62; break; case "try": result += 63; break; case "typedef": result += 64; break; case "typeof": result += 65; break; case "union": result += 66; break; case "unsigned": result += 67; break; case "virtual": result += 68; break; case "void": result += 69; break; case "volatile": result += 70; break; case "while": result += 71; break; default: result += 72; break; } } return result; } int test6(char[][] strings) { int result; foreach (word; strings) { // depth-first, so the optimizer merges the chains if (word.length == 0) goto UNREC; char* p = word.ptr; S0: // switch(*p) { case '_': goto S1; case 'a': goto S59; case 'b': goto S64; case 'c': goto S68; case 'd': goto S85; case 'e': goto S103; case 'f': goto S118; case 'g': goto S127; case 'i': goto S130; case 'l': goto S135; case 'n': goto S138; case 'o': goto S140; case 'p': goto S153; case 'r': goto S169; case 's': goto S187; case 't': goto S209; case 'u': goto S219; case 'v': goto S228; case 'w': goto S241; default: goto UNREC; } S1: // _ p++; switch(*p) { case '_': goto S2; default: goto UNREC; } S2: // __ p++; switch(*p) { case 'a': goto S3; case 'c': goto S23; case 'i': goto S29; case 's': goto S36; case 't': goto S43; case 'v': goto S50; default: goto UNREC; } S3: // __a p++; switch(*p) { case 'l': goto S4; case 's': goto S11; case 't': goto S14; default: goto UNREC; } S4: // __al p++; switch(*p) { case 'i': goto S5; default: goto UNREC; } S5: // __ali p++; switch(*p) { case 'g': goto S6; default: goto UNREC; } S6: // __alig p++; switch(*p) { case 'n': goto S7; default: goto UNREC; } S7: // __align p++; switch(*p) { case 'o': goto S8; default: goto UNREC; } S8: // __aligno p++; switch(*p) { case 'f': if (word.length == "__alignof".length) { result += 1; goto END; } else goto S9; default: goto UNREC; } S9: // __alignof p++; switch(*p) { case '_': goto S10; default: goto UNREC; } S10: // __alignof_ p++; switch(*p) { case '_': if (word.length == "__alignof__".length) { result += 2; goto END; } else goto UNREC; default: goto UNREC; } S11: // __as p++; switch(*p) { case 'm': if (word.length == "__asm".length) { result += 3; goto END; } else goto S12; default: goto UNREC; } S12: // __asm p++; switch(*p) { case '_': goto S13; default: goto UNREC; } S13: // __asm_ p++; switch(*p) { case '_': if (word.length == "__asm__".length) { result += 4; goto END; } else goto UNREC; default: goto UNREC; } S14: // __at p++; switch(*p) { case 't': goto S15; default: goto UNREC; } S15: // __att p++; switch(*p) { case 'r': goto S16; default: goto UNREC; } S16: // __attr p++; switch(*p) { case 'i': goto S17; default: goto UNREC; } S17: // __attri p++; switch(*p) { case 'b': goto S18; default: goto UNREC; } S18: // __attrib p++; switch(*p) { case 'u': goto S19; default: goto UNREC; } S19: // __attribu p++; switch(*p) { case 't': goto S20; default: goto UNREC; } S20: // __attribut p++; switch(*p) { case 'e': if (word.length == "__attribute".length) { result += 5; goto END; } else goto S21; default: goto UNREC; } S21: // __attribute p++; switch(*p) { case '_': goto S22; default: goto UNREC; } S22: // __attribute_ p++; switch(*p) { case '_': if (word.length == "__attribute__".length) { result += 6; goto END; } else goto UNREC; default: goto UNREC; } S23: // __c p++; switch(*p) { case 'o': goto S24; default: goto UNREC; } S24: // __co p++; switch(*p) { case 'n': goto S25; default: goto UNREC; } S25: // __con p++; switch(*p) { case 's': goto S26; default: goto UNREC; } S26: // __cons p++; switch(*p) { case 't': if (word.length == "__const".length) { result += 7; goto END; } else goto S27; default: goto UNREC; } S27: // __const p++; switch(*p) { case '_': goto S28; default: goto UNREC; } S28: // __const_ p++; switch(*p) { case '_': if (word.length == "__const__".length) { result += 8; goto END; } else goto UNREC; default: goto UNREC; } S29: // __i p++; switch(*p) { case 'n': goto S30; default: goto UNREC; } S30: // __in p++; switch(*p) { case 'l': goto S31; default: goto UNREC; } S31: // __inl p++; switch(*p) { case 'i': goto S32; default: goto UNREC; } S32: // __inli p++; switch(*p) { case 'n': goto S33; default: goto UNREC; } S33: // __inlin p++; switch(*p) { case 'e': if (word.length == "__inline".length) { result += 9; goto END; } else goto S34; default: goto UNREC; } S34: // __inline p++; switch(*p) { case '_': goto S35; default: goto UNREC; } S35: // __inline_ p++; switch(*p) { case '_': if (word.length == "__inline__".length) { result += 10; goto END; } else goto UNREC; default: goto UNREC; } S36: // __s p++; switch(*p) { case 'i': goto S37; default: goto UNREC; } S37: // __si p++; switch(*p) { case 'g': goto S38; default: goto UNREC; } S38: // __sig p++; switch(*p) { case 'n': goto S39; default: goto UNREC; } S39: // __sign p++; switch(*p) { case 'e': goto S40; default: goto UNREC; } S40: // __signe p++; switch(*p) { case 'd': if (word.length == "__signed".length) { result += 11; goto END; } else goto S41; default: goto UNREC; } S41: // __signed p++; switch(*p) { case '_': goto S42; default: goto UNREC; } S42: // __signed_ p++; switch(*p) { case '_': if (word.length == "__signed__".length) { result += 12; goto END; } else goto UNREC; default: goto UNREC; } S43: // __t p++; switch(*p) { case 'y': goto S44; default: goto UNREC; } S44: // __ty p++; switch(*p) { case 'p': goto S45; default: goto UNREC; } S45: // __typ p++; switch(*p) { case 'e': goto S46; default: goto UNREC; } S46: // __type p++; switch(*p) { case 'o': goto S47; default: goto UNREC; } S47: // __typeo p++; switch(*p) { case 'f': if (word.length == "__typeof".length) { result += 13; goto END; } else goto S48; default: goto UNREC; } S48: // __typeof p++; switch(*p) { case '_': goto S49; default: goto UNREC; } S49: // __typeof_ p++; switch(*p) { case '_': if (word.length == "__typeof__".length) { result += 14; goto END; } else goto UNREC; default: goto UNREC; } S50: // __v p++; switch(*p) { case 'o': goto S51; default: goto UNREC; } S51: // __vo p++; switch(*p) { case 'l': goto S52; default: goto UNREC; } S52: // __vol p++; switch(*p) { case 'a': goto S53; default: goto UNREC; } S53: // __vola p++; switch(*p) { case 't': goto S54; default: goto UNREC; } S54: // __volat p++; switch(*p) { case 'i': goto S55; default: goto UNREC; } S55: // __volati p++; switch(*p) { case 'l': goto S56; default: goto UNREC; } S56: // __volatil p++; switch(*p) { case 'e': if (word.length == "__volatile".length) { result += 15; goto END; } else goto S57; default: goto UNREC; } S57: // __volatile p++; switch(*p) { case '_': goto S58; default: goto UNREC; } S58: // __volatile_ p++; switch(*p) { case '_': if (word.length == "__volatile__".length) { result += 16; goto END; } else goto UNREC; default: goto UNREC; } S59: // a p++; switch(*p) { case 'l': goto S60; case 's': goto S61; case 'u': goto S62; default: goto UNREC; } S60: // al p++; switch(*p) { case 'l': if (word.length == "all".length) { result += 17; goto END; } else goto UNREC; default: goto UNREC; } S61: // as p++; switch(*p) { case 'm': if (word.length == "asm".length) { result += 18; goto END; } else goto UNREC; default: goto UNREC; } S62: // au p++; switch(*p) { case 't': goto S63; default: goto UNREC; } S63: // aut p++; switch(*p) { case 'o': if (word.length == "auto".length) { result += 19; goto END; } else goto UNREC; default: goto UNREC; } S64: // b p++; switch(*p) { case 'r': goto S65; default: goto UNREC; } S65: // br p++; switch(*p) { case 'e': goto S66; default: goto UNREC; } S66: // bre p++; switch(*p) { case 'a': goto S67; default: goto UNREC; } S67: // brea p++; switch(*p) { case 'k': if (word.length == "break".length) { result += 20; goto END; } else goto UNREC; default: goto UNREC; } S68: // c p++; switch(*p) { case 'a': goto S69; case 'h': goto S73; case 'l': goto S75; case 'o': goto S78; default: goto UNREC; } S69: // ca p++; switch(*p) { case 's': goto S70; case 't': goto S71; default: goto UNREC; } S70: // cas p++; switch(*p) { case 'e': if (word.length == "case".length) { result += 21; goto END; } else goto UNREC; default: goto UNREC; } S71: // cat p++; switch(*p) { case 'c': goto S72; default: goto UNREC; } S72: // catc p++; switch(*p) { case 'h': if (word.length == "catch".length) { result += 22; goto END; } else goto UNREC; default: goto UNREC; } S73: // ch p++; switch(*p) { case 'a': goto S74; default: goto UNREC; } S74: // cha p++; switch(*p) { case 'r': if (word.length == "char".length) { result += 23; goto END; } else goto UNREC; default: goto UNREC; } S75: // cl p++; switch(*p) { case 'a': goto S76; default: goto UNREC; } S76: // cla p++; switch(*p) { case 's': goto S77; default: goto UNREC; } S77: // clas p++; switch(*p) { case 's': if (word.length == "class".length) { result += 24; goto END; } else goto UNREC; default: goto UNREC; } S78: // co p++; switch(*p) { case 'n': goto S79; default: goto UNREC; } S79: // con p++; switch(*p) { case 's': goto S80; case 't': goto S81; default: goto UNREC; } S80: // cons p++; switch(*p) { case 't': if (word.length == "const".length) { result += 25; goto END; } else goto UNREC; default: goto UNREC; } S81: // cont p++; switch(*p) { case 'i': goto S82; default: goto UNREC; } S82: // conti p++; switch(*p) { case 'n': goto S83; default: goto UNREC; } S83: // contin p++; switch(*p) { case 'u': goto S84; default: goto UNREC; } S84: // continu p++; switch(*p) { case 'e': if (word.length == "continue".length) { result += 26; goto END; } else goto UNREC; default: goto UNREC; } S85: // d p++; switch(*p) { case 'e': goto S86; case 'o': if (word.length == "do".length) { result += 27; goto END; } else goto S94; case 'y': goto S98; default: goto UNREC; } S86: // de p++; switch(*p) { case 'f': goto S87; case 'l': goto S91; default: goto UNREC; } S87: // def p++; switch(*p) { case 'a': goto S88; default: goto UNREC; } S88: // defa p++; switch(*p) { case 'u': goto S89; default: goto UNREC; } S89: // defau p++; switch(*p) { case 'l': goto S90; default: goto UNREC; } S90: // defaul p++; switch(*p) { case 't': if (word.length == "default".length) { result += 28; goto END; } else goto UNREC; default: goto UNREC; } S91: // del p++; switch(*p) { case 'e': goto S92; default: goto UNREC; } S92: // dele p++; switch(*p) { case 't': goto S93; default: goto UNREC; } S93: // delet p++; switch(*p) { case 'e': if (word.length == "delete".length) { result += 29; goto END; } else goto UNREC; default: goto UNREC; } S94: // do p++; switch(*p) { case 'u': goto S95; default: goto UNREC; } S95: // dou p++; switch(*p) { case 'b': goto S96; default: goto UNREC; } S96: // doub p++; switch(*p) { case 'l': goto S97; default: goto UNREC; } S97: // doubl p++; switch(*p) { case 'e': if (word.length == "double".length) { result += 30; goto END; } else goto UNREC; default: goto UNREC; } S98: // dy p++; switch(*p) { case 'n': goto S99; default: goto UNREC; } S99: // dyn p++; switch(*p) { case 'a': goto S100; default: goto UNREC; } S100: // dyna p++; switch(*p) { case 'm': goto S101; default: goto UNREC; } S101: // dynam p++; switch(*p) { case 'i': goto S102; default: goto UNREC; } S102: // dynami p++; switch(*p) { case 'c': if (word.length == "dynamic".length) { result += 31; goto END; } else goto UNREC; default: goto UNREC; } S103: // e p++; switch(*p) { case 'l': goto S104; case 'n': goto S106; case 'x': goto S108; default: goto UNREC; } S104: // el p++; switch(*p) { case 's': goto S105; default: goto UNREC; } S105: // els p++; switch(*p) { case 'e': if (word.length == "else".length) { result += 32; goto END; } else goto UNREC; default: goto UNREC; } S106: // en p++; switch(*p) { case 'u': goto S107; default: goto UNREC; } S107: // enu p++; switch(*p) { case 'm': if (word.length == "enum".length) { result += 33; goto END; } else goto UNREC; default: goto UNREC; } S108: // ex p++; switch(*p) { case 'c': goto S109; case 't': goto S115; default: goto UNREC; } S109: // exc p++; switch(*p) { case 'e': goto S110; default: goto UNREC; } S110: // exce p++; switch(*p) { case 'p': goto S111; default: goto UNREC; } S111: // excep p++; switch(*p) { case 't': if (word.length == "except".length) { result += 34; goto END; } else goto S112; default: goto UNREC; } S112: // except p++; switch(*p) { case 'i': goto S113; default: goto UNREC; } S113: // excepti p++; switch(*p) { case 'o': goto S114; default: goto UNREC; } S114: // exceptio p++; switch(*p) { case 'n': if (word.length == "exception".length) { result += 35; goto END; } else goto UNREC; default: goto UNREC; } S115: // ext p++; switch(*p) { case 'e': goto S116; default: goto UNREC; } S116: // exte p++; switch(*p) { case 'r': goto S117; default: goto UNREC; } S117: // exter p++; switch(*p) { case 'n': if (word.length == "extern".length) { result += 36; goto END; } else goto UNREC; default: goto UNREC; } S118: // f p++; switch(*p) { case 'l': goto S119; case 'o': goto S122; case 'r': goto S123; default: goto UNREC; } S119: // fl p++; switch(*p) { case 'o': goto S120; default: goto UNREC; } S120: // flo p++; switch(*p) { case 'a': goto S121; default: goto UNREC; } S121: // floa p++; switch(*p) { case 't': if (word.length == "float".length) { result += 37; goto END; } else goto UNREC; default: goto UNREC; } S122: // fo p++; switch(*p) { case 'r': if (word.length == "for".length) { result += 38; goto END; } else goto UNREC; default: goto UNREC; } S123: // fr p++; switch(*p) { case 'i': goto S124; default: goto UNREC; } S124: // fri p++; switch(*p) { case 'e': goto S125; default: goto UNREC; } S125: // frie p++; switch(*p) { case 'n': goto S126; default: goto UNREC; } S126: // frien p++; switch(*p) { case 'd': if (word.length == "friend".length) { result += 39; goto END; } else goto UNREC; default: goto UNREC; } S127: // g p++; switch(*p) { case 'o': goto S128; default: goto UNREC; } S128: // go p++; switch(*p) { case 't': goto S129; default: goto UNREC; } S129: // got p++; switch(*p) { case 'o': if (word.length == "goto".length) { result += 40; goto END; } else goto UNREC; default: goto UNREC; } S130: // i p++; switch(*p) { case 'f': if (word.length == "if".length) { result += 41; goto END; } else goto UNREC; case 'n': goto S131; default: goto UNREC; } S131: // in p++; switch(*p) { case 'l': goto S132; case 't': if (word.length == "int".length) { result += 42; goto END; } else goto UNREC; default: goto UNREC; } S132: // inl p++; switch(*p) { case 'i': goto S133; default: goto UNREC; } S133: // inli p++; switch(*p) { case 'n': goto S134; default: goto UNREC; } S134: // inlin p++; switch(*p) { case 'e': if (word.length == "inline".length) { result += 43; goto END; } else goto UNREC; default: goto UNREC; } S135: // l p++; switch(*p) { case 'o': goto S136; default: goto UNREC; } S136: // lo p++; switch(*p) { case 'n': goto S137; default: goto UNREC; } S137: // lon p++; switch(*p) { case 'g': if (word.length == "long".length) { result += 44; goto END; } else goto UNREC; default: goto UNREC; } S138: // n p++; switch(*p) { case 'e': goto S139; default: goto UNREC; } S139: // ne p++; switch(*p) { case 'w': if (word.length == "new".length) { result += 45; goto END; } else goto UNREC; default: goto UNREC; } S140: // o p++; switch(*p) { case 'p': goto S141; case 'v': goto S147; default: goto UNREC; } S141: // op p++; switch(*p) { case 'e': goto S142; default: goto UNREC; } S142: // ope p++; switch(*p) { case 'r': goto S143; default: goto UNREC; } S143: // oper p++; switch(*p) { case 'a': goto S144; default: goto UNREC; } S144: // opera p++; switch(*p) { case 't': goto S145; default: goto UNREC; } S145: // operat p++; switch(*p) { case 'o': goto S146; default: goto UNREC; } S146: // operato p++; switch(*p) { case 'r': if (word.length == "operator".length) { result += 46; goto END; } else goto UNREC; default: goto UNREC; } S147: // ov p++; switch(*p) { case 'e': goto S148; default: goto UNREC; } S148: // ove p++; switch(*p) { case 'r': goto S149; default: goto UNREC; } S149: // over p++; switch(*p) { case 'l': goto S150; default: goto UNREC; } S150: // overl p++; switch(*p) { case 'o': goto S151; default: goto UNREC; } S151: // overlo p++; switch(*p) { case 'a': goto S152; default: goto UNREC; } S152: // overloa p++; switch(*p) { case 'd': if (word.length == "overload".length) { result += 47; goto END; } else goto UNREC; default: goto UNREC; } S153: // p p++; switch(*p) { case 'r': goto S154; case 'u': goto S165; default: goto UNREC; } S154: // pr p++; switch(*p) { case 'i': goto S155; case 'o': goto S159; default: goto UNREC; } S155: // pri p++; switch(*p) { case 'v': goto S156; default: goto UNREC; } S156: // priv p++; switch(*p) { case 'a': goto S157; default: goto UNREC; } S157: // priva p++; switch(*p) { case 't': goto S158; default: goto UNREC; } S158: // privat p++; switch(*p) { case 'e': if (word.length == "private".length) { result += 48; goto END; } else goto UNREC; default: goto UNREC; } S159: // pro p++; switch(*p) { case 't': goto S160; default: goto UNREC; } S160: // prot p++; switch(*p) { case 'e': goto S161; default: goto UNREC; } S161: // prote p++; switch(*p) { case 'c': goto S162; default: goto UNREC; } S162: // protec p++; switch(*p) { case 't': goto S163; default: goto UNREC; } S163: // protect p++; switch(*p) { case 'e': goto S164; default: goto UNREC; } S164: // protecte p++; switch(*p) { case 'd': if (word.length == "protected".length) { result += 49; goto END; } else goto UNREC; default: goto UNREC; } S165: // pu p++; switch(*p) { case 'b': goto S166; default: goto UNREC; } S166: // pub p++; switch(*p) { case 'l': goto S167; default: goto UNREC; } S167: // publ p++; switch(*p) { case 'i': goto S168; default: goto UNREC; } S168: // publi p++; switch(*p) { case 'c': if (word.length == "public".length) { result += 50; goto END; } else goto UNREC; default: goto UNREC; } S169: // r p++; switch(*p) { case 'a': goto S170; case 'e': goto S174; default: goto UNREC; } S170: // ra p++; switch(*p) { case 'i': goto S171; default: goto UNREC; } S171: // rai p++; switch(*p) { case 's': goto S172; default: goto UNREC; } S172: // rais p++; switch(*p) { case 'e': if (word.length == "raise".length) { result += 51; goto END; } else goto S173; default: goto UNREC; } S173: // raise p++; switch(*p) { case 's': if (word.length == "raises".length) { result += 52; goto END; } else goto UNREC; default: goto UNREC; } S174: // re p++; switch(*p) { case 'g': goto S175; case 'r': goto S180; case 't': goto S184; default: goto UNREC; } S175: // reg p++; switch(*p) { case 'i': goto S176; default: goto UNREC; } S176: // regi p++; switch(*p) { case 's': goto S177; default: goto UNREC; } S177: // regis p++; switch(*p) { case 't': goto S178; default: goto UNREC; } S178: // regist p++; switch(*p) { case 'e': goto S179; default: goto UNREC; } S179: // registe p++; switch(*p) { case 'r': if (word.length == "register".length) { result += 53; goto END; } else goto UNREC; default: goto UNREC; } S180: // rer p++; switch(*p) { case 'a': goto S181; default: goto UNREC; } S181: // rera p++; switch(*p) { case 'i': goto S182; default: goto UNREC; } S182: // rerai p++; switch(*p) { case 's': goto S183; default: goto UNREC; } S183: // rerais p++; switch(*p) { case 'e': if (word.length == "reraise".length) { result += 54; goto END; } else goto UNREC; default: goto UNREC; } S184: // ret p++; switch(*p) { case 'u': goto S185; default: goto UNREC; } S185: // retu p++; switch(*p) { case 'r': goto S186; default: goto UNREC; } S186: // retur p++; switch(*p) { case 'n': if (word.length == "return".length) { result += 55; goto END; } else goto UNREC; default: goto UNREC; } S187: // s p++; switch(*p) { case 'h': goto S188; case 'i': goto S191; case 't': goto S198; case 'w': goto S205; default: goto UNREC; } S188: // sh p++; switch(*p) { case 'o': goto S189; default: goto UNREC; } S189: // sho p++; switch(*p) { case 'r': goto S190; default: goto UNREC; } S190: // shor p++; switch(*p) { case 't': if (word.length == "short".length) { result += 56; goto END; } else goto UNREC; default: goto UNREC; } S191: // si p++; switch(*p) { case 'g': goto S192; case 'z': goto S195; default: goto UNREC; } S192: // sig p++; switch(*p) { case 'n': goto S193; default: goto UNREC; } S193: // sign p++; switch(*p) { case 'e': goto S194; default: goto UNREC; } S194: // signe p++; switch(*p) { case 'd': if (word.length == "signed".length) { result += 57; goto END; } else goto UNREC; default: goto UNREC; } S195: // siz p++; switch(*p) { case 'e': goto S196; default: goto UNREC; } S196: // size p++; switch(*p) { case 'o': goto S197; default: goto UNREC; } S197: // sizeo p++; switch(*p) { case 'f': if (word.length == "sizeof".length) { result += 58; goto END; } else goto UNREC; default: goto UNREC; } S198: // st p++; switch(*p) { case 'a': goto S199; case 'r': goto S202; default: goto UNREC; } S199: // sta p++; switch(*p) { case 't': goto S200; default: goto UNREC; } S200: // stat p++; switch(*p) { case 'i': goto S201; default: goto UNREC; } S201: // stati p++; switch(*p) { case 'c': if (word.length == "static".length) { result += 59; goto END; } else goto UNREC; default: goto UNREC; } S202: // str p++; switch(*p) { case 'u': goto S203; default: goto UNREC; } S203: // stru p++; switch(*p) { case 'c': goto S204; default: goto UNREC; } S204: // struc p++; switch(*p) { case 't': if (word.length == "struct".length) { result += 60; goto END; } else goto UNREC; default: goto UNREC; } S205: // sw p++; switch(*p) { case 'i': goto S206; default: goto UNREC; } S206: // swi p++; switch(*p) { case 't': goto S207; default: goto UNREC; } S207: // swit p++; switch(*p) { case 'c': goto S208; default: goto UNREC; } S208: // switc p++; switch(*p) { case 'h': if (word.length == "switch".length) { result += 61; goto END; } else goto UNREC; default: goto UNREC; } S209: // t p++; switch(*p) { case 'h': goto S210; case 'r': goto S212; case 'y': goto S213; default: goto UNREC; } S210: // th p++; switch(*p) { case 'i': goto S211; default: goto UNREC; } S211: // thi p++; switch(*p) { case 's': if (word.length == "this".length) { result += 62; goto END; } else goto UNREC; default: goto UNREC; } S212: // tr p++; switch(*p) { case 'y': if (word.length == "try".length) { result += 63; goto END; } else goto UNREC; default: goto UNREC; } S213: // ty p++; switch(*p) { case 'p': goto S214; default: goto UNREC; } S214: // typ p++; switch(*p) { case 'e': goto S215; default: goto UNREC; } S215: // type p++; switch(*p) { case 'd': goto S216; case 'o': goto S218; default: goto UNREC; } S216: // typed p++; switch(*p) { case 'e': goto S217; default: goto UNREC; } S217: // typede p++; switch(*p) { case 'f': if (word.length == "typedef".length) { result += 64; goto END; } else goto UNREC; default: goto UNREC; } S218: // typeo p++; switch(*p) { case 'f': if (word.length == "typeof".length) { result += 65; goto END; } else goto UNREC; default: goto UNREC; } S219: // u p++; switch(*p) { case 'n': goto S220; default: goto UNREC; } S220: // un p++; switch(*p) { case 'i': goto S221; case 's': goto S223; default: goto UNREC; } S221: // uni p++; switch(*p) { case 'o': goto S222; default: goto UNREC; } S222: // unio p++; switch(*p) { case 'n': if (word.length == "union".length) { result += 66; goto END; } else goto UNREC; default: goto UNREC; } S223: // uns p++; switch(*p) { case 'i': goto S224; default: goto UNREC; } S224: // unsi p++; switch(*p) { case 'g': goto S225; default: goto UNREC; } S225: // unsig p++; switch(*p) { case 'n': goto S226; default: goto UNREC; } S226: // unsign p++; switch(*p) { case 'e': goto S227; default: goto UNREC; } S227: // unsigne p++; switch(*p) { case 'd': if (word.length == "unsigned".length) { result += 67; goto END; } else goto UNREC; default: goto UNREC; } S228: // v p++; switch(*p) { case 'i': goto S229; case 'o': goto S234; default: goto UNREC; } S229: // vi p++; switch(*p) { case 'r': goto S230; default: goto UNREC; } S230: // vir p++; switch(*p) { case 't': goto S231; default: goto UNREC; } S231: // virt p++; switch(*p) { case 'u': goto S232; default: goto UNREC; } S232: // virtu p++; switch(*p) { case 'a': goto S233; default: goto UNREC; } S233: // virtua p++; switch(*p) { case 'l': if (word.length == "virtual".length) { result += 68; goto END; } else goto UNREC; default: goto UNREC; } S234: // vo p++; switch(*p) { case 'i': goto S235; case 'l': goto S236; default: goto UNREC; } S235: // voi p++; switch(*p) { case 'd': if (word.length == "void".length) { result += 69; goto END; } else goto UNREC; default: goto UNREC; } S236: // vol p++; switch(*p) { case 'a': goto S237; default: goto UNREC; } S237: // vola p++; switch(*p) { case 't': goto S238; default: goto UNREC; } S238: // volat p++; switch(*p) { case 'i': goto S239; default: goto UNREC; } S239: // volati p++; switch(*p) { case 'l': goto S240; default: goto UNREC; } S240: // volatil p++; switch(*p) { case 'e': if (word.length == "volatile".length) { result += 70; goto END; } else goto UNREC; default: goto UNREC; } S241: // w p++; switch(*p) { case 'h': goto S242; default: goto UNREC; } S242: // wh p++; switch(*p) { case 'i': goto S243; default: goto UNREC; } S243: // whi p++; switch(*p) { case 'l': goto S244; default: goto UNREC; } S244: // whil p++; switch(*p) { case 'e': if (word.length == "while".length) { result += 71; goto END; } else goto UNREC; default: goto UNREC; } UNREC: result += 72; END:; } return result; } // end test6 void main() { const int NDATA = 10_000; const int NLOOPS = 4_000; const int USE_ALGO = 6; srand(10); const char[][] words = [ // right keywords "__alignof", "__alignof__", "__asm", "__asm__", "__attribute", "__attribute__", "__const", "__const__", "__inline", "__inline__", "__signed", "__signed__", "__typeof", "__typeof__", "__volatile", "__volatile__", "all", "asm", "auto", "break", "case", "catch", "char", "class", "const", "continue", "default", "delete", "do", "double", "dynamic", "else", "enum", "except", "exception", "extern", "float", "for", "friend", "goto", "if", "inline", "int", "long", "new", "operator", "overload", "private", "protected", "public", "raise", "raises", "register", "reraise", "return", "short", "signed", "sizeof", "static", "struct", "switch", "this", "try", "typedef", "typeof", "union", "unsigned", "virtual", "void", "volatile", "while", // wrong keywords "the", "of", "and", "a", "to", "in", "is", "you", "that", "it", "he", "was", "for", "on", "are", "as", "with", "his", "they", "I ", "at", "be", "this", "have", "from", "or", "one", "had", "by", ]; // std.string.split() not used here to use the C std lib only // generate test data char[][] testData = new char[][](NDATA); foreach (ref word; testData) word = words[rand() % words.length]; int tot; for (int i; i < NLOOPS; i++) { static if (USE_ALGO == 1) tot += test1(testData); // versions 2..5 removed here static if (USE_ALGO == 6) tot += test6(testData); } printf("%d\n", tot); }
Private
[
?
]
Run code