Algorithm-SlidingWindow-Dynamic
view release on metacpan or search on metacpan
# Algorithm::SlidingWindow::Dynamic
[](https://github.com/haxmeister/perl-algorithm-slidingwindow-dynamic/actions/workflows/ci.yml)
[](https://metacpan.org/pod/Algorithm::SlidingWindow::Dynamic)
Generic, dynamically sized sliding window.
This module implements a count-based sliding window (deque) over arbitrary Perl scalars.
It supports growing and shrinking from either end and a `slide()` operation for fixed-length
rolling windows.
## Install
### From CPAN (recommended)
```bash
cpanm Algorithm::SlidingWindow::Dynamic
```
lib/Algorithm/SlidingWindow/Dynamic.pm view on Meta::CPAN
The API is intentionally small and is designed to support common
sliding-window and two-pointer algorithms. The window may grow or shrink
dynamically, and operations that remove elements return the removed values,
which is useful when maintaining external state such as running sums or
counts.
The module does not impose a fixed capacity or eviction policy. Instead,
window size is controlled explicitly by the caller through the use of
C<push>, C<shift>, C<pop>, and C<slide> operations. This makes the module
suitable for both variable-length windows and fixed-length rolling windows.
The window is count-based only; it does not track time or perform
time-based expiration. Any ordering, comparison, or aggregation logic is
left to user code.
All core operations run in O(1) amortized time using an internal circular
buffer.
=head1 METHODS
lib/Algorithm/SlidingWindow/Dynamic.pm view on Meta::CPAN
=head2 clear
Removes all items from the window. After this call, the window size is zero.
=head1 EXAMPLES
=head2 Shortest Subarray With Sum >= K (Non-Negative Values)
This example demonstrates the classic sliding-window technique for finding the
length of the shortest contiguous subarray whose sum is at least C<K>.
B<Important:> This algorithm assumes that all input values are non-negative.
If negative values are present, a different approach is required.
use Algorithm::SlidingWindow::Dynamic;
sub shortest_subarray_at_least_k {
my ($nums, $k) = @_;
my $w = Algorithm::SlidingWindow::Dynamic->new;
( run in 0.539 second using v1.01-cache-2.11-cpan-d7f47b0818f )