Giter Site home page Giter Site logo

jchristn / slidingwindow Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 1.88 MB

SlidingWindow provides an interface to retrieve chunks from a byte array using a sliding window.

License: MIT License

C# 100.00%
slidingwindow sliding-window data processing stream bytes

slidingwindow's Introduction

SlidingWindow

SlidingWindow provides an interface to retrieve chunks from a byte array using a sliding window.

Use Cases

SlidingWindow is helpful for use cases that require examination of a sliding window of bytes within a byte array. For instance, SlidingWindow is useful for cryptographic and compression use cases.

How It Works

Initialize SlidingWindow with a byte array over which you would like to iterate using a SlidingWindow.

Specify the desired window size in chunkSize.

Call GetNextChunk() to retrieve the next window of data. As you retrieve chunks, the window will advance by the number of bytes defined in shiftSize.

On the last chunk, if fewer than chunkSize bytes remain, the remaining bytes will be returned, and out finalChunk will be set to true, indicating the end of the byte array.

Example

Suppose you have the sentence The quick brown fox jumped over the lazy dog..., which is 47 bytes long.

         1         2         3         4         5
12345678901234567890123456789012345678901234567890
The quick brown fox jumped over the lazy dog...

Initializing the Bytes class with this byte array, a chunkSize of 24 and a sliding window of 8 would result in the following:

using SlidingWindow;

string strData = "The quick brown fox jumped over the lazy dog..."
byte[] byteData = Encoding.UTF8.GetBytes(strData);
Bytes slidingWindow = new Bytes(byteData, 24, 8);

int chunks = slidingWindow.ChunkCount();  // 4

int position = 0;         // zero-based index of current chunk
byte[] chunk = null;      // all of the data in the window
byte[] newData = null;    // new data retrieved from the previous chunk
bool finalChunk = false;  // whether or not it's the last chunk

chunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);  
// position   : 0
// chunk      : 'The quick brown fox jump'
// newData    : 'The quick brown fox jump'
// finalChunk : false

chunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);  
// position   : 8
// chunk      : 'k brown fox jumped over '
// newData    : 'ed over '
// finalChunk : false

chunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);  
// position   : 16
// chunk      : 'fox jumped over the lazy'
// newData    : 'the lazy'
// finalChunk : false

chunk = slidingWindow.GetNextChunk(out position, out newData, out finalChunk);  
// position   : 24
// chunk      : 'ed over the lazy dog...'
// newData    : ' dog...'
// finalChunk : true

chunkSize must be greater than zero and less than the length of the supplied data, and shiftSize must be greater than zero and less than chunkSize.

New in v1.0.x

  • Initial release with support for byte arrays.
  • Added support for net452.
  • Added support for streams where the content length is known.
  • Added out param for position at which the chunk starts from the source byte array or stream.
  • Added out param for byte array containing the new data from the previous chunk included in the returned chunk.

Version History

Please refer to CHANGELOG.md.

slidingwindow's People

Contributors

jchristn avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.