Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
7.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1997.
  5. //
  6. // File: tplist.cxx
  7. //
  8. // Contents: Builds the perfect hash table in plist.cxx
  9. //
  10. // History: 05-Sep-97 KyleP Added header
  11. //
  12. //--------------------------------------------------------------------------
  13. #include <pch.cxx>
  14. #pragma hdrstop
  15. WCHAR * apwc[] =
  16. {
  17. L"DIRECTORY",
  18. L"CLASSID",
  19. L"FILEINDEX",
  20. L"USN",
  21. L"FILENAME",
  22. L"PATH",
  23. L"SIZE",
  24. L"ATTRIB",
  25. L"WRITE",
  26. L"CREATE",
  27. L"ACCESS",
  28. L"ALLOCSIZE",
  29. L"CONTENTS",
  30. L"SHORTFILENAME",
  31. L"RANKVECTOR",
  32. L"RANK",
  33. L"HITCOUNT",
  34. L"WORKID",
  35. L"ALL",
  36. L"VPATH",
  37. L"DOCTITLE",
  38. L"DOCSUBJECT",
  39. L"DOCAUTHOR",
  40. L"DOCKEYWORDS",
  41. L"DOCCOMMENTS",
  42. L"DOCTEMPLATE",
  43. L"DOCLASTAUTHOR",
  44. L"DOCREVNUMBER",
  45. L"DOCEDITTIME",
  46. L"DOCLASTPRINTED",
  47. L"DOCCREATEDTM",
  48. L"DOCLASTSAVEDTM",
  49. L"DOCPAGECOUNT",
  50. L"DOCWORDCOUNT",
  51. L"DOCCHARCOUNT",
  52. L"DOCTHUMBNAIL",
  53. L"DOCAPPNAME",
  54. L"DOCSECURITY",
  55. L"DOCCATEGORY",
  56. L"DOCPRESENTATIONTARGET",
  57. L"DOCBYTECOUNT",
  58. L"DOCLINECOUNT",
  59. L"DOCPARACOUNT",
  60. L"DOCSLIDECOUNT",
  61. L"DOCNOTECOUNT",
  62. L"DOCHIDDENCOUNT",
  63. L"DOCPARTTITLES",
  64. L"DOCMANAGER",
  65. L"DOCCOMPANY",
  66. L"HTMLHREF",
  67. L"A_HREF",
  68. L"IMG_ALT",
  69. L"HTMLHEADING1",
  70. L"HTMLHEADING2",
  71. L"HTMLHEADING3",
  72. L"HTMLHEADING4",
  73. L"HTMLHEADING5",
  74. L"HTMLHEADING6",
  75. L"CHARACTERIZATION",
  76. L"NEWSGROUP",
  77. L"NEWSGROUPS",
  78. L"NEWSREFERENCES",
  79. L"NEWSSUBJECT",
  80. L"NEWSFROM",
  81. L"NEWSMSGID",
  82. L"NEWSDATE",
  83. L"NEWSRECEIVEDDATE",
  84. L"NEWSARTICLEID",
  85. L"MSGNEWSGROUP",
  86. L"MSGNEWSGROUPS",
  87. L"MSGREFERENCES",
  88. L"MSGSUBJECT",
  89. L"MSGFROM",
  90. L"MSGMESSAGEID",
  91. L"MSGDATE",
  92. L"MSGRECEIVEDDATE",
  93. L"MSGARTICLEID",
  94. L"MEDIAEDITOR",
  95. L"MEDIASUPPLIER",
  96. L"MEDIASOURCE",
  97. L"MEDIASEQUENCE_NO",
  98. L"MEDIAPROJECT",
  99. L"MEDIASTATUS",
  100. L"MEDIAOWNER",
  101. L"MEDIARATING",
  102. L"MEDIAPRODUCTION",
  103. L"MUSICARTIST",
  104. L"MUSICSONGTITLE",
  105. L"MUSICALBUM",
  106. L"MUSICYEAR",
  107. L"MUSICCOMMENT",
  108. L"MUSICTRACK",
  109. L"MUSICGENRE",
  110. L"DRMLICENSE",
  111. L"DRMDESCRIPTION",
  112. L"DRMPLAYCOUNT",
  113. L"DRMPLAYSTARTS",
  114. L"DRMPLAYEXPIRES",
  115. L"IMAGEFILETYPE",
  116. L"IMAGECX",
  117. L"IMAGECY",
  118. L"IMAGERESOLUTIONX",
  119. L"IMAGERESOLUTIONY",
  120. L"IMAGEBITDEPTH",
  121. L"IMAGECOLORSPACE",
  122. L"IMAGECOMPRESSION",
  123. L"IMAGETRANSPARENCY",
  124. L"IMAGEGAMMAVALUE",
  125. L"IMAGEFRAMECOUNT",
  126. L"IMAGEDIMENSIONS",
  127. L"AUDIOFORMAT",
  128. L"AUDIOTIMELENGTH",
  129. L"AUDIOAVGDATARATE",
  130. L"AUDIOSAMPLERATE",
  131. L"AUDIOSAMPLESIZE",
  132. L"AUDIOCHANNELCOUNT",
  133. L"VIDEOSTREAMNAME",
  134. L"VIDEOFRAMEWIDTH",
  135. L"VIDEOFRAMEHEIGHT",
  136. L"VIDEOTIMELENGTH",
  137. L"VIDEOFRAMECOUNT",
  138. L"VIDEOFRAMERATE",
  139. L"VIDEODATARATE",
  140. L"VIDEOSAMPLESIZE",
  141. L"VIDEOCOMPRESSION",
  142. };
  143. BOOL IsPrime( ULONG ul )
  144. {
  145. unsigned ulStop = (ul + 1)/2;
  146. for ( unsigned i = 2; i <= ulStop && 0 != (ul % i); i++ )
  147. continue;
  148. return (i > ulStop);
  149. }
  150. ULONG Hash0( WCHAR const * pc ) //846
  151. {
  152. unsigned ulH,ulG;
  153. for (ulH=0; *pc; pc++)
  154. {
  155. ulH = (ulH << 4) + (*pc);
  156. if (ulG = (ulH & 0xf0000000))
  157. ulH ^= ulG >> 24;
  158. ulH &= ~ulG;
  159. }
  160. return ulH;
  161. }
  162. ULONG Hash1( WCHAR const * pwcName ) //541
  163. {
  164. WCHAR const *pwcStart = pwcName;
  165. ULONG ulHash = 0;
  166. while ( 0 != *pwcName )
  167. {
  168. ulHash <<= 1;
  169. ulHash += *pwcName;
  170. pwcName++;
  171. }
  172. ulHash <<= 1;
  173. ulHash += ( pwcName - pwcStart );
  174. return ulHash;
  175. }
  176. ULONG Hash2( WCHAR const * pwcName ) //443
  177. {
  178. WCHAR const *pwcStart = pwcName;
  179. ULONG ulHash = *pwcName++;
  180. ulHash <<= 6;
  181. while ( 0 != *pwcName )
  182. {
  183. ulHash <<= 1;
  184. ulHash += *pwcName;
  185. pwcName++;
  186. }
  187. ulHash <<= 1;
  188. ulHash += ( pwcName - pwcStart );
  189. return ulHash;
  190. }
  191. ULONG Hash3( WCHAR const * pwcName ) //664
  192. {
  193. WCHAR const *pwcStart = pwcName;
  194. ULONG ulHash = *pwcName++;
  195. ulHash <<= 7;
  196. if ( 0 != *pwcName )
  197. {
  198. ulHash += *pwcName++;
  199. ulHash <<= 7;
  200. }
  201. while ( 0 != *pwcName )
  202. {
  203. ulHash <<= 1;
  204. ulHash += *pwcName;
  205. pwcName++;
  206. }
  207. ulHash <<= 1;
  208. ulHash += ( pwcName - pwcStart );
  209. return ulHash;
  210. }
  211. ULONG Hash4( WCHAR const * pwcName ) //345
  212. {
  213. WCHAR const *pwcStart = pwcName;
  214. ULONG ulHash = *pwcName++;
  215. ulHash <<= 5;
  216. while ( 0 != *pwcName )
  217. {
  218. ulHash <<= 2;
  219. ulHash += *pwcName++;
  220. }
  221. ulHash <<= 2;
  222. ulHash += ( pwcName - pwcStart );
  223. return ulHash;
  224. }
  225. ULONG Hash5( WCHAR const * pwcName ) //645 with '4', 558 with '6'
  226. {
  227. WCHAR const *pwcStart = pwcName;
  228. ULONG ulHash = *pwcName++;
  229. ulHash <<= 6;
  230. while ( 0 != *pwcName )
  231. {
  232. ulHash <<= 2;
  233. ulHash += *pwcName;
  234. pwcName++;
  235. }
  236. ulHash <<= 2;
  237. ulHash += ( pwcName - pwcStart );
  238. return ulHash;
  239. }
  240. typedef ULONG (* HFun)( WCHAR const * );
  241. HFun aHashFun[] = { Hash0, Hash1, Hash2, Hash3, Hash4, Hash5 };
  242. void __cdecl main()
  243. {
  244. const cStrings = sizeof apwc / sizeof apwc[0];
  245. ULONG a[2000];
  246. ULONG ulSmallest = 0xFFFFFFFF;
  247. ULONG iHFSmallest = 0;
  248. for ( unsigned iHFun = 0; iHFun < sizeof(aHashFun)/sizeof(aHashFun[0]); iHFun++ )
  249. {
  250. HFun hf = aHashFun[iHFun];
  251. for ( ULONG size = cStrings; size < sizeof a / sizeof a[0]; size++ )
  252. {
  253. //if ( !IsPrime(size) )
  254. // continue;
  255. BOOL fOK = TRUE;
  256. RtlZeroMemory( a, sizeof a );
  257. for ( ULONG x = 0; x < cStrings; x++ )
  258. {
  259. unsigned h = (*hf)( apwc[x] ) % size;
  260. //printf( "%d '%ws'\n", h, apwc[x] );
  261. if ( 0 != a[h] )
  262. {
  263. //printf( " %d fail\n", size );
  264. fOK = FALSE;
  265. break;
  266. }
  267. a[h] = 1 + x;
  268. }
  269. if ( fOK )
  270. {
  271. #if 0
  272. printf( "Hash Function %d: %d worked for %d strings\n", iHFun, size, cStrings );
  273. //
  274. // Print out
  275. //
  276. for ( ULONG i = 0; i < size; i++ )
  277. {
  278. unsigned index = a[i] - 1;
  279. if ( 0 == a[i] )
  280. printf( " 0, // %d\n", i );
  281. else
  282. printf( " (CPropEntry *) &aStaticList[%d], // %d '%ws'\n",
  283. index, i, apwc[index] );
  284. }
  285. #endif
  286. if ( size < ulSmallest )
  287. {
  288. ulSmallest = size;
  289. iHFSmallest = iHFun;
  290. }
  291. break;
  292. }
  293. }
  294. }
  295. if ( ulSmallest != 0xFFFFFFFF )
  296. {
  297. printf( "Hash Function #%d: %d worked for %d strings\n", iHFSmallest, ulSmallest, cStrings );
  298. //
  299. // Recompute hash
  300. //
  301. RtlZeroMemory( a, sizeof a );
  302. for ( ULONG x = 0; x < cStrings; x++ )
  303. {
  304. unsigned h = (*aHashFun[iHFSmallest])( apwc[x] ) % ulSmallest;
  305. a[h] = 1 + x;
  306. }
  307. //
  308. // Print out
  309. //
  310. for ( ULONG i = 0; i < ulSmallest; i++ )
  311. {
  312. unsigned index = a[i] - 1;
  313. if ( 0 == a[i] )
  314. printf( " 0, // %d\n", i );
  315. else
  316. printf( " (CPropEntry *) &aStaticList[%d], // %d '%ws'\n",
  317. index, i, apwc[index] );
  318. }
  319. }
  320. else
  321. printf( "no perfect hash!\n" );
  322. } //main