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
- Host: GitHub
- URL: https://github.com/xp-framework/collections
- Owner: xp-framework
- Created: 2015-11-08T12:18:48.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2024-03-23T23:46:53.000Z (almost 2 years ago)
- Last Synced: 2024-04-25T08:22:32.180Z (over 1 year ago)
- Topics: generic-collections, php, xp-framework
- Language: PHP
- Size: 101 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
Awesome Lists containing this project
README
Collections
===========
[](https://github.com/xp-framework/collections/actions)
[](https://github.com/xp-framework/core)
[](https://github.com/xp-framework/core/blob/master/LICENCE.md)
[](http://php.net/)
[](http://php.net/)
[](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)