An open API service indexing awesome lists of open source software.

https://github.com/remorhaz/php-int-rangesets

Integer range sets manipulation
https://github.com/remorhaz/php-int-rangesets

integer range ranges set sets

Last synced: 12 months ago
JSON representation

Integer range sets manipulation

Awesome Lists containing this project

README

          

# Integer Range Sets
[![Latest Stable Version](https://poser.pugx.org/remorhaz/int-rangesets/v/stable)](https://packagist.org/packages/remorhaz/int-rangesets)
[![Build Status](https://travis-ci.com/remorhaz/php-int-rangesets.svg?branch=master)](https://travis-ci.com/remorhaz/php-int-rangesets)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/remorhaz/php-int-rangesets/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/remorhaz/php-int-rangesets/?branch=master)
[![codecov](https://codecov.io/gh/remorhaz/php-int-rangesets/branch/master/graph/badge.svg)](https://codecov.io/gh/remorhaz/php-int-rangesets)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fremorhaz%2Fphp-int-rangesets%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/remorhaz/php-int-rangesets/master)
[![License](https://poser.pugx.org/remorhaz/int-rangesets/license)](https://packagist.org/packages/remorhaz/int-rangesets)

This small library allows manipulating integer sets.

# Requirements

- PHP 8.1, 8.2, 8.3.

# Installation

```shell script
composer require remorhaz/int-rangesets
```

# Usage
## Introduction
_Set_ is represented in a form of collection of continuous _ranges_; each range is represented by a pair of integers denoting it's first and last values.

## Range
Range cannot be empty, but can contain only one integer; in that case it's first and last values are the same.

```php
withStart(7);
```

## Range sets
All ranges in a set are normalized: they follow each other in ascending order and are separated by non-empty gaps, so none of them follow immediately after previous one or overlap.

Range sets are also immutable. Any operation on a set creates a new instance, leaving the original one intact.

```php
withRanges(new Range(4, 10));
```

Merging of ranges requires resources, so there's a fast, but unsafe way to initialize set with ranges. In this case constructing code must take full responsibility for normalization of ranges.

```php
createUnion($b);` |
| [Intersection](https://en.wikipedia.org/wiki/Intersection_(set_theory)) | A ∩ B | `$result = $a->createIntersection($b);` |
| [Symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference) | A ∆ B | `$result = $a->createSymmetricDifference($b);` |

# License
This library is licensed under [MIT license](./LICENSE).