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

https://github.com/xp-framework/collections

Generic Collections for the XP Framework
https://github.com/xp-framework/collections

generic-collections php xp-framework

Last synced: 5 months ago
JSON representation

Generic Collections for the XP Framework

Awesome Lists containing this project

README

          

Collections
===========

[![Build status on GitHub](https://github.com/xp-framework/collections/workflows/Tests/badge.svg)](https://github.com/xp-framework/collections/actions)
[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)
[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)
[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/)
[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)
[![Latest Stable Version](https://poser.pugx.org/xp-framework/collections/version.png)](https://packagist.org/packages/xp-framework/collections)

Generic collections for the XP Framework

API
---
```php
package util.collections {
public interface util.collections.IList
public interface util.collections.Map
public interface util.collections.Set

public class util.collections.HashSet
public class util.collections.HashTable
public class util.collections.LRUBuffer
public class util.collections.Pair
public class util.collections.Queue
public class util.collections.Stack
public class util.collections.Vector
}
```

Example: HashTable
------------------
```php
$map= create('new util.collections.HashTable');
$empty= $map->isEmpty();
$size= $map->size();

// Write values
$map['@example']= new Customer(0, 'Example customer');
$map->put('@friebe', new Customer(1, 'Timm Friebe'));

// Raises an exception
$map['@invalid']= new Date();

// Access
$customer= $map['@example'];
$customer= $map->get('@example');

// Test
if (isset($map['@example'])) {
// ...
}

// Will return NULL
$customer= $map['@nonexistant'];

// Remove
unset($map['@example']);
$map->remove('@example');

// Iteration
foreach ($map as $pair) {
echo $pair->key, ': ', $pair->value->toString(), "\n";
}
```

Example: Vector
---------------
```php
$list= create('new util.collections.Vector');
$empty= $list->isEmpty();
$size= $list->size();

// Write values
$list[]= new Customer(0, 'Example customer');
$list->add(new Customer(1, 'Timm Friebe'));

$list[0]= new Customer(0, 'Example customer');
$list->set(1, new Customer(1, 'Timm Friebe'));

// Raises an exception
$list[0]= new Date();

// Access
$customer= $list[0];
$customer= $list->get(0);

// Test
if (isset($list[1])) {
// ...
}

// Will return NULL
$customer= $list[1];

// Remove
unset($list[1]);
$list->remove(1);

// Iteration
foreach ($list as $customer) {
echo $customer->toString(), "\n";
}
```

Further reading
---------------
* [RFC #0193: Generics optimization](https://github.com/xp-framework/rfc/issues/193)
* [RFC #0106: Array access / iteration / type boxing / generics](https://github.com/xp-framework/rfc/issues/106)
* [HHVM: Hack Language Reference: Generic](http://docs.hhvm.com/manual/en/hack.generics.php)
* [PHP RFC: Introduce generics into PHP](https://wiki.php.net/rfc/generics)