Download the PHP package jrdev/drange without Composer
On this page you can find all versions of the php package jrdev/drange. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Vendor jrdev
Package drange
Short Description Efficiently manage non-contiguous integer ranges with automatic merging and set operations
License MIT
Homepage https://github.com/jrobinsonc/drange/blob/master/README.md
Package drange
Short Description Efficiently manage non-contiguous integer ranges with automatic merging and set operations
License MIT
Homepage https://github.com/jrobinsonc/drange/blob/master/README.md
Please rate this library. Is it a good library?
Informations about the package drange
DRange
DRange is a PHP library for managing discontinuous (non-contiguous) ranges of integers. Rather than storing every number individually, it holds a compact, sorted list of contiguous sub-ranges — automatically merging adjacent ranges when you add numbers and splitting them when you subtract. The result is an expressive, memory-efficient structure for any problem that involves gaps in sequences.
Features
- Add, subtract, or intersect individual integers, numeric ranges,
SubRangeinstances, or entireDRangeobjects - Automatic merging of adjacent and overlapping ranges on
add() - Automatic splitting of ranges on
subtract() - Random-access by logical index (
->index($n)) across all sub-ranges - Native PHP
count()support viaCountable - Human-readable string representation (e.g.
[ 1-5, 8-10 ]) - Chainable
add()andsubtract()calls - Full clone support — deep copy, mutations on the clone do not affect the original
- Zero runtime dependencies
Requirements
- PHP >= 8.2
- Composer
Installation
Quick Start
API Reference
DRange (jrdev\DRange)
| Method / Usage | Returns | Description |
|---|---|---|
new DRange() |
DRange |
Empty range |
new DRange($n) |
DRange |
Range containing the single integer $n |
new DRange($low, $high) |
DRange |
Contiguous range [$low..$high] |
->add($n) |
$this |
Add a single integer |
->add($low, $high) |
$this |
Add a contiguous range |
->add($drange) |
$this |
Add all sub-ranges from another DRange |
->add($subrange) |
$this |
Add a SubRange instance |
->subtract($n) |
$this |
Subtract a single integer |
->subtract($low, $high) |
$this |
Subtract a contiguous range |
->subtract($drange) |
$this |
Subtract all sub-ranges of another DRange |
->subtract($subrange) |
$this |
Subtract a SubRange instance |
->intersect($n) |
DRange |
New range containing only integers present in both this range and $n |
->intersect($low, $high) |
DRange |
Intersection with a contiguous range |
->intersect($drange) |
DRange |
Intersection with another DRange |
->intersect($subrange) |
DRange |
Intersection with a SubRange instance |
->index($i) |
int\|null |
Value at logical position $i (0-based); null if out of bounds |
count($drange) |
int |
Total number of integers across all sub-ranges |
(string) $drange |
string |
e.g. "[ 1-5, 8-10 ]" |
clone $drange |
DRange |
Deep copy; mutations do not affect the original |
Adjacent ranges (touching but not overlapping) are merged automatically on every add() call.
SubRange (jrdev\DRange\SubRange)
| Member | Type | Description |
|---|---|---|
new SubRange($low, $high) |
— | Throws \UnexpectedValueException if $low > $high |
->low |
int |
Lower bound (inclusive) |
->high |
int |
Upper bound (inclusive) |
->length |
int |
$high - $low + 1 |
->overlaps($range) |
bool |
true if the two ranges share at least one integer |
->touches($range) |
bool |
true if the ranges overlap or are adjacent (no gap between them) |
->add($range) |
SubRange |
Merged range — only meaningful when touches() is true |
->subtract($range) |
SubRange[] |
Returns 0, 1, or 2 sub-ranges after removing the overlap |
(string) $subrange |
string |
"low-high" or just "n" for a single-number range |
clone $subrange |
SubRange |
Deep copy |
Usage Examples
Method chaining
Operating on two DRange objects
Random access with index()
Counting with native count()
Computing intersections with intersect()
Using SubRange directly
Real-World Use Cases
- Network port management — Represent firewall allowlists or blocklists as unions of port ranges; subtract blocked ranges from the full
0–65535range to derive what is open. - HTTP range requests — Track which byte offsets of a large file have been downloaded; call
subtract()as each chunk arrives andcount()to know how many bytes remain. - Appointment and booking systems — Represent availability as numeric time ranges (minutes since midnight, Unix timestamps); subtract booked slots to see what is still open.
- Batch job progress tracking — Record which database record ID ranges have been processed; use
index()to resume from an arbitrary position without scanning the full set. - Pagination and lazy loading — Track which page or item index ranges have already been fetched from an API; avoid re-requesting pages that fall inside an already-loaded range.
- Seat allocation — Model auditorium or transit rows as numeric ranges; subtract reserved seats and inspect the remaining range to find the next available contiguous block.
- IP address management — Convert IPv4 addresses to 32-bit integers and use
DRangeto track allocated and free address blocks without needing a dedicated CIDR library.
License
Licensed under the MIT licence.
All versions of drange with dependencies
PHP Build Version
Package Version
Requires
php Version
>=8.2
The package jrdev/drange contains the following files
Loading the files please wait ...