The Set class

(PECL ds >= 1.0.0)

Introduction

A Set is a sequence of unique values. This implementation uses the same hash table as Ds\Map, where values are used as keys and the mapped value is ignored.

Strengths

  • Values can be any type, including objects.
  • Supports array syntax (square brackets).
  • Insertion order is preserved.
  • Automatically frees allocated memory when its size drops low enough.
  • add(), remove() and contains() are all O(1).

Weaknesses

  • Doesn’t support push(), pop(), insert(), shift(), or unshift().
  • get() is O(n) if there are deleted values in the buffer before the accessed index, O(1) otherwise.

Class synopsis

classDs\SetimplementsDs\Collection, ArrayAccess {
constintMIN_CAPACITY = 16;
publicadd(mixed...$values): void
publicallocate(int$capacity): void
publiccapacity(): int
publicclear(): void
publiccontains(mixed...$values): bool
publiccopy(): Ds\Set
publicdiff(Ds\Set$set): Ds\Set
publicfilter(callable$callback = ?): Ds\Set
publicfirst(): mixed
publicget(int$index): mixed
publicintersect(Ds\Set$set): Ds\Set
publicisEmpty(): bool
publicjoin(string$glue = ?): string
publiclast(): mixed
publicmerge(mixed$values): Ds\Set
publicreduce(callable$callback, mixed$initial = ?): mixed
publicremove(mixed...$values): void
publicreverse(): void
publicreversed(): Ds\Set
publicslice(int$index, int$length = ?): Ds\Set
publicsort(callable$comparator = ?): void
publicsorted(callable$comparator = ?): Ds\Set
publicsum(): int|float
publictoArray(): array
publicunion(Ds\Set$set): Ds\Set
publicxor(Ds\Set$set): Ds\Set
}

Predefined Constants

Ds\Set::MIN_CAPACITY

Changelog

VersionDescription
PECL ds 1.3.0 The class now implements ArrayAccess.

Table of Contents

To Top