Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async *fingerprints(tokens: string[]): AsyncIterableIterator<Fingerprint> {
- const hash = new RollingHash(this.k);
- let window: string[] = [];
- let filePos: number = -this.k;
- let bufferPos = 0;
- let minPos = 0;
- const buffer: number[] = new Array(this.windowSize).fill(Number.MAX_SAFE_INTEGER);
- for await (const [hashedToken, token] of this.hashTokens(tokens)) {
- filePos++;
- window = window.slice(-this.k + 1);
- window.push(token);
- if (filePos < 0) {
- hash.nextHash(hashedToken);
- continue;
- }
- // minPos define far right hashing.
- bufferPos = (bufferPos + 1) % this.windowSize;
- buffer[bufferPos] = hash.nextHash(hashedToken);
- if (minPos === bufferPos) {
- // Scan buffer starting from bufferPos for the far right minimal hashing.
- let i = (bufferPos + 1) % this.windowSize;
- for (; i !== bufferPos; i = (i + 1) % this.windowSize) {
- if (buffer[i] <= buffer[minPos]) {
- minPos = i;
- }
- }
- const offset = (minPos - bufferPos - this.windowSize) % this.windowSize;
- const start = filePos + offset;
- }
- else {
- if (buffer[bufferPos] <= buffer[minPos]) {
- minPos = bufferPos;
- const start = filePos + ((minPos - bufferPos - this.windowSize) % this.windowSize);
- }
- }
- yield {
- hash: buffer[minPos],
- start,
- stop: start + this.k - 1,
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment