Skip to content

xp-framework/collections

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Collections

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

Generic collections for the XP Framework

API

package util.collections {
  public interface util.collections.IList<T>
  public interface util.collections.Map<K, V>
  public interface util.collections.Set<T>

  public class util.collections.HashSet<T>
  public class util.collections.HashTable<K, V>
  public class util.collections.LRUBuffer<T>
  public class util.collections.Pair<K, V>
  public class util.collections.Queue<T>
  public class util.collections.Stack<T>
  public class util.collections.Vector<T>
}

Example: HashTable

$map= create('new util.collections.HashTable<string, com.example.Customer>');
$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

$list= create('new util.collections.Vector<com.example.Customer>');
$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