yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1#include "slang-perfect-hash-codegen.h" 2 3#include "../core/slang-io.h" 4 5namespace Slang 6{ 7SlangResult writeHashFile ( 8String outCppPath , 9String valueType , 10const List < String > includes , 11const HashParams & hashParams , 12const List < String > values ) 13{ 14StringBuilder sb ; 15StringWriter writer (& sb ,WriterFlags (0 )); 16WriterHelper w (& writer ); 17 18w ."// Hash function for %s\n" ,valueType .getBuffer ()); 19w ."//\n" ); 20w ."// This file was thoughtfully generated by a machine,\n" ); 21w ."// don't even think about modifying it yourself!\n" ); 22w ."//\n" ); 23w ."\n" ); 24for (const auto & i :includes ) 25 { 26if (i .getLength ()) 27w ."#include \"%s\"\n" ,i .getBuffer ()); 28 } 29w ."\n" ); 30w ."\n" ); 31w ."namespace Slang\n" ); 32w ."{\n" ); 33w ."\n" ); 34 35w .put (perfectHashToEmbeddableCpp ( 36hashParams , 37valueType .getUnownedSlice (), 38 (String ("lookup" )+ valueType ).getUnownedSlice (), 39values ) 40 .getBuffer ()); 41 42w ."}\n" ); 43 44return File ::writeAllTextIfChanged (outCppPath ,sb .getUnownedSlice ()); 45} 46 47SlangResult writePerfectHashLookupCppFile ( 48String fileName , 49List < String > opnames , 50String enumName , 51String enumerantPrefix , 52String enumHeaderFile , 53DiagnosticSink * sink ) 54{ 55HashParams hashParams ; 56auto r = minimalPerfectHash (opnames ,hashParams ); 57switch (r ) 58 { 59case HashFindResult ::UnavoidableHashCollision : 60 { 61sink -> diagnoseRaw ( 62Severity ::Error , 63"Unable to find a non-overlapping hash function.\n" 64"The hash function probably has a unavoidable " 65"collision for some input words\n" ); 66return SLANG_FAIL ; 67 } 68case HashFindResult ::NonUniqueKeys : 69 { 70sink -> diagnoseRaw (Severity ::Error ,"Input word list has duplicates\n" ); 71return SLANG_FAIL ; 72 } 73case HashFindResult ::Success :; 74 } 75 76List < String > values ; 77values .reserve (hashParams .destTable .getCount ()); 78for (const auto & v :hashParams .destTable ) 79values .add (enumerantPrefix + v ); 80return writeHashFile ( 81fileName , 82enumName , 83 {"core/slang-common.h" ,"core/slang-string.h" ,enumHeaderFile }, 84hashParams , 85values ); 86} 87 88}// namespace Slang