The Map class

Introduction

A Map is a sequential collection of key-value pairs, almost identical to an array used in a similar context. Keys can be any type, but must be unique. Values are replaced if added to the map using the same key.

Strengths

  • Keys and values can be any type, including objects.
  • Supports array syntax (square brackets).
  • Insertion order is preserved.
  • Performance and memory efficiency is very similar to an array.
  • Automatically frees allocated memory when its size drops low enough.

Weaknesses

  • Can’t be converted to an array when objects are used as keys.

Class synopsis

Ds\Map
class Ds\Map implements Ds\Collection, ArrayAccess {
/* Constants */
const int Ds\Map::MIN_CAPACITY = 16;
/* Methods */
public void allocate(int $capacity)
public void apply(callable $callback)
public int capacity()
public void clear()
public Ds\Map copy()
public Ds\Map diff(Ds\Map $map)
public Ds\Map filter(callable $callback = ?)
public Ds\Pair first()
public mixed get(mixed $key, mixed $default = ?)
public bool hasKey(mixed $key)
public bool hasValue(mixed $value)
public Ds\Map intersect(Ds\Map $map)
public bool isEmpty()
public Ds\Set keys()
public void ksort(callable $comparator = ?)
public Ds\Map ksorted(callable $comparator = ?)
public Ds\Pair last()
public Ds\Map map(callable $callback)
public Ds\Map merge(mixed $values)
public Ds\Sequence pairs()
public void put(mixed $key, mixed $value)
public void putAll(mixed $pairs)
public mixed reduce(callable $callback, mixed $initial = ?)
public mixed remove(mixed $key, mixed $default = ?)
public void reverse()
public Ds\Map reversed()
public Ds\Pair skip(int $position)
public Ds\Map slice(int $index, int $length = ?)
public void sort(callable $comparator = ?)
public Ds\Map sorted(callable $comparator = ?)
public intfloat sum()
public array toArray()
public Ds\Map union(Ds\Map $map)
public Ds\Sequence values()
public Ds\Map xor(Ds\Map $map)
}

Predefined Constants

Ds\Map::MIN_CAPACITY

Changelog

Version Description
PECL ds 1.3.0 The class now implements ArrayAccess.

Table of Contents