PHPackages                             keyvanakbary/medusa - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. keyvanakbary/medusa

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

keyvanakbary/medusa
===================

Immutable and persistent collections

15445413PHP

Since May 11Pushed 10y ago7 watchersCompare

[ Source](https://github.com/keyvanakbary/medusa)[ Packagist](https://packagist.org/packages/keyvanakbary/medusa)[ RSS](/packages/keyvanakbary-medusa/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Medusa
======

[](#medusa)

[![Build Status](https://camo.githubusercontent.com/1e1ec570f25a54944a3d332a9574dece7aca33d2bfe4d8b1be7d57213b5e94b3/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6b657976616e616b626172792f6d65647573612e7376673f6272616e63683d6d6173746572)](http://travis-ci.org/keyvanakbary/medusa)

*Immutable* and *persistent* collections for PHP.

Life would be a lot simpler if we had *immutable* data structures. Code would be easier to understand, easy to test and free of side-effects. Being *immutable* is not all, these data structures must be efficient. By making them *persistent*, collections reuse internal structure to minimize the number of operations needed to represent altered versions of an instance of a collection.

Installation
------------

[](#installation)

To install this library, run the command below and you will get the latest version

```
composer require keyvanakbary/medusa

```

Usage
-----

[](#usage)

### Persistent Stack

[](#persistent-stack)

```
$s = Medusa\Stack\PersistentStack::createEmpty();

$s1 = $s->push(1);
$s2 = $s1->pop();
echo $s1->peek();//1
echo $s2->peek();//Runtime exception
```

#### Complexity

[](#complexity)

operationbig-OpushO(1)peekO(1)popO(1)isEmptyO(1)countO(1)### Persistent Queue

[](#persistent-queue)

```
$q = Medusa\Queue\PersistentQueue::createEmpty();

$q1 = $q->enqueue(1);
$q2 = $q1->dequeue();
echo $q1->peek();//1
echo $q2->peek();//Runtime exception
```

#### Complexity

[](#complexity-1)

operationbig-OisEmptyO(1)peekO(1)enqueueO(1)dequeueO(1) in average, O(n) in some casescountO(1)### Persistent AVL Tree

[](#persistent-avl-tree)

```
$t = Medusa\Tree\PersistentAvlTree::createEmpty();

$t1 = $t->add(1, 'one');
$t2 = $t1->remove(1);
echo $t1->search(1)->value();//one
echo $t2->lookup(1);//Runtime exception
```

#### Complexity

[](#complexity-2)

operationbig-OisEmptyO(1)valueO(1)keyO(1)addO(1)searchO(log(n))containsO(log(n))heightO(1)lookupO(log(n))### Persistent Red-Black Tree

[](#persistent-red-black-tree)

```
$t = Medusa\Tree\PersistentRedBlackTree::createEmpty();

$t1 = $t->add(1, 'one');
$t2 = $t1->remove(1);
echo $t1->search(1)->value();//one
echo $t2->lookup(1);//Runtime exception
```

#### Complexity

[](#complexity-3)

operationbig-OisEmptyO(1)valueO(1)keyO(1)addO(1)searchO(log(n))containsO(log(n))heightO(1)lookupO(log(n))minO(log(n))removeMinO(log(n))

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/75f9162f0d8091fa8af45f617ffcca643d0e196079ca1d2345bba4c28025ba76?d=identicon)[keyvanakbary](/maintainers/keyvanakbary)

---

Top Contributors

[![keyvanakbary](https://avatars.githubusercontent.com/u/717129?v=4)](https://github.com/keyvanakbary "keyvanakbary (22 commits)")[![magdkudama](https://avatars.githubusercontent.com/u/1323130?v=4)](https://github.com/magdkudama "magdkudama (3 commits)")[![Trismegiste](https://avatars.githubusercontent.com/u/1260026?v=4)](https://github.com/Trismegiste "Trismegiste (2 commits)")[![csomakk](https://avatars.githubusercontent.com/u/629462?v=4)](https://github.com/csomakk "csomakk (1 commits)")[![ivanbokii](https://avatars.githubusercontent.com/u/206985?v=4)](https://github.com/ivanbokii "ivanbokii (1 commits)")

---

Tags

immutable-datastructuresphp

### Embed Badge

![Health badge](/badges/keyvanakbary-medusa/health.svg)

```
[![Health](https://phpackages.com/badges/keyvanakbary-medusa/health.svg)](https://phpackages.com/packages/keyvanakbary-medusa)
```

###  Alternatives

[leo108/php_sdk_skeleton

A skeleton for PHP SDK development

261.1k2](/packages/leo108-php-sdk-skeleton)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
