MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / RLEncodebytes

Function RLEncodebytes

compression/rlecoding.go:50–64  ·  view source on GitHub ↗

RLEncodebytes takes a byte slice and returns its run-length encoding as a byte slice

(data []byte)

Source from the content-addressed store, hash-verified

48
49// RLEncodebytes takes a byte slice and returns its run-length encoding as a byte slice
50func RLEncodebytes(data []byte) []byte {
51 var result []byte
52 var count byte = 1
53
54 for i := 0; i < len(data); i++ {
55 if i+1 < len(data) && data[i] == data[i+1] {
56 count++
57 continue
58 }
59 result = append(result, count, data[i])
60 count = 1
61 }
62
63 return result
64}
65
66// RLEdecodebytes takes a run-length encoded byte slice and returns the original byte slice
67func RLEdecodebytes(data []byte) []byte {

Callers 2

BenchmarkRLEncodeBytesFunction · 0.92

Calls

no outgoing calls

Tested by 2

BenchmarkRLEncodeBytesFunction · 0.74