Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

vmware-archive/java-run-length-encoding

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Java Run-Length Encoding

This sample demonstrates functions of finite streams that maintain stream-related state.

The Encode function takes an input stream of integers and emits a run-length encoding output stream in which each maximal consecutive sequence of a given value is replaced by the size of the sequence followed by the value. For example, the stream:

< 0, 1, 1, 2, 2, 2 >

encodes to:

< 1, 0, 2, 1, 3, 2 >

Encoding an empty input stream results in an empty output stream.

The Decode function takes a run-length encoded input stream and emits the decoded output stream. For example, the stream:

< 3, 1, 1, 0 >

decodes to:

< 1, 1, 1, 0 >

Decoding an empty input stream results in an empty output stream.

Acknowledgements

Thanks to Simon Baslé and others in the Reactor project for help with the encoding algorithm. Simon has written a blog describing a more efficient algorithm.

Optional puzzle

Encode followed by Decode usually produces the original stream of integers, but when will Decode followed by Encode not produce the original stream of integers? (Strong hint - don't look if you want to solve this puzzle yourself)

Answer: here

About

A riff sample involving functions of finite streams that maintain stream-related state

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •