Skip to content

Core\ObjectMap::count

$objectMap->count(): uint

Returns the number of entries in a Core\ObjectMap. The result is a uint, and it is 0 for an empty map.

Each key is counted once. When you call set with a key that is already in the map, the value is replaced and the count stays the same. remove makes the count one smaller, and clear makes it 0.

A key is found with ==. For an object of your own class, two different objects are two keys, even when all their property values are equal. Two equal Core\Time values, such as two Date values for the same day, are one key. The examples show this, and show how to use count to limit how many tasks run at the same time.

count does not walk the map, so it is fast even for a large map.

Return value

Returns uint. The number of keys; 0 for an empty map.

Examples

How many entries

<?nvs
// Shows how to count the entries of a map with `count`.
final class Task
{
public string $title;
public function constructor(string $title)
{
$this->title = $title;
}
}
Task $write = new Task('Write the report');
Task $send = new Task('Send the report');
Core\ObjectMap<Task, string> $owner = new Core\ObjectMap<Task, string>();
// A new map is empty. This prints 0.
echo $owner->count(), "\n";
$owner->set($write, 'Lea');
$owner->set($send, 'Sam');
// This prints 2.
echo $owner->count(), "\n";
// `set` with a key that is already in the map replaces its value.
// The map still has 2 entries, so this prints 2 again.
$owner->set($send, 'Lea');
echo $owner->count(), "\n";
Output
0
2
2

Equal objects are different keys

<?nvs
// Shows that `count` counts objects, not their contents. Two objects with the same data are two keys.
final class Point
{
public int $x;
public int $y;
public function constructor(int $x, int $y)
{
$this->x = $x;
$this->y = $y;
}
}
Point $first = new Point(1, 2);
Point $second = new Point(1, 2);
Core\ObjectMap<Point, string> $labels = new Core\ObjectMap<Point, string>();
$labels->set($first, 'start');
$labels->set($second, 'end');
// `$first` and `$second` have the same property values, but they are different objects. This prints 2.
echo $labels->count(), "\n";
// `$first` is the same object as before, so this replaces its value. This prints 2 again.
$labels->set($first, 'begin');
echo $labels->count(), "\n";
Output
2
2

Limit open uploads

<?nvs
// Shows how to limit the number of uploads that run at the same time.
final class Upload
{
public string $file;
public function constructor(string $file)
{
$this->file = $file;
}
}
final class Uploads
{
public static function start(Core\ObjectMap<Upload, int> $running, Upload $u, int $limit): void
{
// `count` is the number of uploads that run now. At the limit, the new one waits.
if ($running->count() >= ($limit as uint)) {
echo $u->file, ": wait\n";
return;
}
$running->set($u, 0);
echo $u->file, ': started, ', $running->count(), " running\n";
}
}
Core\ObjectMap<Upload, int> $running = new Core\ObjectMap<Upload, int>();
Upload $a = new Upload('a.jpg');
Upload $b = new Upload('b.jpg');
Upload $c = new Upload('c.jpg');
Uploads::start($running, $a, 2);
Uploads::start($running, $b, 2);
Uploads::start($running, $c, 2);
// When an upload finishes, `remove` takes it out, and the next one can start.
$running->remove($a);
Uploads::start($running, $c, 2);
Output
a.jpg: started, 1 running
b.jpg: started, 2 running
c.jpg: wait
c.jpg: started, 2 running