irapilguy

Untitled

Dec 13th, 2021
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   public async *fingerprints(tokens: string[]): AsyncIterableIterator<Fingerprint> {
  2.     const hash = new RollingHash(this.k);
  3.     let window: string[] = [];
  4.     let filePos: number = -this.k;
  5.     let bufferPos = 0;
  6.     let minPos = 0;
  7.     const buffer: number[] = new Array(this.windowSize).fill(Number.MAX_SAFE_INTEGER);
  8.     for await (const [hashedToken, token] of this.hashTokens(tokens)) {
  9.       filePos++;
  10.       window = window.slice(-this.k + 1);
  11.       window.push(token);
  12.       if (filePos < 0) {
  13.         hash.nextHash(hashedToken);
  14.         continue;
  15.       }
  16.       // minPos define far right hashing.
  17.       bufferPos = (bufferPos + 1) % this.windowSize;
  18.       buffer[bufferPos] = hash.nextHash(hashedToken);
  19.       if (minPos === bufferPos) {
  20.         // Scan buffer starting from bufferPos for the far right minimal hashing.
  21.         let i = (bufferPos + 1) % this.windowSize;
  22.         for (; i !== bufferPos; i = (i + 1) % this.windowSize) {
  23.           if (buffer[i] <= buffer[minPos]) {
  24.             minPos = i;
  25.           }
  26.         }
  27.         const offset = (minPos - bufferPos - this.windowSize) % this.windowSize;
  28.         const start = filePos + offset;
  29.       }
  30.       else {
  31.         if (buffer[bufferPos] <= buffer[minPos]) {
  32.           minPos = bufferPos;
  33.           const start = filePos + ((minPos - bufferPos - this.windowSize) % this.windowSize);
  34.         }
  35.        }
  36.       yield {
  37.          hash: buffer[minPos],
  38.          start,
  39.          stop: start + this.k - 1,
  40.       };   
  41.     }
  42.   }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment