Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/djboris9/slidingwindow
Sliding Window implementation
https://github.com/djboris9/slidingwindow
Last synced: 26 days ago
JSON representation
Sliding Window implementation
- Host: GitHub
- URL: https://github.com/djboris9/slidingwindow
- Owner: djboris9
- Created: 2015-12-18T17:12:31.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-18T18:48:32.000Z (about 9 years ago)
- Last Synced: 2023-03-27T17:24:51.878Z (almost 2 years ago)
- Language: Go
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sliding Window
Package slidingwindow implements an sliding window. But without move back.
It's like a ringbuffer with the possibility to read an arbitary cell and remove items from the front.
[godoc](https://godoc.org/github.com/djboris9/slidingwindow)## Explain it!
```go
import sw "github.com/djboris9/slidingwindow"w := sw.Window{}
w.Create(19, 3)
// Creates the following:
// ┌--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┐
// |00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|
// |__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
// |----------------------CAPACITY OF 19-----------------------|w.Add(1)
w.Add(2)
// ┌--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┐
// |01|02|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|
// |__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
// |-----|
// Sliding Window (Size 3, but not enough items in it)w.Add(3)
w.Add(4)
// ┌--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┬--┐
// |01|02|03|04|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|
// |__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
// |--------|
// Sliding Window (Size 3)w.Slice()
// Returns [2, 3, 4]
```
If the capacity is exceeded, it will simply rollover, which can be an more time consuming.## Example
```go
package mainimport (
"fmt"
sw "github.com/djboris9/slidingwindow"
)func main() {
// Create a new sliding window with capacity 10 and window size 3
w := sw.Window{}
w.Create(10, 3)// Print newly created slice
fmt.Printf("%v\n", w.Slice()) // Prints: []// Add two new items
w.Add(7)
w.Add(8)
fmt.Printf("%v\n", w.Slice()) // Prints: [7 8]// Remove the last item
w.Remove()
fmt.Printf("%v\n", w.Slice()) // Prints: [7]// Bulk load two new items
w.Load([]float64{4, 5})
fmt.Printf("%v\n", w.Slice()) // Prints: [7 4 5]// Clear window
w.Clear()
fmt.Printf("%v\n", w.Slice()) // Prints: []
}
```## Testing and benchmarking
Run go test on your own hardware.
```
$ go test -cover -bench=.
PASS
BenchmarkSlice-4 50000000 30.3 ns/op
BenchmarkAdd100X10-4 30000000 53.8 ns/op
BenchmarkAdd100X50-4 30000000 57.5 ns/op
BenchmarkAdd100X80-4 20000000 63.5 ns/op
BenchmarkAdd1000X20-4 30000000 53.7 ns/op
BenchmarkAdd100000X20-4 30000000 54.9 ns/op
BenchmarkAdd100000X200-4 20000000 55.8 ns/op
BenchmarkLoadNormal-4 500000 2809 ns/op
BenchmarkLoadRollover-4 3000000 512 ns/op
coverage: 100.0% of statements
ok github.com/djboris9/slidingwindow 14.402s
```