定制 dan-on/php-interval-tree 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dan-on/php-interval-tree

最新稳定版本:v2.1.0

Composer 安装命令:

composer require dan-on/php-interval-tree

包简介

Is an implementation of interval binary search tree according to Thomas Cormen book "Introduction to Algorithms".

README 文档

README

Latest Stable Version Total Downloads License PHP Version Require

Overview

Package dan-on/php-interval-tree is an implementation of self balancing binary search tree data structure called Red-Black Tree.

Based on interval tree described in "Introduction to Algorithms 3rd Edition", published by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein.

Complexity

Operation Best, Average, Worst
Insertion O(log(n))
Search O(log(n))
Remove O(log(n))
Space O(n)

Installing via Composer

composer require dan-on/php-interval-tree

Usage

Interval Tree

insert(IntervalInterface $interval, mixed $value): void

Insert new pair (interval + value) into interval tree

use Danon\IntervalTree\IntervalTree;

$tree = new IntervalTree();
$tree->insert(new NumericInterval(1, 10), 'val1');
$tree->insert(new NumericInterval(2, 5), 'val2');
$tree->insert(new NumericInterval(11, 12), 'val3');

findIntersections(IntervalInterface $interval): Iterator<Pair>

Find pairs which intervals intersect with given interval

$intersections = $tree->findIntersections(new NumericInterval(3, 5));
foreach($intersections as $pair) {
    $pair->getInterval()->getLow(); // 1, 2
    $pair->getInterval()->getHigh(); // 10, 5
    $pair->getValue(); // 'val1', 'val2'
}

hasIntersection(IntervalInterface $interval): bool

Returns true if interval has at least one intersection in tree

$tree->hasIntersection(new NumericInterval(3, 5)); // true

countIntersections(IntervalInterface $interval): int

Count intersections given interval in tree

$tree->countIntersections(new NumericInterval(3, 5)); // 2

remove(IntervalInterface $interval, $value): bool

Remove node from tree by interval and value

$tree->remove(new NumericInterval(11, 12), 'val3'); // true

exist(IntervalInterface $interval, $value): bool

Returns true if interval and value exist in the tree

$tree->exists(new NumericInterval(11, 12), 'val3'); // true

isEmpty(): bool

Returns true if tree is empty

$tree->isEmpty(); // false

getSize(): int

Get number of items stored in the interval tree

$tree->getSize(); // 3

Intervals

There are numeric and DateTimeInterface-based interval types included.

Numeric interval

use Danon\IntervalTree\Interval\NumericInterval;

// Instantiate numeric interval from array
$numericInterval = NumericInterval::fromArray([1, 100]);

// Instantiate numeric interval with constructor
$numericInterval = new NumericInterval(1, 100);

DateTime interval

use Danon\IntervalTree\Interval\DateTimeInterval;

// Instantiate DateTime interval from array
$dateTimeInterval = DateTimeInterval::fromArray([
    new DateTimeImmutable('2021-01-01 00:00:00'),
    new DateTimeImmutable('2021-01-02 00:00:00'),
]);

// Instantiate DateTime interval with constructor
$dateTimeInterval = new DateTimeInterval(
    new DateTimeImmutable('2021-01-01 00:00:00'), 
    new DateTimeImmutable('2021-01-02 00:00:00')
);

Tests

./vendor/bin/phpunit

统计信息

  • 总下载量: 14.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-11