Advertisement
KoMeDiAnT

WSHHashFunc

Apr 14th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by KoMeDiAnT on 4/12/2017.
  3.  */
  4.  
  5. function printHelp() {
  6.     WSH.echo('usage: CScript //nologo WSHHash.js input_file.txt \
  7.    \n//nologo: Prevent logo display: No banner will be shown at execution time \
  8.    \ninput_file: name of input file')
  9. }
  10.  
  11. //@return {number}
  12. function SimpleHash(str) {
  13.     var hash = 0;
  14.  
  15.     for (var i = 0; i < str.length; i++){
  16.         hash += str.charCodeAt(i);
  17.     }
  18.  
  19.     return hash;
  20. }
  21.  
  22. //@return {number}
  23. function SquareHash(str) {
  24.     var hash = 0;
  25.  
  26.     for (var i = 0; i < str.length; i++){
  27.         hash += str.charCodeAt(i)*str.charCodeAt(i);
  28.     }
  29.    
  30.     return hash;
  31. }
  32.  
  33. function FindBySimpleHash(str, subStr) {
  34.     var collision = 0;
  35.     var strHash = 0;
  36.     var subStrHash = SimpleHash(subStr);
  37.  
  38.     for (var i = 0; i < str.length - subStr.length + 1; i++){
  39.         if(i === 0){
  40.             strHash = SimpleHash(str.substr(0, subStr.length));
  41.         }
  42.         else{
  43.             strHash += str.charCodeAt(i + subStr.length - 1) - str.charCodeAt(i - 1);
  44.         }
  45.  
  46.         if (strHash === subStrHash){
  47.             var j = 0;
  48.  
  49.             while(str.charAt(i + j) === subStr.charAt(j)){
  50.                 j++;
  51.  
  52.                 if (j === subStr.length){
  53.                     var end = i + j - 1;
  54.                     WSH.echo('Begin: ' + i + '   End: ' + end);
  55.                     collision -= 1;
  56.                     break;
  57.                 }
  58.             }
  59.  
  60.             collision++;
  61.         }
  62.     }
  63.  
  64.     WSH.echo('Amount of collisions: ' + collision);
  65. }
  66.  
  67. function FindBySquareHash(str, subStr) {
  68.     var collision = 0;
  69.     var strHash = 0;
  70.     var subStrHash = SquareHash(subStr);
  71.  
  72.     for (var i = 0; i < str.length - subStr.length + 1; i++){
  73.         if (i === 0){
  74.             strHash = SquareHash(str.substr(0, subStr.length));
  75.         }
  76.         else{
  77.             strHash += str.charCodeAt(i + subStr.length - 1)*str.charCodeAt(i + subStr.length - 1)
  78.                 - str.charCodeAt(i - 1)*str.charCodeAt(i - 1);
  79.         }
  80.  
  81.         if (strHash === subStrHash){
  82.             var j = 0;
  83.  
  84.             while(str.charAt(i + j) === subStr.charAt(j)){
  85.                 j++;
  86.  
  87.                 if (j === subStr.length){
  88.                     var end = i + j - 1;
  89.                     WSH.echo('Begin: ' + i + '   End: ' + end);
  90.                     collision--;
  91.                     break;
  92.                 }
  93.             }
  94.  
  95.             collision++;
  96.         }
  97.     }
  98.  
  99.    WSH.echo('Amount of collisions: ' + collision);
  100. }
  101.  
  102. function FindByRAndCHash(str, subStr) {
  103.     var collision = 0;
  104.     var strHash = 0;
  105.     var subStrHash = 0;
  106.  
  107.     for (var i = 0; i < subStr.length; i++){
  108.         strHash += str.charCodeAt(i)*Math.pow(2, subStr.length - i - 1);
  109.         subStrHash += subStr.charCodeAt(i)*Math.pow(2, subStr.length - i - 1);
  110.     }
  111.  
  112.     var i = 0;
  113.  
  114.     while(i < str.length - subStr.length + 1){
  115.  
  116.         if (strHash === subStrHash){
  117.             var j = 0;
  118.  
  119.             while(str.charAt(i + j) === subStr.charAt(j)){
  120.                 j++;
  121.  
  122.                 if (j === subStr.length){
  123.                     var end = i + j - 1;
  124.                     WSH.echo('Begin: ' + i + '   End: ' + end);
  125.                     collision--;
  126.                     break;
  127.                 }
  128.             }
  129.             collision++;
  130.         }
  131.         i++;
  132.         strHash = (strHash - str.charCodeAt(i - 1)*Math.pow(2, subStr.length - 1))*2 + str.charCodeAt(i + subStr.length - 1);
  133.     }
  134.  
  135.     WSH.echo('Amount of collisions: ' + collision);
  136. }
  137.  
  138. function main() {
  139.     if (WSH.Arguments.length === 0){
  140.         printHelp();
  141.         return;
  142.     }
  143.  
  144.     var fso = new ActiveXObject('Scripting.FileSystemObject')
  145.     var ts = fso.OpenTextFile(WSH.Arguments(0));
  146.     var inputData = ts.ReadAll();
  147.  
  148.     // Раскомментируй, если хочешь проверку работы скрипта
  149.     // WSH.echo('Type string, please')
  150.     // var inputData = WScript.StdIn.ReadLine();
  151.  
  152.     if (inputData === ''){
  153.         WSH.echo('Input file is empty');
  154.         return;
  155.     }
  156.  
  157.     WSH.echo('Type substring')
  158.     var subStr = WScript.StdIn.ReadLine();
  159.  
  160.     if (subStr === ''){
  161.         WSH.echo('Wrong input substring');
  162.         return;
  163.     }
  164.     var start;
  165.     var end;
  166.     var time;
  167.  
  168.     WSH.echo('\nBy Simple Hash:');
  169.     start = new Date().getTime();
  170.     FindBySimpleHash(inputData, subStr);
  171.     end = new Date().getTime();
  172.     time = end - start;
  173.     WSH.echo('Time for Simple Hash: '+ time + '\n');
  174.  
  175.     WSH.echo('By Square Hash:');
  176.     start = new Date().getTime();
  177.     FindBySquareHash(inputData, subStr);
  178.     end = new Date().getTime();
  179.     time = end - start;
  180.     WSH.echo('Time for Square Hash: ' + time + '\n');
  181.  
  182.     WSH.echo('By Rabin And Carp Hash:');
  183.     start = new Date().getTime();
  184.     FindByRAndCHash(inputData, subStr);
  185.     end = new Date().getTime();
  186.     time = end - start;
  187.     WSH.echo('Time for Rabin And Carp Hash: ' + time + '\n');
  188. }
  189.  
  190. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement