Created
January 7, 2023 01:45
-
-
Save lukaszo/f45dc4ac7e3fb1cbb7e65689743adbb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func blockPosition(datastream []byte, size int) (int, error) { | |
start := 0 | |
count := 1 | |
indexes := make([]int, 256) | |
for i := 0; i < 256; i++ { | |
indexes[i] = -1 | |
} | |
var index int | |
indexes[datastream[0]] = 0 | |
var i = 1 | |
for ; count < size; i++ { | |
block := datastream[start : start+count] | |
b := datastream[i] | |
index = indexes[b] | |
if index < 0 { | |
indexes[b] = count | |
count += 1 | |
} else { | |
for j := 0; j < index; j++ { | |
indexes[block[j]] = -1 | |
} | |
for j := index + 1; j < count; j++ { | |
indexes[block[j]] -= index + 1 | |
} | |
count = count - index | |
start = start + index + 1 | |
indexes[b] = count - 1 | |
} | |
} | |
if count == size { | |
return i, nil | |
} | |
return 0, errors.New("not found") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment