rivulet 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +165 -0
- data/Rakefile +7 -0
- data/lefthook.yml +13 -0
- data/lib/rivulet/count_stream.rb +15 -0
- data/lib/rivulet/count_window.rb +41 -0
- data/lib/rivulet/deque.rb +27 -0
- data/lib/rivulet/minmax_stream.rb +15 -0
- data/lib/rivulet/minmax_window.rb +44 -0
- data/lib/rivulet/stats_stream.rb +9 -0
- data/lib/rivulet/stats_window.rb +38 -0
- data/lib/rivulet/stream.rb +85 -0
- data/lib/rivulet/sum_stream.rb +33 -0
- data/lib/rivulet/sum_window.rb +32 -0
- data/lib/rivulet/version.rb +5 -0
- data/lib/rivulet/window.rb +21 -0
- data/lib/rivulet/window_builder.rb +93 -0
- data/lib/rivulet.rb +22 -0
- data/sig/rivulet.rbs +4 -0
- metadata +66 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6f36b4a53bbbe5ad4bf8fff267c5128bd891cbc6e822a5ddac9daf14d04226e1
|
|
4
|
+
data.tar.gz: 0da92be9908be413dffd62252cdc416f896f2ddf7bfad45acd8cdbc72d659b7a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4f209850a7d9eeb8e227c32cac41e5f3c567744744b8e83fc261958b88c2866cac947875a09fb0012b7887f15dc53926a1772525a1bde32fc406541852b852eb
|
|
7
|
+
data.tar.gz: e86115172c2281b4dfa9dedaa74dbf6780e9de4a78b3b7965a6fbd6ae2389af4de83048323b3a4cd990afffcfd13d394e43681a5bfff1dc461f3150194a95ec2
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Brandon Weaver
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Rivulet
|
|
2
|
+
|
|
3
|
+
A small stream with a bit of state flowing through it.
|
|
4
|
+
|
|
5
|
+
Rivulet gives Ruby a vocabulary for sliding window operations — the grow-shrink-emit pattern that `Enumerable` never named. It sits between `each_cons` (fixed windows, no state) and hand-rolled pointer loops (flexible but noisy).
|
|
6
|
+
|
|
7
|
+
Near-zero allocations on the hot path. 200k items, single-digit object count.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem "rivulet"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Moving average
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
Rivulet.sum(latencies).windows(3) { |w| w.average }
|
|
21
|
+
# => [20.0, 30.0, 40.0]
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Batch records under a byte budget
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
Rivulet.sum(records, &:bytesize)
|
|
28
|
+
.max_size_while { |w| w.sum <= 1024 }
|
|
29
|
+
# => 12 (max records that fit)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Minimum subarray meeting a target
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
Rivulet.sum(nums).min_size_where { |w| w.sum >= target }
|
|
36
|
+
# => 2 (smallest window with sum >= target)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Longest non-repeating run
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
Rivulet.count(events)
|
|
43
|
+
.max_window { |w| !w.repeats? }
|
|
44
|
+
.max_by { |w| w.size }
|
|
45
|
+
# => 5
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Sliding window maximum (O(n) via monotonic deque)
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
Rivulet.minmax(prices).windows(20) { |w| w.max }
|
|
52
|
+
# => [103, 105, 105, ...]
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Top-k windows
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
Rivulet.sum(data).windows(5).max_by(3) { |w| w.average }
|
|
59
|
+
# => [98.2, 95.1, 93.7]
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Minimum window containing all target characters
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
target = Hash.new(0)
|
|
66
|
+
t.each_char { |c| target[c] += 1 }
|
|
67
|
+
|
|
68
|
+
Rivulet.count(s.chars)
|
|
69
|
+
.min_window { |w| w.covers?(target) }
|
|
70
|
+
.min_by { |w| w.size }
|
|
71
|
+
# => 4 (smallest window covering all of t)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Early termination
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
Rivulet.sum(data)
|
|
78
|
+
.max_window { |w| w.sum <= budget }
|
|
79
|
+
.first { |w| w.size }
|
|
80
|
+
# => 1 (first valid window's size)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Window Types
|
|
84
|
+
|
|
85
|
+
| Entry point | Tracks | Window methods |
|
|
86
|
+
|---|---|---|
|
|
87
|
+
| `Rivulet.sum(source)` | Running total | `sum`, `average`, `size` |
|
|
88
|
+
| `Rivulet.sum(source) { \|item\| ... }` | Derived metric sum | `sum`, `average`, `size` |
|
|
89
|
+
| `Rivulet.count(source)` | Item frequencies | `distinct`, `repeats?`, `max_count`, `covers?`, `sum`, `counts`, `size` |
|
|
90
|
+
| `Rivulet.minmax(source)` | Rolling min/max | `min`, `max`, `range`, `size` |
|
|
91
|
+
| `Rivulet.stats(source)` | All of the above | `sum`, `average`, `min`, `max`, `range`, `distinct`, `repeats?`, `max_count`, `covers?`, `counts`, `size` |
|
|
92
|
+
|
|
93
|
+
## API
|
|
94
|
+
|
|
95
|
+
### Building a window stream
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
stream = Rivulet.sum(data) # or .count, .minmax
|
|
99
|
+
stream = Rivulet.sum(data) { |item| item.bytesize } # with mapper
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Fixed-size windows
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
stream.windows(n) # => WindowBuilder
|
|
106
|
+
stream.windows(n) { |w| ... } # => Array (filter_map semantics)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Variable-size windows (grow/shrink)
|
|
110
|
+
|
|
111
|
+
```ruby
|
|
112
|
+
stream.max_window { |w| rule } # => WindowBuilder (maximize: shrink when invalid)
|
|
113
|
+
stream.min_window { |w| goal } # => WindowBuilder (minimize: shrink while still valid)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`max_window` grows the window and evicts from the front when the rule fails — use it to find the **largest** window under a constraint.
|
|
117
|
+
|
|
118
|
+
`min_window` grows the window and shrinks from the front while the goal holds — use it to find the **smallest** window meeting a goal.
|
|
119
|
+
|
|
120
|
+
### Terminal methods on WindowBuilder
|
|
121
|
+
|
|
122
|
+
All terminals yield the **live window** — no snapshots allocated.
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
builder.each_window { |w| ... } # filter_map: collect non-nil block results
|
|
126
|
+
builder.max_by { |w| ... } # single best score
|
|
127
|
+
builder.max_by(k) { |w| ... } # top-k scores (descending)
|
|
128
|
+
builder.min_by { |w| ... } # single smallest score
|
|
129
|
+
builder.min_by(k) { |w| ... } # bottom-k scores (ascending)
|
|
130
|
+
builder.first { |w| ... } # first non-nil result
|
|
131
|
+
builder.first(k) { |w| ... } # first k non-nil results
|
|
132
|
+
builder.take(k) { |w| ... } # alias for first(k)
|
|
133
|
+
builder.count # number of valid windows
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Reducers (single-value shortcuts)
|
|
137
|
+
|
|
138
|
+
```ruby
|
|
139
|
+
stream.max_size_while { |w| rule } # largest window size under constraint
|
|
140
|
+
stream.max_sum_while { |w| rule } # largest sum under constraint (sum only)
|
|
141
|
+
stream.min_size_where { |w| goal } # smallest window size meeting goal (sum only)
|
|
142
|
+
stream.max_distinct_while { |w| rule } # most distinct items (count only)
|
|
143
|
+
stream.max_range_while { |w| rule } # largest range (minmax only)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Performance
|
|
147
|
+
|
|
148
|
+
Rivulet's builder path allocates near-zero objects regardless of input size:
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
200k items, budget 500:
|
|
152
|
+
max_window.max_by { size } 39 objects
|
|
153
|
+
max_size_while (reducer) 13 objects
|
|
154
|
+
windows(5) { average } 18 objects
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
The `minmax` window uses monotonic deques for O(1) amortized min/max per step. All window types maintain O(1) incremental state updates.
|
|
158
|
+
|
|
159
|
+
## Requirements
|
|
160
|
+
|
|
161
|
+
- Ruby >= 3.2
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
MIT
|
data/Rakefile
ADDED
data/lefthook.yml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
class CountStream < Stream
|
|
5
|
+
def max_distinct_while(&rule)
|
|
6
|
+
best = 0
|
|
7
|
+
each_max_window(rule) { |w| best = w.distinct if w.distinct > best }
|
|
8
|
+
best
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def new_window = CountWindow.new
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
class CountWindow < Window
|
|
5
|
+
def initialize
|
|
6
|
+
super
|
|
7
|
+
@counts = Hash.new(0)
|
|
8
|
+
@items = []
|
|
9
|
+
@front = 0
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def add(item)
|
|
13
|
+
@counts[item] += 1
|
|
14
|
+
@items.push(item)
|
|
15
|
+
super
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def evict
|
|
19
|
+
if @front < @items.size
|
|
20
|
+
item = @items[@front]
|
|
21
|
+
@counts[item] -= 1
|
|
22
|
+
@counts.delete(item) if @counts[item].zero?
|
|
23
|
+
@front += 1
|
|
24
|
+
super
|
|
25
|
+
item
|
|
26
|
+
else
|
|
27
|
+
super
|
|
28
|
+
nil
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def repeats? = @counts.any? { |_, n| n > 1 }
|
|
33
|
+
def distinct = @counts.size
|
|
34
|
+
def max_count = @counts.values.max || 0
|
|
35
|
+
def counts = @counts
|
|
36
|
+
|
|
37
|
+
def covers?(target_counts)
|
|
38
|
+
target_counts.all? { |k, v| @counts[k] >= v }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
# A double-ended queue with O(1) push/pop at both ends.
|
|
5
|
+
# Backed by a growing array with a front index; no element is ever shifted.
|
|
6
|
+
class Deque
|
|
7
|
+
def initialize
|
|
8
|
+
@data = []
|
|
9
|
+
@front = 0
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def push(val) = @data.push(val)
|
|
13
|
+
|
|
14
|
+
def pop
|
|
15
|
+
@data.pop unless empty?
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def shift
|
|
19
|
+
@front += 1 unless empty?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def first = @data[@front]
|
|
23
|
+
def last = @data.last
|
|
24
|
+
def any? = @front < @data.size
|
|
25
|
+
def empty? = @front >= @data.size
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
class MinMaxStream < Stream
|
|
5
|
+
def max_range_while(&rule)
|
|
6
|
+
best = 0
|
|
7
|
+
each_max_window(rule) { |w| best = w.range if w.range && w.range > best }
|
|
8
|
+
best
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def new_window = MinMaxWindow.new
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
class MinMaxWindow < Window
|
|
5
|
+
def initialize
|
|
6
|
+
super
|
|
7
|
+
@items = []
|
|
8
|
+
@min_deque = Deque.new
|
|
9
|
+
@max_deque = Deque.new
|
|
10
|
+
@front = 0
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def add(item)
|
|
14
|
+
idx = @items.size
|
|
15
|
+
@min_deque.pop while @min_deque.any? && @items[@min_deque.last] >= item
|
|
16
|
+
@max_deque.pop while @max_deque.any? && @items[@max_deque.last] <= item
|
|
17
|
+
@min_deque.push(idx)
|
|
18
|
+
@max_deque.push(idx)
|
|
19
|
+
@items.push(item)
|
|
20
|
+
super
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def evict
|
|
24
|
+
return super if @front >= @items.size
|
|
25
|
+
|
|
26
|
+
@min_deque.shift if @min_deque.first == @front
|
|
27
|
+
@max_deque.shift if @max_deque.first == @front
|
|
28
|
+
@front += 1
|
|
29
|
+
super
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def min
|
|
33
|
+
@items[@min_deque.first] unless empty?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def max
|
|
37
|
+
@items[@max_deque.first] unless empty?
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def range
|
|
41
|
+
max - min unless empty?
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
class StatsWindow < MinMaxWindow
|
|
5
|
+
def initialize
|
|
6
|
+
super
|
|
7
|
+
@sum = 0
|
|
8
|
+
@counts = Hash.new(0)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def add(item)
|
|
12
|
+
@sum += item
|
|
13
|
+
@counts[item] += 1
|
|
14
|
+
super
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def evict
|
|
18
|
+
if @front < @items.size
|
|
19
|
+
item = @items[@front]
|
|
20
|
+
@sum -= item
|
|
21
|
+
@counts[item] -= 1
|
|
22
|
+
@counts.delete(item) if @counts[item].zero?
|
|
23
|
+
end
|
|
24
|
+
super
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def sum = @sum
|
|
28
|
+
def average = empty? ? nil : @sum.fdiv(size)
|
|
29
|
+
|
|
30
|
+
def distinct = @counts.size
|
|
31
|
+
def repeats? = @counts.any? { |_, n| n > 1 }
|
|
32
|
+
def max_count = @counts.values.max || 0
|
|
33
|
+
def counts = @counts
|
|
34
|
+
def covers?(target_counts)
|
|
35
|
+
target_counts.all? { |k, v| @counts[k] >= v }
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
class Stream
|
|
5
|
+
def initialize(source)
|
|
6
|
+
@source = source
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def windows(size, step: 1, &block)
|
|
10
|
+
raise ArgumentError, "size must be positive" unless size.positive?
|
|
11
|
+
raise ArgumentError, "step must be positive" unless step.positive?
|
|
12
|
+
|
|
13
|
+
builder = WindowBuilder.new(stream: self, mode: :fixed, size: size, step: step)
|
|
14
|
+
block ? builder.each_window(&block) : builder
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def tumbling(size, &block)
|
|
18
|
+
windows(size, step: size, &block)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def max_window(&rule)
|
|
22
|
+
WindowBuilder.new(stream: self, mode: :variable, rule: rule)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def min_window(&goal)
|
|
26
|
+
WindowBuilder.new(stream: self, mode: :satisfied, rule: goal)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @api private — used by WindowBuilder
|
|
30
|
+
def iterate_windows(mode:, size: nil, rule: nil, step: 1, &block)
|
|
31
|
+
case mode
|
|
32
|
+
when :fixed then iterate_fixed(size: size, step: step, &block)
|
|
33
|
+
when :variable then iterate_variable(rule: rule, &block)
|
|
34
|
+
when :satisfied then iterate_satisfied(rule: rule, &block)
|
|
35
|
+
# :nocov:
|
|
36
|
+
else raise ArgumentError, "unknown mode: #{mode}"
|
|
37
|
+
# :nocov:
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def iterate_fixed(size:, step:)
|
|
44
|
+
window = new_window
|
|
45
|
+
emit_count = 0
|
|
46
|
+
@source.each do |item|
|
|
47
|
+
window.add(item)
|
|
48
|
+
window.evict while window.size > size
|
|
49
|
+
|
|
50
|
+
if window.size == size
|
|
51
|
+
yield window if (emit_count % step).zero?
|
|
52
|
+
emit_count += 1
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def iterate_variable(rule:)
|
|
58
|
+
window = new_window
|
|
59
|
+
@source.each do |item|
|
|
60
|
+
window.add(item)
|
|
61
|
+
window.evict until window.empty? || rule.call(window)
|
|
62
|
+
|
|
63
|
+
yield window unless window.empty?
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def iterate_satisfied(rule:)
|
|
68
|
+
window = new_window
|
|
69
|
+
@source.each do |item|
|
|
70
|
+
window.add(item)
|
|
71
|
+
|
|
72
|
+
until window.empty? || !rule.call(window)
|
|
73
|
+
yield window
|
|
74
|
+
window.evict
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def each_max_window(rule)
|
|
80
|
+
iterate_windows(mode: :variable, rule: rule) { |w| yield w }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def new_window = Window.new
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
class SumStream < Stream
|
|
5
|
+
def initialize(source, mapper: nil)
|
|
6
|
+
super(mapper ? source.lazy.map(&mapper) : source)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def max_size_while(&rule)
|
|
10
|
+
best = 0
|
|
11
|
+
each_max_window(rule) { |w| best = w.size if w.size > best }
|
|
12
|
+
best
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def max_sum_while(&rule)
|
|
16
|
+
best = 0
|
|
17
|
+
each_max_window(rule) { |w| best = w.sum if w.sum > best }
|
|
18
|
+
best
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def min_size_where(&goal)
|
|
22
|
+
best = nil
|
|
23
|
+
iterate_windows(mode: :satisfied, rule: goal) do |w|
|
|
24
|
+
best = w.size if best.nil? || w.size < best
|
|
25
|
+
end
|
|
26
|
+
best
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def new_window = SumWindow.new
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
class SumWindow < Window
|
|
5
|
+
def initialize
|
|
6
|
+
super
|
|
7
|
+
@sum = 0
|
|
8
|
+
@items = []
|
|
9
|
+
@front = 0
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def add(value)
|
|
13
|
+
@sum += value
|
|
14
|
+
@items.push(value)
|
|
15
|
+
super
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def evict
|
|
19
|
+
if @front < @items.size
|
|
20
|
+
@sum -= @items[@front]
|
|
21
|
+
@front += 1
|
|
22
|
+
end
|
|
23
|
+
super
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def sum = @sum
|
|
27
|
+
|
|
28
|
+
def average
|
|
29
|
+
@sum.fdiv(size) unless empty?
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
class Window
|
|
5
|
+
def initialize
|
|
6
|
+
@size = 0
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def size = @size
|
|
10
|
+
def empty? = @size.zero?
|
|
11
|
+
|
|
12
|
+
def add(item)
|
|
13
|
+
@size += 1
|
|
14
|
+
self
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def evict
|
|
18
|
+
@size -= 1 if @size > 0
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rivulet
|
|
4
|
+
class WindowBuilder
|
|
5
|
+
def initialize(stream:, mode:, size: nil, rule: nil, step: 1)
|
|
6
|
+
@stream = stream
|
|
7
|
+
@mode = mode
|
|
8
|
+
@size = size
|
|
9
|
+
@rule = rule
|
|
10
|
+
@step = step
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def each_window(&block)
|
|
14
|
+
results = []
|
|
15
|
+
iterate do |window|
|
|
16
|
+
value = block.call(window)
|
|
17
|
+
results << value if value
|
|
18
|
+
end
|
|
19
|
+
results
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def max_by(k = nil, &block)
|
|
23
|
+
if k
|
|
24
|
+
top = []
|
|
25
|
+
iterate do |window|
|
|
26
|
+
score = block.call(window)
|
|
27
|
+
top << score
|
|
28
|
+
top.sort!.shift if top.size > k
|
|
29
|
+
end
|
|
30
|
+
top.sort.reverse
|
|
31
|
+
else
|
|
32
|
+
best = nil
|
|
33
|
+
iterate do |window|
|
|
34
|
+
score = block.call(window)
|
|
35
|
+
best = score if best.nil? || score > best
|
|
36
|
+
end
|
|
37
|
+
best
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def min_by(k = nil, &block)
|
|
42
|
+
if k
|
|
43
|
+
top = []
|
|
44
|
+
iterate do |window|
|
|
45
|
+
score = block.call(window)
|
|
46
|
+
top << score
|
|
47
|
+
top.sort!.pop if top.size > k
|
|
48
|
+
end
|
|
49
|
+
top.sort
|
|
50
|
+
else
|
|
51
|
+
best = nil
|
|
52
|
+
iterate do |window|
|
|
53
|
+
score = block.call(window)
|
|
54
|
+
best = score if best.nil? || score < best
|
|
55
|
+
end
|
|
56
|
+
best
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def first(k = nil, &block)
|
|
61
|
+
if k
|
|
62
|
+
results = []
|
|
63
|
+
iterate do |window|
|
|
64
|
+
value = block ? block.call(window) : window
|
|
65
|
+
results << value if value
|
|
66
|
+
break if results.size == k
|
|
67
|
+
end
|
|
68
|
+
results
|
|
69
|
+
else
|
|
70
|
+
result = nil
|
|
71
|
+
iterate do |window|
|
|
72
|
+
result = block ? block.call(window) : window
|
|
73
|
+
break if result
|
|
74
|
+
end
|
|
75
|
+
result
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def take(k, &block) = first(k, &block)
|
|
80
|
+
|
|
81
|
+
def count
|
|
82
|
+
n = 0
|
|
83
|
+
iterate { |_| n += 1 }
|
|
84
|
+
n
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
def iterate(&block)
|
|
90
|
+
@stream.iterate_windows(mode: @mode, size: @size, rule: @rule, step: @step, &block)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
data/lib/rivulet.rb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "rivulet/version"
|
|
4
|
+
require_relative "rivulet/deque"
|
|
5
|
+
require_relative "rivulet/window"
|
|
6
|
+
require_relative "rivulet/window_builder"
|
|
7
|
+
require_relative "rivulet/stream"
|
|
8
|
+
require_relative "rivulet/sum_window"
|
|
9
|
+
require_relative "rivulet/sum_stream"
|
|
10
|
+
require_relative "rivulet/count_window"
|
|
11
|
+
require_relative "rivulet/count_stream"
|
|
12
|
+
require_relative "rivulet/minmax_window"
|
|
13
|
+
require_relative "rivulet/minmax_stream"
|
|
14
|
+
require_relative "rivulet/stats_window"
|
|
15
|
+
require_relative "rivulet/stats_stream"
|
|
16
|
+
|
|
17
|
+
module Rivulet
|
|
18
|
+
def self.sum(source, &mapper) = SumStream.new(source, mapper: mapper || nil)
|
|
19
|
+
def self.count(source) = CountStream.new(source)
|
|
20
|
+
def self.minmax(source) = MinMaxStream.new(source)
|
|
21
|
+
def self.stats(source) = StatsStream.new(source)
|
|
22
|
+
end
|
data/sig/rivulet.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rivulet
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Brandon Weaver
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2026-06-01 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: A small stream with a bit of state flowing through it. Rivulet gives
|
|
13
|
+
Ruby a vocabulary for sliding window operations — the grow-shrink-emit pattern that
|
|
14
|
+
Enumerable never named.
|
|
15
|
+
email:
|
|
16
|
+
- keystonelemur@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- CHANGELOG.md
|
|
22
|
+
- CODE_OF_CONDUCT.md
|
|
23
|
+
- LICENSE.txt
|
|
24
|
+
- README.md
|
|
25
|
+
- Rakefile
|
|
26
|
+
- lefthook.yml
|
|
27
|
+
- lib/rivulet.rb
|
|
28
|
+
- lib/rivulet/count_stream.rb
|
|
29
|
+
- lib/rivulet/count_window.rb
|
|
30
|
+
- lib/rivulet/deque.rb
|
|
31
|
+
- lib/rivulet/minmax_stream.rb
|
|
32
|
+
- lib/rivulet/minmax_window.rb
|
|
33
|
+
- lib/rivulet/stats_stream.rb
|
|
34
|
+
- lib/rivulet/stats_window.rb
|
|
35
|
+
- lib/rivulet/stream.rb
|
|
36
|
+
- lib/rivulet/sum_stream.rb
|
|
37
|
+
- lib/rivulet/sum_window.rb
|
|
38
|
+
- lib/rivulet/version.rb
|
|
39
|
+
- lib/rivulet/window.rb
|
|
40
|
+
- lib/rivulet/window_builder.rb
|
|
41
|
+
- sig/rivulet.rbs
|
|
42
|
+
homepage: https://github.com/baweaver/rivulet
|
|
43
|
+
licenses:
|
|
44
|
+
- MIT
|
|
45
|
+
metadata:
|
|
46
|
+
homepage_uri: https://github.com/baweaver/rivulet
|
|
47
|
+
source_code_uri: https://github.com/baweaver/rivulet
|
|
48
|
+
changelog_uri: https://github.com/baweaver/rivulet/blob/main/CHANGELOG.md
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: 3.2.0
|
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
requirements: []
|
|
63
|
+
rubygems_version: 3.6.6
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: Sliding window operations for Ruby collections
|
|
66
|
+
test_files: []
|