Blog

  • PHP SPL Data structures

    SplDoublyLinkedList Functions

    add()
    Description: Add a new value at the given index in the doubly linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->add(0, "A");
    $list->add(1, "B");
    
    print_r($list);
    // Output: SplDoublyLinkedList Object ([0] => A [1] => B)

    bottom()
    Description: Peek the value of the node from the beginning of the doubly linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->push("A");
    $list->push("B");
    
    echo $list->bottom();
    // Output: A

    count()
    Description: Count the number of elements present in a doubly linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->push("A");
    $list->push("B");
    
    echo $list->count();
    // Output: 2

    push()
    Description: Push an element at the end of the doubly linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->push("A");
    $list->push("B");
    
    print_r($list);
    // Output: SplDoublyLinkedList Object ([0] => A [1] => B)

    unshift()
    Description: Add an element at the beginning of the doubly linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->push("B");
    $list->unshift("A");
    
    print_r($list);
    // Output: SplDoublyLinkedList Object ([0] => A [1] => B)

    top()
    Description: Return the value of the last (top) node in a doubly-linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->push("A");
    $list->push("B");
    
    echo $list->top();
    // Output: B

    pop()
    Description: Pop the node from the end of the doubly linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->push("A");
    $list->push("B");
    
    echo $list->pop();
    // Output: B

    isEmpty()
    Description: Check whether the doubly linked list is empty.
    Example:

    $list = new SplDoublyLinkedList();
    
    echo $list->isEmpty();
    // Output: 1 (true)

    rewind()
    Description: Rewind the iterator back to the start or beginning of the doubly linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->push("A");
    $list->push("B");
    
    $list->rewind();
    echo $list->current();
    // Output: A

    shift()
    Description: Remove the first element from the doubly linked list and return it.
    Example:

    $list = new SplDoublyLinkedList();
    $list->push("A");
    $list->push("B");
    
    echo $list->shift();
    // Output: A

    add()
    Description: Add a new value at the given index in the doubly linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->add(0, "A");
    $list->add(1, "B");
    
    print_r($list);
    // Output: SplDoublyLinkedList Object ([0] => A [1] => B)

    bottom()
    Description: Peek the value of the node from the beginning of the doubly linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->push("A");
    $list->push("B");
    
    echo $list->bottom();
    // Output: A

    count()
    Description: Count the number of elements present in a doubly linked list.
    Example:

    $list = new SplDoublyLinkedList();
    $list->push("A");
    $list->push("B");
    
    echo $list->count();
    // Output: 2

    SplFixedArray Functions

    count()
    Description: Return the number of elements in the SplFixedArray.
    Example:

    $array = new SplFixedArray(5);
    echo $array->count();
    // Output: 5

    current()
    Description: Get the current entry of the array iterator.
    Example:

    $array = new SplFixedArray(3);
    $array[0] = "A";
    $array[1] = "B";
    
    $array->rewind();
    echo $array->current();
    // Output: A

    getSize()
    Description: Get the size of the SplFixedArray.
    Example:

    $array = new SplFixedArray(4);
    echo $array->getSize();
    // Output: 4

    key()
    Description: Get the key of the current iterator index.
    Example:

    $array = new SplFixedArray(3);
    $array[0] = "A";
    
    $array->rewind();
    echo $array->key();
    // Output: 0

    next()
    Description: Move the iterator to the next entry.
    Example:

    $array = new SplFixedArray(3);
    $array[0] = "A";
    $array[1] = "B";
    
    $array->rewind();
    $array->next();
    echo $array->current();
    // Output: B

    offsetExists()
    Description: Check if the provided index exists in the SplFixedArray.
    Example:

    $array = new SplFixedArray(3);
    echo $array->offsetExists(2);
    // Output: 1 (true)

    offsetGet()
    Description: Get the value at the specified index in the SplFixedArray.
    Example:

    $array = new SplFixedArray(3);
    $array[1] = "B";
    echo $array->offsetGet(1);
    // Output: B

    offsetUnset()
    Description: Unset the value of the specified index in the SplFixedArray.
    Example:

    $array = new SplFixedArray(3);
    $array[1] = "B";
    $array->offsetUnset(1);
    
    print_r($array);
    // Output: SplFixedArray Object ([0] => , [1] => , [2] => )

    rewind()
    Description: Rewind the array iterator back to the start.
    Example:

    $array = new SplFixedArray(3);
    $array[0] = "A";
    $array[1] = "B";
    
    $array->next();
    $array->rewind();
    echo $array->current();
    // Output: A

    rewind()
    Description: Rewind the array iterator back to the start.
    Example:

    $array = new SplFixedArray(3);
    $array[0] = "A";
    $array[1] = "B";
    
    $array->next();
    $array->rewind();
    echo $array->current();
    // Output: A

    List of PHP SPL SplObjectStorage Functions

    addAll()
    Description: Add elements from another SplObjectStorage to the current one.
    Example:

    $storage1 = new SplObjectStorage();
    $storage2 = new SplObjectStorage();
    
    $obj1 = new stdClass();
    $obj2 = new stdClass();
    $obj3 = new stdClass();
    
    $storage1->attach($obj1);
    $storage2->attach($obj2);
    $storage2->attach($obj3);
    
    $storage1->addAll($storage2);
    echo $storage1->count();
    // Output: 3

    attach()
    Description: Add an object to the SplObjectStorage.
    Example:

    $storage = new SplObjectStorage();
    $obj = new stdClass();
    
    $storage->attach($obj);
    echo $storage->count();
    // Output: 1

    contains()
    Description: Check if a specific object exists in the SplObjectStorage.
    Example:

    $storage = new SplObjectStorage();
    $obj = new stdClass();
    
    $storage->attach($obj);
    echo $storage->contains($obj);
    // Output: 1 (true)

    count()
    Description: Get the number of objects in the SplObjectStorage.
    Example:

    $storage = new SplObjectStorage();
    echo $storage->count();
    // Output: 0

    current()
    Description: Get the current object in the iterator.
    Example:

    $storage = new SplObjectStorage();
    $obj1 = new stdClass();
    $obj2 = new stdClass();
    
    $storage->attach($obj1);
    $storage->attach($obj2);
    
    $storage->rewind();
    print_r($storage->current());
    // Output: Object(stdClass)

    detach()
    Description: Remove a specific object from the SplObjectStorage.
    Example:

    $storage = new SplObjectStorage();
    $obj = new stdClass();
    
    $storage->attach($obj);
    $storage->detach($obj);
    echo $storage->count();
    // Output: 0

    getInfo()
    Description: Retrieve the data associated with the current object.
    Example:

    $storage = new SplObjectStorage();
    $obj = new stdClass();
    
    $storage->attach($obj, "data");
    echo $storage->getInfo();
    // Output: data

    key()
    Description: Get the current iterator index.
    Example:

    $storage = new SplObjectStorage();
    $obj1 = new stdClass();
    $obj2 = new stdClass();
    
    $storage->attach($obj1);
    $storage->attach($obj2);
    
    $storage->rewind();
    echo $storage->key();
    // Output: 0

    next()
    Description: Move the iterator to the next object.
    Example:

    $storage = new SplObjectStorage();
    $obj1 = new stdClass();
    $obj2 = new stdClass();
    
    $storage->attach($obj1);
    $storage->attach($obj2);
    
    $storage->rewind();
    $storage->next();
    print_r($storage->current());
    // Output: Object(stdClass)

    offsetExists()
    Description: Check if an object exists in the SplObjectStorage.
    Example:

    $storage = new SplObjectStorage();
    $obj = new stdClass();
    
    $storage->attach($obj);
    echo $storage->offsetExists($obj);
    // Output: 1 (true)

    offsetGet()
    Description: Retrieve the data associated with the specified object.
    Example:

    $storage = new SplObjectStorage();
    $obj = new stdClass();
    
    $storage->attach($obj, "info");
    echo $storage->offsetGet($obj);
    // Output: info

    offsetSet()
    Description: Set data for a specific object in the storage.
    Example:

    $storage = new SplObjectStorage();
    $obj = new stdClass();
    
    $storage->offsetSet($obj, "info");
    echo $storage->getInfo();
    // Output: info

    offsetUnset()
    Description: Unset a specific object from the storage.
    Example:

    $storage = new SplObjectStorage();
    $obj = new stdClass();
    
    $storage->attach($obj);
    $storage->offsetUnset($obj);
    echo $storage->count();
    // Output: 0

    List of PHP SPL SplObjectStorage Functions

    __construct()
    Description: Constructs a queue that is implemented using a doubly-linked list.
    Example:

    $queue = new SplQueue();
    echo get_class($queue);
    // Output: SplQueue

    dequeue()
    Description: Removes and returns the node from the front of the queue.
    Example:

    $queue = new SplQueue();
    $queue->enqueue("First");
    $queue->enqueue("Second");
    
    echo $queue->dequeue();
    // Output: First

    enqueue()
    Description: Adds an element to the back of the queue.
    Example:

    $queue = new SplQueue();
    $queue->enqueue("First");
    $queue->enqueue("Second");
    
    echo $queue->bottom();
    // Output: First

    bottom()
    Description: Returns the value of the node at the bottom (front) of the queue without removing it.
    Example:

    $queue = new SplQueue();
    $queue->enqueue("First");
    $queue->enqueue("Second");
    
    echo $queue->bottom();
    // Output: First
  • Ds\Sequence Functions

    Ds\Deque Functions

    allocate()
    Description: This is used to allocate memory as specified in the argument.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    $sequence->allocate(10);
    
    echo $sequence->capacity();
    // Output: 10

    apply()
    Description: Update the values of the Deque by performing operations as defined by the callback function.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    $sequence->apply(fn($value) => $value * 2);
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 2 [1] => 4 [2] => 6)

    capacity()
    Description: Return the current capacity of the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2]);
    echo $sequence->capacity();
    // Output: 2

    contains()
    Description: Check whether the given value exists in the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->contains(2);
    // Output: 1 (true)

    filter()
    Description: Create a new sequence using a filter function.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3, 4]);
    $filtered = $sequence->filter(fn($value) => $value % 2 == 0);
    
    print_r($filtered);
    // Output: Ds\Vector Object ([0] => 2 [1] => 4)

    find()
    Description: Find the index of a value in the sequence. Returns false if not found.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->find(3);
    // Output: 2

    first()
    Description: Return the first element in the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->first();
    // Output: 1

    get()
    Description: Return the value at the given index.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->get(1);
    // Output: 2

    insert()
    Description: Insert a value at the given index in the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 4]);
    $sequence->insert(2, 3);
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3 [3] => 4)

    join()
    Description: Join all values in the sequence into a string.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->join(", ");
    // Output: 1, 2, 3

    last()
    Description: Return the last element in the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->last();
    // Output: 3

    map()
    Description: Return the result of applying a callback function to each value in the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    $mapped = $sequence->map(fn($value) => $value * 2);
    
    print_r($mapped);
    // Output: Ds\Vector Object ([0] => 2 [1] => 4 [2] => 6)

    merge()
    Description: Returns a sequence after adding all given values to the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2]);
    $merged = $sequence->merge([3, 4, 5]);
    
    print_r($merged);
    // Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5)

    pop()
    Description: Removes and returns the last value from the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->pop();
    // Output: 3
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 1 [1] => 2)

    push()
    Description: Adds values to the end of the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2]);
    $sequence->push(3, 4);
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3 [3] => 4)

    reduce()
    Description: Reduce the sequence to a single value using a callback function.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3, 4]);
    $result = $sequence->reduce(fn($carry, $value) => $carry + $value, 0);
    
    echo $result;
    // Output: 10

    remove()
    Description: Remove and return the value at a given index.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->remove(1);
    // Output: 2
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 1 [1] => 3)

    reverse()
    Description: Reverse the sequence in place.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    $sequence->reverse();
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 3 [1] => 2 [2] => 1)

    reversed()
    Description: Return a reversed copy of the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    $reversed = $sequence->reversed();
    
    print_r($reversed);
    // Output: Ds\Vector Object ([0] => 3 [1] => 2 [2] => 1)

    rotate()
    Description: Rotate the sequence elements by a given number of rotations.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3, 4]);
    $sequence->rotate(2);
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 3 [1] => 4 [2] => 1 [3] => 2)

    set()
    Description: Update the value at a given index in the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    $sequence->set(1, 42);
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 1 [1] => 42 [2] => 3)

    shift()
    Description: Remove and return the first element of the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->shift();
    // Output: 1
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 2 [1] => 3)

    slice()
    Description: Return a sub-sequence containing elements within the specified range.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3, 4, 5]);
    $sliced = $sequence->slice(1, 3);
    
    print_r($sliced);
    // Output: Ds\Vector Object ([0] => 2 [1] => 3 [2] => 4)

    sort()
    Description: Sort the sequence elements in place.
    Example:

    $sequence = new \Ds\Vector([3, 1, 2]);
    $sequence->sort();
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3)

    sorted()
    Description: Return a sorted copy of the sequence.
    Example:

    $sequence = new \Ds\Vector([3, 1, 2]);
    $sorted = $sequence->sorted();
    
    print_r($sorted);
    // Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3)

    sum()
    Description: Return the sum of all values in the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3, 4, 5]);
    echo $sequence->sum();
    // Output: 15

    toArray()
    Description: Convert the sequence into an array.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    $array = $sequence->toArray();
    
    print_r($array);
    // Output: Array ( [0] => 1 [1] => 2 [2] => 3 )

    unshift()
    Description: Add values to the beginning of the sequence.
    Example:

    $sequence = new \Ds\Vector([2, 3]);
    $sequence->unshift(1);
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3)

    apply()
    Description: Update all values in the sequence by applying a callback function to each value.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    $sequence->apply(fn($value) => $value * 2);
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 2 [1] => 4 [2] => 6)

    filter()
    Description: Create a new sequence containing only the elements that match the callback function’s condition.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3, 4, 5]);
    $filtered = $sequence->filter(fn($value) => $value % 2 === 0);
    
    print_r($filtered);
    // Output: Ds\Vector Object ([0] => 2 [1] => 4)

    find()
    Description: Find the index of the first occurrence of a value in the sequence.
    Example:

    $sequence = new \Ds\Vector([10, 20, 30, 40]);
    echo $sequence->find(30);
    // Output: 2

    first()
    Description: Return the first value in the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->first();
    // Output: 1

    last()
    Description: Return the last value in the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->last();
    // Output: 3

    contains()
    Description: Check if a sequence contains a given value.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    echo $sequence->contains(2);
    // Output: 1 (true)

    map()
    Description: Return a new sequence by applying a callback function to each value in the sequence.
    Example:

    $sequence = new \Ds\Vector([1, 2, 3]);
    $mapped = $sequence->map(fn($value) => $value * 10);
    
    print_r($mapped);
    // Output: Ds\Vector Object ([0] => 10 [1] => 20 [2] => 30)

    rotate()
    Description: Rotate the sequence elements by a given number of rotations (positive or negative).
    Example:

    $sequence = new \Ds\Vector([1, 2, 3, 4]);
    $sequence->rotate(-2);
    
    print_r($sequence);
    // Output: Ds\Vector Object ([0] => 3 [1] => 4 [2] => 1 [3] => 2)
  • Ds\Deque Functions

    Ds\Deque Functions

    allocate()
    Description: Allocate memory for a PriorityQueue class instance.
    Example:

    $deque = new \Ds\Deque();
    $deque->allocate(10);
    
    echo $deque->capacity();
    // Output: 10

    capacity()
    Description: Check the current capacity of a PriorityQueue instance.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    $deque->apply(function ($value) {
        return $value * 2;
    });
    
    print_r($deque);
    // Output: Ds\Deque Object ([0] => 2 [1] => 4 [2] => 6)

    capacity()
    Description: Get the current capacity of the Deque.
    Example:

    $deque = new \Ds\Deque();
    $deque->allocate(20);
    
    echo $deque->capacity();
    // Output: 20

    clear()
    Description: Clear the Deque by removing all elements.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    $deque->clear();
    
    echo $deque->isEmpty();
    // Output: 1 (true)

    copy()
    Description: Return a shallow copy of the Deque.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    $copy = $deque->copy();
    
    print_r($copy);
    // Output: Ds\Deque Object ([0] => 1 [1] => 2 [2] => 3)

    count()
    Description: Get the number of elements in the Deque.
    Examaple:

    $deque = new \Ds\Deque([1, 2, 3]);
    
    echo $deque->count();
    // Output: 3

    filter()
    Description: Filter out the elements from the Deque based on the operation defined in the callback function.
    Example:

    $deque = new \Ds\Deque([1, 2, 3, 4, 5]);
    $filtered = $deque->filter(function ($value) {
        return $value % 2 === 0;
    });
    
    print_r($filtered);
    // Output: Ds\Deque Object ([0] => 2 [1] => 4)

    find()
    Description: Find the index of the element in the Deque if the element is found.
    Example:

    $deque = new \Ds\Deque(["a", "b", "c"]);
    
    echo $deque->find("b");
    // Output: 1

    first()
    Description: Returns the first value in the Deque if it is not empty.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    
    echo $deque->first();
    // Output: 1

    get()
    Description: Return the value at the given index.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    
    echo $deque->get(1);
    // Output: 2

    insert()
    Description: Insert the value at the given index in the Deque.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    $deque->insert(1, 99);
    
    print_r($deque);
    // Output: Ds\Deque Object ([0] => 1 [1] => 99 [2] => 2 [3] => 3)

    toArray()
    Description: Convert a PriorityQueue into an associative array.
    Example:

    $deque = new \Ds\Deque();
    
    echo $deque->isEmpty();
    // Output: 1 (true)

    join()
    Description: Join all values in the Deque as a string.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    
    echo $deque->join(", ");
    // Output: 1, 2, 3

    last()
    Description: Return the last element of the Deque if it is not empty.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    
    echo $deque->last();
    // Output: 3

    map()
    Description: Returns the result of applying a callback to each value in the Deque.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    $mapped = $deque->map(function ($value) {
        return $value * 10;
    });
    
    print_r($mapped);
    // Output: Ds\Deque Object ([0] => 10 [1] => 20 [2] => 30)

    merge()
    Description: Returns the result of adding all given values to the Deque.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push(10, 1);
    $pq->push(20, 2);
    $pq->push(30, 3);
    
    $sum = array_reduce($pq->toArray(), function ($carry, $item) {
        return $carry + $item[0]; // Sum all values
    }, 0);
    
    echo $sum;
    // Output: 60

    reversed()
    Description: Get a reversed version of the PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $reversed = array_reverse($pq->toArray());
    
    print_r($reversed);
    // Output: Array ( [0] => One [1] => Two [2] => Three )

    contains()
    Description: Check whether a PriorityQueue contains a specific value.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    
    echo $pq->contains("One") ? "Yes" : "No";
    // Output: Yes

    merge()
    Description: Merge another collection into the PriorityQueue and maintain priorities.
    Example:

    $pq1 = new \Ds\PriorityQueue();
    $pq1->push("One", 1);
    $pq1->push("Two", 2);
    
    $pq2 = new \Ds\PriorityQueue();
    $pq2->push("Three", 3);
    $pq2->push("Four", 4);
    
    foreach ($pq2->toArray() as $item) {
        $pq1->push($item[0], $item[1]);
    }
    
    print_r($pq1->toArray());
    // Output: Array ( [0] => Four [1] => Three [2] => Two [3] => One )

    sort()
    Description: Sort elements in-place by their priorities in descending order.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 3);
    $pq->push("Two", 1);
    $pq->push("Three", 2);
    
    $sorted = $pq->toArray();
    usort($sorted, function ($a, $b) {
        return $b[1] - $a[1];
    });
    
    print_r($sorted);
    // Output: Array ( [0] => One [1] => Three [2] => Two )

    find()
    Description: Find and return an element in the PriorityQueue by a condition.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $found = array_filter($pq->toArray(), function ($item) {
        return $item[1] === 2; // Find item with priority 2
    });
    
    print_r($found);
    // Output: Array ( [0] => Two )

    splice()
    Description: Remove and return a subset of elements from the PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $subset = array_slice($pq->toArray(), 1, 2);
    
    print_r($subset);
    // Output: Array ( [0] => Two [1] => One )

    reverse()
    Description: Reverse the order of elements in a PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $reversed = array_reverse($pq->toArray());
    
    print_r($reversed);
    // Output: Array ( [0] => One [1] => Two [2] => Three )

    maxPriority()
    Description: Find the maximum priority present in the PriorityQueue.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    echo $deque->shift();
    // Output: 1
    
    print_r($deque);
    // Output: Ds\Deque Object ([0] => 2 [1] => 3)

    slice()
    Description: Return a sub-Deque containing elements within the specified index range.
    Example:

    $deque = new \Ds\Deque([1, 2, 3, 4, 5]);
    $sliced = $deque->slice(1, 3);
    
    print_r($sliced);
    // Output: Ds\Deque Object ([0] => 2 [1] => 3 [2] => 4)

    sort()
    Description: Sort the Deque in place by arranging elements in increasing order.
    Example:

    $deque = new \Ds\Deque([3, 1, 2]);
    $deque->sort();
    
    print_r($deque);
    // Output: Ds\Deque Object ([0] => 1 [1] => 2 [2] => 3)

    sorted()
    Description: Return a copy of the Deque with elements sorted in ascending order.
    Example:

    $deque = new \Ds\Deque([3, 1, 2]);
    $sorted = $deque->sorted();
    
    print_r($sorted);
    // Output: Ds\Deque Object ([0] => 1 [1] => 2 [2] => 3)

    sum()
    Description: Return the sum of all elements in the Deque.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    
    echo $deque->sum();
    // Output: 6

    toArray()
    Description: Convert the Deque to an array.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    $array = $deque->toArray();
    
    print_r($array);
    // Output: Array ([0] => 1 [1] => 2 [2] => 3)

    unshift()
    Description: Add a value to the front of the Deque.
    Example:

    $deque = new \Ds\Deque([2, 3]);
    $deque->unshift(1);
    
    print_r($deque);
    // Output: Ds\Deque Object ([0] => 1 [1] => 2 [2] => 3)

    filter()
    Description: Filter out the elements from the Deque based on the condition defined in the callback function.
    Example:

    find()
    Description: Find the index of the specified element in the Deque. Returns false if not found.
    Example:

    $deque = new \Ds\Deque([1, 2, 3, 4, 5]);
    $filtered = $deque->filter(fn($value) => $value % 2 == 0);
    
    print_r($filtered);
    // Output: Ds\Deque Object ([0] => 2 [1] => 4)

    first()
    Description: Return the first element in the Deque.
    Example:

    $deque = new \Ds\Deque([1, 2, 3, 4]);
    echo $deque->find(3);
    // Output: 2

    first()
    Description: Return the first element in the Deque.
    Example:

    $deque = new \Ds\Deque([1, 2, 3]);
    echo $deque->first();
    // Output: 1
    $deque = new \Ds\Deque([1, 2, 3]);
    echo $deque->first();
    // Output: 1
  • Ds\PriorityQueue Functions

    Basic Ds\PriorityQueue Functions

    allocate()
    Description: Allocate memory for a PriorityQueue class instance.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->allocate(10);
    echo $pq->capacity();
    // Output: 10

    capacity()
    Description: Check the current capacity of a PriorityQueue instance.
    Example:

    $pq = new \Ds\PriorityQueue();
    echo $pq->capacity();
    // Output: 10 (default or allocated capacity)

    clear()
    Description: Clear all of the elements from a PriorityQueue instance.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->clear();
    echo $pq->count();
    // Output: 0

    clear()
    Description: Removes all values from the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    $set->clear();
    print_r($set);
    // Output: Ds\Set Object ()

    copy()
    Description: Create a shallow copy of a particular PriorityQueue instance.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $copy = $pq->copy();
    print_r($copy->toArray());
    // Output: Array ( [0] => Two [1] => One ) (based on priority)

    count()
    Description: Get the count of elements present in a PriorityQueue instance.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    var_dump($set->contains(2));
    // Output: (bool true)

    copy()
    Description: Returns a shallow copy of the set.
    Example:

    $result = gmp_div_qr("10", "3");
    // Output: ([quotient => 3, remainder => 1])

    count()
    Description: Counts the number of values in the set.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    echo $pq->count();
    // Output: 2

    peek()
    Description: Get the value present at the front of a PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    echo $pq->peek();
    // Output: Two (highest priority)

    pop()
    Description: Remove and return the value present at the top of the PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    echo $pq->pop();
    // Output: Two

    push()
    Description: Push or insert values in a PriorityQueue instance with a given priority.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    print_r($pq->toArray());
    // Output: Array ( [0] => Two [1] => One ) (based on priority)

    toArray()
    Description: Convert a PriorityQueue into an associative array.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    print_r($pq->toArray());
    // Output: Array ( [0] => Two [1] => One )

    union()
    Description: Create a new PriorityQueue that combines elements from two PriorityQueue instances.
    Example:

    $pq1 = new \Ds\PriorityQueue();
    $pq1->push("One", 1);
    $pq1->push("Two", 2);
    
    $pq2 = new \Ds\PriorityQueue();
    $pq2->push("Three", 3);
    $pq2->push("Four", 4);
    
    $union = new \Ds\PriorityQueue();
    
    foreach ($pq1 as $item) {
        $union->push($item[0], $item[1]);
    }
    foreach ($pq2 as $item) {
        $union->push($item[0], $item[1]);
    }
    
    print_r($union->toArray());
    // Output: Array ( [0] => Four [1] => Three [2] => Two [3] => One )

    intersect()
    Description: Create a new PriorityQueue containing only the common elements of two PriorityQueue instances.
    Example:

    $pq1 = new \Ds\PriorityQueue();
    $pq1->push("One", 1);
    $pq1->push("Two", 2);
    
    $pq2 = new \Ds\PriorityQueue();
    $pq2->push("Two", 2);
    $pq2->push("Three", 3);
    
    $intersection = [];
    foreach ($pq1 as $item1) {
        foreach ($pq2 as $item2) {
            if ($item1[0] === $item2[0] && $item1[1] === $item2[1]) {
                $intersection[] = $item1;
            }
        }
    }
    
    print_r($intersection);
    // Output: Array ( [0] => Array ( [0] => Two [1] => 2 ) )

    filter()
    Description: Filter elements of a PriorityQueue based on a callback function.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $filtered = array_filter($pq->toArray(), function ($item) {
        return $item[1] > 1; // Keep elements with priority > 1
    });
    
    print_r($filtered);
    // Output: Array ( [0] => Two [1] => Three )

    reduce()
    Description: Reduce the elements of the PriorityQueue to a single value using a callback function.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push(10, 1);
    $pq->push(20, 2);
    $pq->push(30, 3);
    
    $sum = array_reduce($pq->toArray(), function ($carry, $item) {
        return $carry + $item[0]; // Sum all values
    }, 0);
    
    echo $sum;
    // Output: 60

    reversed()
    Description: Get a reversed version of the PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $reversed = array_reverse($pq->toArray());
    
    print_r($reversed);
    // Output: Array ( [0] => One [1] => Two [2] => Three )

    contains()
    Description: Check whether a PriorityQueue contains a specific value.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    
    echo $pq->contains("One") ? "Yes" : "No";
    // Output: Yes

    merge()
    Description: Merge another collection into the PriorityQueue and maintain priorities.
    Example:

    $pq1 = new \Ds\PriorityQueue();
    $pq1->push("One", 1);
    $pq1->push("Two", 2);
    
    $pq2 = new \Ds\PriorityQueue();
    $pq2->push("Three", 3);
    $pq2->push("Four", 4);
    
    foreach ($pq2->toArray() as $item) {
        $pq1->push($item[0], $item[1]);
    }
    
    print_r($pq1->toArray());
    // Output: Array ( [0] => Four [1] => Three [2] => Two [3] => One )

    sort()
    Description: Sort elements in-place by their priorities in descending order.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 3);
    $pq->push("Two", 1);
    $pq->push("Three", 2);
    
    $sorted = $pq->toArray();
    usort($sorted, function ($a, $b) {
        return $b[1] - $a[1];
    });
    
    print_r($sorted);
    // Output: Array ( [0] => One [1] => Three [2] => Two )

    find()
    Description: Find and return an element in the PriorityQueue by a condition.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $found = array_filter($pq->toArray(), function ($item) {
        return $item[1] === 2; // Find item with priority 2
    });
    
    print_r($found);
    // Output: Array ( [0] => Two )

    splice()
    Description: Remove and return a subset of elements from the PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $subset = array_slice($pq->toArray(), 1, 2);
    
    print_r($subset);
    // Output: Array ( [0] => Two [1] => One )

    reverse()
    Description: Reverse the order of elements in a PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $reversed = array_reverse($pq->toArray());
    
    print_r($reversed);
    // Output: Array ( [0] => One [1] => Two [2] => Three )

    maxPriority()
    Description: Find the maximum priority present in the PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $maxPriority = max(array_column($pq->toArray(), 1));
    
    echo $maxPriority;
    // Output: 3

    minPriority()
    Description: Find the minimum priority present in the PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $minPriority = min(array_column($pq->toArray(), 1));
    
    echo $minPriority;
    // Output: 1

    extractWithPriority()
    Description: Extract all elements with a specific priority.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Another One", 1);
    
    $priorityToExtract = 1;
    $extracted = array_filter($pq->toArray(), function ($item) use ($priorityToExtract) {
        return $item[1] === $priorityToExtract;
    });
    
    print_r($extracted);
    // Output: Array ( [0] => One [1] => Another One )

    priorities()
    Description: Get a list of all unique priorities in the PriorityQueue.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    $pq->push("Another One", 1);
    
    $uniquePriorities = array_unique(array_column($pq->toArray(), 1));
    
    print_r($uniquePriorities);
    // Output: Array ( [0] => 1 [1] => 2 [2] => 3 )

    mergeWithPriorityOverride()
    Description: Merge two PriorityQueue instances, overriding the priority of duplicate elements.
    Example:

    $pq1 = new \Ds\PriorityQueue();
    $pq1->push("One", 1);
    $pq1->push("Two", 2);
    
    $pq2 = new \Ds\PriorityQueue();
    $pq2->push("Two", 5);
    $pq2->push("Three", 3);
    
    $merged = [];
    foreach ($pq1->toArray() as $item) {
        $merged[$item[0]] = $item[1];
    }
    foreach ($pq2->toArray() as $item) {
        $merged[$item[0]] = $item[1]; // Override priority if the key exists
    }
    
    $result = new \Ds\PriorityQueue();
    foreach ($merged as $value => $priority) {
        $result->push($value, $priority);
    }
    
    print_r($result->toArray());
    // Output: Array ( [0] => Two [1] => Three [2] => One )

    filterByPriorityRange()
    Description: Filter elements with priorities within a given range.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $rangeStart = 1;
    $rangeEnd = 2;
    
    $filtered = array_filter($pq->toArray(), function ($item) use ($rangeStart, $rangeEnd) {
        return $item[1] >= $rangeStart && $item[1] <= $rangeEnd;
    });
    
    print_r($filtered);
    // Output: Array ( [0] => One [1] => Two )

    topN()
    Description: Retrieve the top N elements based on priority.
    Example:

    $pq = new \Ds\PriorityQueue();
    $pq->push("One", 1);
    $pq->push("Two", 2);
    $pq->push("Three", 3);
    
    $topN = 2;
    $sorted = $pq->toArray();
    usort($sorted, function ($a, $b) {
        return $b[1] - $a[1];
    });
    
    print_r(array_slice($sorted, 0, $topN));
    // Output: Array ( [0] => Three [1] => Two )
  • Ds\Queue Functions

    Basic Ds\Queue Functions

    allocate()
    Description: Allocate memory for a Queue instance.
    Example:

    $queue = new \Ds\Queue();
    $queue->allocate(10);
    echo $queue->capacity();
    // Output: 10

    capacity()
    Description: Check the current capacity of a Queue instance.
    Example:

    $queue = new \Ds\Queue();
    $queue->allocate(5);
    echo $queue->capacity();
    // Output: 5

    clear()
    Description: Clear all elements from a Queue instance.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    $queue->clear();
    print_r($queue);
    // Output: Ds\Queue Object ( )

    copy()
    Description: Create a shallow copy of a particular Queue instance.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    $copiedQueue = $queue->copy();
    print_r($copiedQueue);
    // Output: Ds\Queue Object ( [0] => 1 [1] => 2 [2] => 3 )

    copy()
    Description: Create a shallow copy of a particular Queue instance.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    $copiedQueue = $queue->copy();
    print_r($copiedQueue);
    // Output: Ds\Queue Object ( [0] => 1 [1] => 2 [2] => 3 )

    count()
    Description: Get the count of elements present in a Queue instance.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    echo $queue->count();
    // Output: 3

    isEmpty()
    Description: Check whether a particular Queue instance is empty or not.
    Example:

    $queue = new \Ds\Queue();
    echo $queue->isEmpty();
    // Output: 1 (true)

    peek()
    Description: Get the value present at the front of a Queue.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    echo $queue->peek();
    // Output: 1

    pop()
    Description: Remove and return the value present at the front of the Queue.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    echo $queue->pop();
    // Output: 1

    push()
    Description: Insert values into the Queue.
    Example:

    $queue = new \Ds\Queue([1, 2]);
    $queue->push(3, 4);
    print_r($queue);
    // Output: Ds\Queue Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

    toArray()
    Description: Convert a Queue into an array.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    print_r($queue->toArray());
    // Output: Array ( [0] => 1 [1] => 2 [2] => 3 )

    reverse() (Custom Implementation)
    Description: Reverse the order of elements in the Queue.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    $reversedQueue = new \Ds\Queue(array_reverse($queue->toArray()));
    print_r($reversedQueue);
    // Output: Ds\Queue Object ( [0] => 3 [1] => 2 [2] => 1 )

    merge() (Custom Implementation)
    Description: Merge the current Queue with another iterable and return a new Queue.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    $mergedQueue = new \Ds\Queue([...$queue->toArray(), ...[4, 5]]);
    print_r($mergedQueue);
    // Output: Ds\Queue Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

    filter() (Custom Implementation)
    Description: Filter elements in the Queue based on a condition.
    Example:

    $queue = new \Ds\Queue([1, 2, 3, 4]);
    $filteredQueue = new \Ds\Queue(array_filter($queue->toArray(), fn($value) => $value % 2 === 0));
    print_r($filteredQueue);
    // Output: Ds\Queue Object ( [0] => 2 [1] => 4 )

    find() (Custom Implementation)
    Description: Find a specific element in the Queue.
    Example:

    $queue = new \Ds\Queue([1, 2, 3, 4]);
    $index = array_search(3, $queue->toArray());
    echo $index !== false ? "Found at index $index" : "Not found";
    // Output: Found at index 2

    rotate() (Custom Implementation)
    Description: Rotate the Queue by moving the first element to the end.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    $array = $queue->toArray();
    array_push($array, array_shift($array));
    $rotatedQueue = new \Ds\Queue($array);
    print_r($rotatedQueue);
    // Output: Ds\Queue Object ( [0] => 2 [1] => 3 [2] => 1 )

    sum() (Custom Implementation)
    Description: Calculate the sum of all elements in the Queue.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    echo array_sum($queue->toArray());
    // Output: 6

    merge()
    Description: Merges another set or array into the current set.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    echo array_sum($queue->toArray());
    // Output: 6

    product() (Custom Implementation)
    Description: Calculate the product of all elements in the Queue.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    echo array_product($queue->toArray());
    // Output: 6

    chunk() (Custom Implementation)
    Description: Split the Queue into chunks of a specified size.
    Example:

    $queue = new \Ds\Queue([1, 2, 3, 4, 5]);
    $chunks = array_chunk($queue->toArray(), 2);
    foreach ($chunks as $chunk) {
        print_r(new \Ds\Queue($chunk));
    }
    // Output:
    // Ds\Queue Object ( [0] => 1 [1] => 2 )
    // Ds\Queue Object ( [0] => 3 [1] => 4 )
    // Ds\Queue Object ( [0] => 5 )

    flatten() (Custom Implementation)
    Description: Flatten a multi-dimensional Queue into a single-level Queue.
    Example:

    $queue = new \Ds\Queue([[1, 2], [3, 4], 5]);
    $flattenedQueue = new \Ds\Queue(array_merge(...$queue->toArray()));
    print_r($flattenedQueue);
    // Output: Ds\Queue Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

    ksort() (Custom Implementation)
    Description: Sort the elements of the Queue in ascending order.
    Example:

    $queue = new \Ds\Queue([3, 1, 4, 2]);
    $sortedQueue = new \Ds\Queue(sort($queue->toArray()) ? $queue->toArray() : []);
    print_r($sortedQueue);
    // Output: Ds\Queue Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

    unique() (Custom Implementation)
    Description: Remove duplicate elements from the Queue.
    Example:

    $queue = new \Ds\Queue([1, 2, 2, 3, 4, 4]);
    $uniqueQueue = new \Ds\Queue(array_unique($queue->toArray()));
    print_r($uniqueQueue);
    // Output: Ds\Queue Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

    shuffle() (Custom Implementation)
    Description: Randomly shuffle the elements of the Queue.
    Example:

    $queue = new \Ds\Queue([1, 2, 3, 4]);
    $array = $queue->toArray();
    shuffle($array);
    $shuffledQueue = new \Ds\Queue($array);
    print_r($shuffledQueue);
    // Output: Ds\Queue Object ( [0] => 3 [1] => 1 [2] => 4 [3] => 2 ) // (order may vary)

    intersect() (Custom Implementation)
    Description: Find the intersection of elements between two Queue instances.
    Example:

    $queue1 = new \Ds\Queue([1, 2, 3, 4]);
    $queue2 = new \Ds\Queue([3, 4, 5, 6]);
    $intersection = new \Ds\Queue(array_intersect($queue1->toArray(), $queue2->toArray()));
    print_r($intersection);
    // Output: Ds\Queue Object ( [0] => 3 [1] => 4 )

    diff() (Custom Implementation)
    Description: Find the difference between two Queue instances.
    Example:

    $queue1 = new \Ds\Queue([1, 2, 3, 4]);
    $queue2 = new \Ds\Queue([3, 4, 5, 6]);
    $difference = new \Ds\Queue(array_diff($queue1->toArray(), $queue2->toArray()));
    print_r($difference);
    // Output: Ds\Queue Object ( [0] => 1 [1] => 2 )

    union() (Custom Implementation)
    Description: Combine elements from two Queue instances, removing duplicates.
    Example:

    $queue1 = new \Ds\Queue([1, 2, 3]);
    $queue2 = new \Ds\Queue([3, 4, 5]);
    $union = new \Ds\Queue(array_unique(array_merge($queue1->toArray(), $queue2->toArray())));
    print_r($union);
    // Output: Ds\Queue Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

    slice() (Custom Implementation)
    Description: Extract a portion of the Queue based on the given offset and length.
    Example:

    $queue = new \Ds\Queue([1, 2, 3, 4, 5]);
    $slicedQueue = new \Ds\Queue(array_slice($queue->toArray(), 1, 3));
    print_r($slicedQueue);
    // Output: Ds\Queue Object ( [0] => 2 [1] => 3 [2] => 4 )

    partition() (Custom Implementation)
    Description: Partition the Queue into two based on a condition.
    Example:

    $queue = new \Ds\Queue([1, 2, 3, 4, 5]);
    $even = new \Ds\Queue(array_filter($queue->toArray(), fn($value) => $value % 2 === 0));
    $odd = new \Ds\Queue(array_filter($queue->toArray(), fn($value) => $value % 2 !== 0));
    print_r($even);
    print_r($odd);
    // Output:
    // Ds\Queue Object ( [0] => 2 [1] => 4 )
    // Ds\Queue Object ( [0] => 1 [1] => 3 [2] => 5 )

    partition() (Custom Implementation)
    Description: Partition the Queue into two based on a condition.
    Example:

    $queue = new \Ds\Queue([1, 2, 3, 4, 5]);
    $even = new \Ds\Queue(array_filter($queue->toArray(), fn($value) => $value % 2 === 0));
    $odd = new \Ds\Queue(array_filter($queue->toArray(), fn($value) => $value % 2 !== 0));
    print_r($even);
    print_r($odd);
    // Output:
    // Ds\Queue Object ( [0] => 2 [1] => 4 )
    // Ds\Queue Object ( [0] => 1 [1] => 3 [2] => 5 )

    reverseEach() (Custom Implementation)
    Description: Reverse the elements of a Queue and process each one.
    Example:

    $queue = new \Ds\Queue([1, 2, 3, 4]);
    $reversedQueue = array_reverse($queue->toArray());
    foreach ($reversedQueue as $element) {
        echo $element . " ";
    }
    // Output: 4 3 2 1

    reduce() (Custom Implementation)
    Description: Reduce the Queue to a single value by applying a callback function.
    Example:

    $queue = new \Ds\Queue([1, 2, 3, 4]);
    $result = array_reduce($queue->toArray(), fn($carry, $item) => $carry + $item, 0);
    echo $result;
    // Output: 10

    map() (Custom Implementation)
    Description: Apply a transformation function to each element of the Queue.
    Example:

    $queue = new \Ds\Queue([1, 2, 3]);
    $mappedQueue = new \Ds\Queue(array_map(fn($value) => $value * 2, $queue->toArray()));
    print_r($mappedQueue);
    // Output: Ds\Queue Object ( [0] => 2 [1] => 4 [2] => 6 )
  • Basic Ds\Stack Functions

    Basic Ds\Stack Functions

    clear()
    Description: Removes all elements from a Stack and clears it.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $stack->clear();
    print_r($stack);
    // Output: Ds\Stack Object ()

    copy()
    Description: Creates a shallow copy of the original stack and returns the copied stack.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $copiedStack = $stack->copy();
    print_r($copiedStack);
    // Output: Ds\Stack Object ( [0] => 1 [1] => 2 [2] => 3 )

    isEmpty()
    Description: Checks whether a Stack is empty or not.
    Example:

    $stack = new \Ds\Stack();
    echo $stack->isEmpty();
    // Output: 1 (true)

    clear()
    Description: Removes all values from the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    $set->clear();
    print_r($set);
    // Output: Ds\Set Object ()

    peek()
    Description: Gets the element present at the top of the Stack instance without removing it.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    echo $stack->peek();
    // Output: 3

    pop()
    Description: Removes the element present at the top of the Stack instance and returns it.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    echo $stack->pop();
    // Output: 3
    print_r($stack);
    // Output: Ds\Stack Object ( [0] => 1 [1] => 2 )

    push()
    Description: Adds elements to the end of the stack.
    Example:

    $stack = new \Ds\Stack([1, 2]);
    $stack->push(3, 4);
    print_r($stack);
    // Output: Ds\Stack Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

    count()
    Description: Returns the number of elements present in the stack.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    echo $stack->count();
    // Output: 3

    allocate()
    Description: Allocates enough memory for a required capacity. This can improve performance when the stack grows.
    Example:

    $stack = new \Ds\Stack();
    $stack->allocate(10);
    echo $stack->capacity();
    // Output: 10

    capacity()
    Description: Returns the current capacity of the stack.
    Example:

    $stack = new \Ds\Stack();
    $stack->allocate(20);
    echo $stack->capacity();
    // Output: 20

    contains()
    Description: Checks if the stack contains one or more specified values.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    echo $stack->contains(2);
    // Output: 1 (true)

    merge()
    Description: Merges the stack with another iterable and returns a new stack.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $mergedStack = $stack->merge([4, 5]);
    print_r($mergedStack);
    // Output: Ds\Stack Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

    reverse()
    Description: Reverses the elements of the stack in place.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $stack->reverse();
    print_r($stack);
    // Output: Ds\Stack Object ( [0] => 3 [1] => 2 [2] => 1 )

    reversed()
    Description: Returns a new stack with elements in reverse order.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $reversedStack = $stack->reversed();
    print_r($reversedStack);
    // Output: Ds\Stack Object ( [0] => 3 [1] => 2 [2] => 1 )

    reduce()
    Description: Reduces the stack to a single value using a callback function.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $sum = $stack->reduce(function ($carry, $item) {
        return $carry + $item;
    }, 0);
    echo $sum;
    // Output: 6

    filter()
    Description: 
    Filters the stack based on a callback function and returns a new stack.
    Example:

    $stack = new \Ds\Stack([1, 2, 3, 4]);
    $filteredStack = $stack->filter(function ($value) {
        return $value % 2 === 0;
    });
    print_r($filteredStack);
    // Output: Ds\Stack Object ( [0] => 2 [1] => 4 )

    map()
    Description: Applies a callback function to all elements in the stack and returns a new stack with the results.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $mappedStack = $stack->map(function ($value) {
        return $value * 2;
    });
    print_r($mappedStack);
    // Output: Ds\Stack Object ( [0] => 2 [1] => 4 [2] => 6 )

    merge()
    Description: Merges another set or array into the current set.
    Example:

    $set = new \Ds\Set([1, 2]);
    $result = $set->merge([3, 4]);
    print_r($result);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3, [3] => 4)

    slice()
    Description: Returns a subset of the stack as a new stack.
    Example:

    $stack = new \Ds\Stack([1, 2, 3, 4]);
    $slicedStack = $stack->slice(1, 2);
    print_r($slicedStack);
    // Output: Ds\Stack Object ( [0] => 2 [1] => 3 )

    sort()
    Description: Sorts the elements of the stack in place.
    Example:

    $stack = new \Ds\Stack([3, 1, 2]);
    $stack->sort();
    print_r($stack);
    // Output: Ds\Stack Object ( [0] => 1 [1] => 2 [2] => 3 )

    reverse()
    Description: Reverses the order of elements in the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    $set->reverse();
    print_r($set);
    // Output: Ds\Set Object ([0] => 3, [1] => 2, [2] => 1)

    sorted()
    Description: Returns a new stack with elements sorted in ascending order.
    Example:

    $stack = new \Ds\Stack([3, 1, 2]);
    $sortedStack = $stack->sorted();
    print_r($sortedStack);
    // Output: Ds\Stack Object ( [0] => 1 [1] => 2 [2] => 3 )

    join()
    Description: Joins all elements of the stack into a string using a specified delimiter.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    echo $stack->join(", ");
    // Output: 1, 2, 3

    findIndex() (Custom Implementation)
    Description: Finds the index of the first occurrence of a specific value.
    Example:

    $stack = new \Ds\Stack([1, 2, 3, 2]);
    $index = array_search(2, $stack->toArray());
    echo $index;
    // Output: 1

    containsAll() (Custom Implementation)
    Description: Checks if the stack contains all the specified values.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $containsAll = array_diff([1, 2], $stack->toArray()) === [];
    echo $containsAll;
    // Output: 1 (true)

    sum()
    Description: Returns the sum of all elements in the set.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    echo array_sum($stack->toArray());
    // Output: 6

    unique() (Custom Implementation)
    Description: Removes duplicate elements from the stack and returns a new stack.
    Example:

    $stack = new \Ds\Stack([1, 2, 2, 3]);
    $uniqueStack = new \Ds\Stack(array_unique($stack->toArray()));
    print_r($uniqueStack);
    // Output: Ds\Stack Object ( [0] => 1 [1] => 2 [2] => 3 )

    shuffle() (Custom Implementation)
    Description: Randomly shuffles the elements of the stack and returns a new stack.
    Example:

    $stack = new \Ds\Stack([1, 2, 3, 4]);
    $shuffledArray = $stack->toArray();
    shuffle($shuffledArray);
    $shuffledStack = new \Ds\Stack($shuffledArray);
    print_r($shuffledStack);
    // Output: Ds\Stack Object ( [0] => 3 [1] => 1 [2] => 4 [3] => 2 ) (varies)

    reverseElements() (Custom Implementation)
    Description: Reverses the stack elements and returns a new stack.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $reversedStack = new \Ds\Stack(array_reverse($stack->toArray()));
    print_r($reversedStack);
    // Output: Ds\Stack Object ( [0] => 3 [1] => 2 [2] => 1 )

    product() (Custom Implementation)
    Description: Calculates the product of all elements in the stack.
    Example:

    $stack = new \Ds\Stack([1, 2, 3, 4]);
    $product = array_product($stack->toArray());
    echo $product;
    // Output: 24

    min() (Custom Implementation)
    Description: Finds the minimum value in the stack.
    Example:

    $stack = new \Ds\Stack([3, 1, 4, 2]);
    echo min($stack->toArray());
    // Output: 1

    max() (Custom Implementation)
    Description: Finds the maximum value in the stack.
    Example:

    $stack = new \Ds\Stack([3, 1, 4, 2]);
    echo max($stack->toArray());
    // Output: 4

    swapTop() (Custom Implementation)
    Description: Swaps the top two elements of the stack.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $array = $stack->toArray();
    list($array[0], $array[1]) = [$array[1], $array[0]];
    $stack = new \Ds\Stack($array);
    print_r($stack);
    // Output: Ds\Stack Object ( [0] => 2 [1] => 1 [2] => 3 )

    concat()
    Description: Concatenates the current stack with another iterable and returns a new stack.
    Example:

    $stack = new \Ds\Stack([1, 2]);
    $concatenatedStack = new \Ds\Stack([...$stack->toArray(), ...[3, 4]]);
    print_r($concatenatedStack);
    // Output: Ds\Stack Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

    rotate() (Custom Implementation)
    Description: Rotates the stack by moving the top element to the bottom.
    Example:

    $stack = new \Ds\Stack([1, 2, 3]);
    $array = $stack->toArray();
    array_unshift($array, array_pop($array));
    $rotatedStack = new \Ds\Stack($array);
    print_r($rotatedStack);
    // Output: Ds\Stack Object ( [0] => 3 [1] => 1 [2] => 2 )

    chunk() (Custom Implementation)
    Description: Splits the stack into chunks of a specified size.
    Example:

    $stack = new \Ds\Stack([1, 2, 3, 4, 5]);
    $chunks = array_chunk($stack->toArray(), 2);
    foreach ($chunks as $chunk) {
        print_r(new \Ds\Stack($chunk));
    }
    // Output:
    // Ds\Stack Object ( [0] => 1 [1] => 2 )
    // Ds\Stack Object ( [0] => 3 [1] => 4 )
    // Ds\Stack Object ( [0] => 5 )

    flatten() (Custom Implementation)
    Description: Flattens a multi-dimensional stack into a single-level stack.
    Example:

    $stack = new \Ds\Stack([[1, 2], [3, 4], 5]);
    $flattened = new \Ds\Stack(array_merge(...$stack->toArray()));
    print_r($flattened);
    // Output: Ds\Stack Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
  • Ds\Map Function

    Basic Ds\Map Function

    allocate()
    Description: Allocates enough memory for the required capacity.
    Example:

    $set = new \Ds\Set();
    $set->add(1, 2, 3);
    print_r($set);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

    apply()
    Description: Applies a specific operation to all of the elements present in the map.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $map->apply(function($key, $value) {
        return strtoupper($value);
    });
    // Output: Map with values ["ONE", "TWO"]

    capacity()
    Description: Returns the current capacity of the set.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    echo $map->capacity();
    // Output: Capacity of the map (e.g., 2)

    clear()
    Description: Removes all values from the set.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $map->clear();
    // Output: The map is now empty.

    __construct()
    Description: 
    Creates a new instance of a Map.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    // Output: A map with key-value pairs [1 => "One", 2 => "Two"]

    copy()
    Description: Gets a shallow copy of the specified Map instance.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $copy = $map->copy();
    // Output: A shallow copy of the map [1 => "One", 2 => "Two"]

    copy()
    Description: Returns a shallow copy of the set.
    Example:

    $result = gmp_div_qr("10", "3");
    // Output: ([quotient => 3, remainder => 1])

    count()
    Description: Counts the number of values in the set.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    echo $map->count();
    // Output: 2

    diff()
    Description: Creates a new set with elements in the current set but not in the given set.
    Example:

    $map1 = new \Ds\Map([1 => "One", 2 => "Two"]);
    $map2 = new \Ds\Map([2 => "Two", 3 => "Three"]);
    $diffMap = $map1->diff($map2);
    // Output: Map with [1 => "One"]

    filter()
    Description: Creates a new set with values that pass a callback function.
    Example:

    $set = new \Ds\Set([1, 2, 3, 4]);
    $result = $set->filter(fn($value) => $value % 2 === 0);
    print_r($result);
    // Output: Ds\Set Object ([0] => 2, [1] => 4)

    first()
    Description: Gets the first key-value pair from the Map instance.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $first = $map->first();
    // Output: Pair with [1 => "One"]

    get()
    Description: Returns the value of the given key.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    echo $map->get(1);
    // Output: "One"

    hasKey()
    Description: Checks whether a given Key is present in the Map object.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    echo $map->hasKey(1);
    // Output: 1 (true)

    intersect()
    Description: Creates a new set with elements common to both sets.
    Example:

    $map1 = new \Ds\Map([1 => "One", 2 => "Two"]);
    $map2 = new \Ds\Map([2 => "Two", 3 => "Three"]);
    $intersection = $map1->intersect($map2);
    // Output: Map with [2 => "Two"]

    isEmpty()
    Description: Checks whether a given Map is empty or not.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    echo $map->isEmpty();
    // Output:  (false)

    keys()
    Description: Returns the set of keys of the current Map instance.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $keys = $map->keys();
    // Output: [1, 2]

    ksort()
    Description: Sorts the map elements in-place by key.
    Example:

    $map = new \Ds\Map([2 => "Two", 1 => "One"]);
    $map->ksort();
    // Output: Map with [1 => "One", 2 => "Two"]

    ksorted()
    Description: Returns a copy which is sorted by key.
    Example:

    $map = new \Ds\Map([2 => "Two", 1 => "One"]);
    $sortedMap = $map->ksorted();
    // Output: Map with [1 => "One", 2 => "Two"]

    last()
    Description: Finds and gets the last key-value pair from a Map object.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $last = $map->last();
    // Output: Pair with [2 => "Two"]

    remove()
    Description: Removes specific values from the set.
    Example:

    $set = new \Ds\Set([1, 2, 3, 4]);
    $set->remove(2, 4);
    print_r($set);
    // Output: Ds\Set Object ([0] => 1, [1] => 3)

    map()
    Description: Applies a callback function to a Map object.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $newMap = $map->map(function($key, $value) {
        return strtoupper($value);
    });
    // Output: Map with [1 => "ONE", 2 => "TWO"]

    merge()
    Description: Returns the result of adding all given associations.
    Example:

    $map1 = new \Ds\Map([1 => "One"]);
    $map2 = new \Ds\Map([2 => "Two"]);
    $mergedMap = $map1->merge($map2);
    // Output: Map with [1 => "One", 2 => "Two"]

    slice()
    Description: Returns a subset of the set based on the specified range.
    Example:

    $result = gmp_nextprime("100");
    // Output: (next prime is 101)

    pairs()
    Description: Gets all of the pairs from the specified Map instance.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $pairs = $map->pairs();
    // Output: Pair objects [1 => "One", 2 => "Two"]

    put()
    Description: Associates a key with a value.
    Example:

    $set = new \Ds\Set([3, 1, 2]);
    $result = $set->sorted();
    print_r($result);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

    putAll()
    Description: Associates all key-value pairs of a traversable object or array.
    Example:

    $map = new \Ds\Map([1 => "One"]);
    $map->putAll([2 => "Two", 3 => "Three"]);
    // Output: Map with [1 => "One", 2 => "Two", 3 => "Three"]

    reduce()
    Description: Reduces the map to a single value by applying operations using the callback function.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $result = $map->reduce(function($carry, $item) {
        return $carry . $item;
    }, "");
    // Output: "OneTwo"

    reverse()
    Description: In-place reverse the elements of a specified Map instance.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $map->reverse();
    // Output: Map with [2 => "Two", 1 => "One"]

    reversed()
    Description: Gets a copy of the reverse of elements of a specified Map instance.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $reversedMap = $map->reversed();
    // Output: Map with [2 => "Two", 1 => "One"]

    skip()
    Description: Returns the pair at a given positional index.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $pair = $map->skip(1);
    // Output: Pair with [2 => "Two"]

    slice()
    Description: Gets a subset of the specified Map instance.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two", 3 => "Three"]);
    $subset = $map->slice(0, 2);
    // Output: Map with [1 => "One", 2 => "Two"]

    sort()
    Description: In-place sort the elements of a specified Map instance according to the values.
    Example:

    $map = new \Ds\Map([2 => "Two", 1 => "One"]);
    $map->sort();
    // Output: Map with [1 => "One", 2 => "Two"]

    sorted()
    Description: Gets a copy of the specified Map instance sorted according to the values.
    Example:

    $map = new \Ds\Map([2 => "Two", 1 => "One"]);
    $sortedMap = $map->sorted();
    // Output: Map with [1 => "One", 2 => "Two"]

    sum()
    Description: Gets the sum of all of the values present in the Map instance.
    Example:

    $map = new \Ds\Map([1 => 1, 2 => 2, 3 => 3]);
    $sum = $map->sum();
    // Output: 6

    toArray()
    Description: Gets an array generated by converting the Map instance into an Array.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $array = $map->toArray();
    // Output: [1 => "One", 2 => "Two"]

    union()
    Description: Creates a new map that contains the union of two maps.
    Example:

    $map1 = new \Ds\Map([1 => "One", 2 => "Two"]);
    $map2 = new \Ds\Map([3 => "Three", 4 => "Four"]);
    $unionMap = $map1->union($map2);
    // Output: Map with [1 => "One", 2 => "Two", 3 => "Three", 4 => "Four"]

    values()
    Description: Returns a sequence of the map’s values.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $values = $map->values();
    // Output: ["One", "Two"]

    xor()
    Description: Creates a new map that contains the value either in the first map or the second map, but not both.
    Example:

    $map1 = new \Ds\Map([1 => "One", 2 => "Two"]);
    $map2 = new \Ds\Map([2 => "Two", 3 => "Three"]);
    $xorMap = $map1->xor($map2);
    // Output: Map with [1 => "One", 3 => "Three"]

    mergeRecursive()
    Description: Recursively merges all key-value pairs from the provided map into the current map.
    Example:

    $map1 = new \Ds\Map([1 => "One", 2 => "Two"]);
    $map2 = new \Ds\Map([2 => "Second", 3 => "Three"]);
    $mergedMap = $map1->mergeRecursive($map2);
    // Output: Map with [1 => "One", 2 => "Second", 3 => "Three"]

    pop()
    Description: Removes and returns the last key-value pair from the map.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $last = $map->pop();
    // Output: Pair with [2 => "Two"], and the map becomes [1 => "One"]

    push()
    Description: Adds a key-value pair to the end of the map.
    Example:

    $map = new \Ds\Map([1 => "One"]);
    $map->push(2, "Two");
    // Output: Map with [1 => "One", 2 => "Two"]

    shift()
    Description: Removes and returns the first key-value pair from the map.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $first = $map->shift();
    // Output: Pair with [1 => "One"], and the map becomes [2 => "Two"]

    sliceKeys()
    Description: Returns a subset of the map containing only the specified keys.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two", 3 => "Three"]);
    $sliced = $map->sliceKeys([1, 3]);
    // Output: Map with [1 => "One", 3 => "Three"]

    mapKeys()
    Description: Transforms the keys of the map using a given callback function.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $newMap = $map->mapKeys(function($key) {
        return $key * 2;
    });
    // Output: Map with [2 => "One", 4 => "Two"]

    swap()
    Description: Swaps the positions of two elements in the map using their keys.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two", 3 => "Three"]);
    $map->swap(1, 3);
    // Output: Map with [3 => "One", 2 => "Two", 1 => "Three"]

    mapKeys()
    Description: Transforms the keys of the map using a given callback function.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $newMap = $map->mapKeys(function($key) {
        return $key * 2;
    });
    // Output: Map with [2 => "One", 4 => "Two"]

    unique()
    Description: Removes duplicate values from the map.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "One", 3 => "Two"]);
    $uniqueMap = $map->unique();
    // Output: Map with [1 => "One", 3 => "Two"]

    find()
    Description: Finds and returns the key of a given value.
    Example:

    $map = new \Ds\Map([1 => "One", 2 => "Two"]);
    $key = $map->find("Two");
    // Output: 2
  • Ds\Set Functions

    Basic Ds\Set Functions

    add()
    Description: Adds one or more values to the set.
    Example:

    $set = new \Ds\Set();
    $set->add(1, 2, 3);
    print_r($set);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

    allocate()
    Description: Allocates memory for a given capacity.
    Example:

    $set = new \Ds\Set();
    $set->allocate(10);
    // Output: (allocates memory for 10 elements)

    capacity()
    Description: Returns the current capacity of the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->capacity();
    // Output: (capacity is 3)

    clear()
    Description: Removes all values from the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    $set->clear();
    print_r($set);
    // Output: Ds\Set Object ()

    __construct()
    Description: Creates a new instance of a set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    print_r($set);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

    contains()
    Description: Checks if the given value exists in the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    var_dump($set->contains(2));
    // Output: (bool true)

    copy()
    Description: Returns a shallow copy of the set.
    Example:

    $result = gmp_div_qr("10", "3");
    // Output: ([quotient => 3, remainder => 1])

    count()
    Description: Counts the number of values in the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->count();
    // Output: (3)

    diff()
    Description: Creates a new set with elements in the current set but not in the given set.
    Example:

    $set1 = new \Ds\Set([1, 2, 3]);
    $set2 = new \Ds\Set([2, 3, 4]);
    $result = $set1->diff($set2);
    print_r($result);
    // Output: Ds\Set Object ([0] => 1)

    filter()
    Description: Creates a new set with values that pass a callback function.
    Example:

    $set = new \Ds\Set([1, 2, 3, 4]);
    $result = $set->filter(fn($value) => $value % 2 === 0);
    print_r($result);
    // Output: Ds\Set Object ([0] => 2, [1] => 4)

    first()
    Description: Returns the first value in the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->first();
    // Output: (1)

    get()
    Description: Retrieves a value by its index.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->get(1);
    // Output: (2)

    gmp_gcdext()
    Description: Calculates the GCD and multipliers for the equation.
    Example:

    $result = gmp_gcdext("48", "18");
    // Output: ([gcd => 6, s => -1, t => 3])

    intersect()
    Description: Creates a new set with elements common to both sets.
    Example:

    $set1 = new \Ds\Set([1, 2, 3]);
    $set2 = new \Ds\Set([2, 3, 4]);
    $result = $set1->intersect($set2);
    print_r($result);
    // Output: Ds\Set Object ([0] => 2, [1] => 3)

    isEmpty()
    Description: Checks whether the set is empty.
    Example:

    $set = new \Ds\Set();
    var_dump($set->isEmpty());
    // Output: (bool true)

    join()
    Description: Joins all elements of the set into a string.
    Example:

    $set = new \Ds\Set(["a", "b", "c"]);
    echo $set->join(", ");
    // Output: (a, b, c)

    last()
    Description: Returns the last value in the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->last();
    // Output: (3)

    merge()
    Description: Merges another set or array into the current set.
    Example:

    $set = new \Ds\Set([1, 2]);
    $result = $set->merge([3, 4]);
    print_r($result);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3, [3] => 4)

    reduce()
    Description: Reduces the set to a single value using a callback.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    $result = $set->reduce(fn($carry, $value) => $carry + $value, 0);
    echo $result;
    // Output: (6)

    remove()
    Description: Removes specific values from the set.
    Example:

    $set = new \Ds\Set([1, 2, 3, 4]);
    $set->remove(2, 4);
    print_r($set);
    // Output: Ds\Set Object ([0] => 1, [1] => 3)

    reverse()
    Description: Reverses the order of elements in the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    $set->reverse();
    print_r($set);
    // Output: Ds\Set Object ([0] => 3, [1] => 2, [2] => 1)

    reversed()
    Description: Creates a new set with values in reverse order.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    $result = $set->reversed();
    print_r($result);
    // Output: Ds\Set Object ([0] => 3, [1] => 2, [2] => 1)

    slice()
    Description: Returns a subset of the set based on the specified range.
    Example:

    $result = gmp_nextprime("100");
    // Output: (next prime is 101)

    sort()
    Description: Sorts the elements of the set in-place.
    Example:

    $set = new \Ds\Set([3, 1, 2]);
    $set->sort();
    print_r($set);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

    sorted()
    Description: Returns a new set with elements sorted in ascending order.
    Example:

    $set = new \Ds\Set([3, 1, 2]);
    $result = $set->sorted();
    print_r($result);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

    sum()
    Description: Returns the sum of all elements in the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->sum();
    // Output: (6)

    toArray()
    Description: Converts the set into an array.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    print_r($set->toArray());
    // Output: Array ([0] => 1, [1] => 2, [2] => 3)

    union()
    Description: Creates a new set that contains the union of two sets.
    Example:

    $set1 = new \Ds\Set([1, 2, 3]);
    $set2 = new \Ds\Set([3, 4, 5]);
    $result = $set1->union($set2);
    print_r($result);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3, [3] => 4, [4] => 5)

    xor()
    Description: Creates a new set containing values that exist in either set but not both.
    Example:

    $set1 = new \Ds\Set([1, 2, 3]);
    $set2 = new \Ds\Set([3, 4, 5]);
    $result = $set1->xor($set2);
    print_r($result);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 4, [3] => 5)

    add()
    Description: Adds values to the set.
    Example:

    $set = new \Ds\Set([1, 2]);
    $set->add(3);
    print_r($set);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

    allocate()
    Description: Allocates memory for the required capacity of the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    $set->allocate(10);
    echo "Capacity: " . $set->capacity();
    // Output: Capacity: 10

    capacity()
    Description: Returns the capacity of the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->capacity();
    // Output: 8 (The default capacity may vary based on implementation)

    clear()
    Description: Removes all values from the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    $set->clear();
    print_r($set);
    // Output: Ds\Set Object () (empty set)

    __construct()
    Description: Creates a new instance of a set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    print_r($set);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

    contains()
    Description: Checks if a given value exists in the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->contains(2);
    // Output: 1 (true)

    copy()
    Description: Returns a copy of the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    $copy = $set->copy();
    print_r($copy);
    // Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

    count()
    Description: Counts the number of values present in the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->count();
    // Output: 3

    diff()
    Description: Creates a set containing the elements of the first set that are not present in the second set.
    Example:

    $set1 = new \Ds\Set([1, 2, 3]);
    $set2 = new \Ds\Set([3, 4, 5]);
    $result = $set1->diff($set2);
    print_r($result);
    // Output: Ds\Set Object ([0] => 1, [1] => 2)

    filter()
    Description: Creates a new set using a filter function.
    Example:

    $set = new \Ds\Set([1, 2, 3, 4]);
    $result = $set->filter(function($value) {
        return $value > 2;
    });
    print_r($result);
    // Output: Ds\Set Object ([0] => 3, [1] => 4)

    first()
    Description: Returns the first element of the set.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->first();
    // Output: 1

    get()
    Description: Gets a value from the set instance.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->get(1);
    // Output: 2 (Gets the value at index 1)

    intersect()
    Description: Creates a new set that contains the intersection of two sets.
    Example:

    $set1 = new \Ds\Set([1, 2, 3]);
    $set2 = new \Ds\Set([2, 3, 4]);
    $result = $set1->intersect($set2);
    print_r($result);
    // Output: Ds\Set Object ([0] => 2, [1] => 3)

    isEmpty()
    Description: Checks whether the set is empty or not.
    Example:

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->isEmpty() ? 'Yes' : 'No';
    // Output: No

    join()
    Description: Joins all values in the set as a string.

    $set = new \Ds\Set([1, 2, 3]);
    echo $set->join(', ');
    // Output: 1, 2, 3
  • GMP Functions

    Basic GMP Functions

    gmp_abs()
    Description: Calculates the absolute value of a GMP number.
    Example:

    $result = gmp_abs("-42");
    // Output: (absolute value is 42)

    gmp_add()
    Description: Adds two GMP numbers.
    Example:

    $result = gmp_add("123", "456");
    // Output: (sum is 579)

    gmp_clrbit()
    Description: Sets the bit at a specified index in a GMP number to 0.
    Example:

    $num = gmp_init("15");
    gmp_clrbit($num, 1);
    // Output: (number becomes 13 after clearing bit 1)

    gmp_cmp()
    Description: Compares two GMP numbers.
    Example:

    $result = gmp_cmp("100", "200");
    // Output: (-1 because 100 < 200)

    gmp_com()
    Description: Calculates the one’s complement of a GMP number.
    Example:

    $result = gmp_com("5");
    // Output: (-6, the one's complement of 5)

    gmp_div_q()
    Description: Performs division of GMP numbers, returning the quotient.
    Example:

    $result = gmp_div_q("10", "3");
    // Output: (quotient is 3)

    gmp_div_qr()
    Description: Performs division and returns both quotient and remainder.
    Example:

    $result = gmp_div_qr("10", "3");
    // Output: ([quotient => 3, remainder => 1])

    gmp_div_r()
    Description: Performs division and returns only the remainder.
    Example:

    $result = gmp_div_r("10", "3");
    // Output: (remainder is 1)

    gmp_divexact()
    Description: Divides exactly when one GMP number is divisible by another.
    Example:

    $result = gmp_divexact("10", "2");
    // Output: (exact quotient is 5)

    gmp_export()
    Description: Exports a GMP number to a binary string.
    Example:

    $result = gmp_export("255");
    // Output: ("\xFF" - binary string representation of 255)

    gmp_fact()
    Description: Calculates the factorial of a GMP number.
    Example:

    $result = gmp_fact("5");
    // Output: (factorial is 120)

    gmp_gcd()
    Description: Calculates the GCD of two GMP numbers.
    Example:

    $result = gmp_gcd("48", "18");
    // Output: (GCD is 6)

    gmp_gcdext()
    Description: Calculates the GCD and multipliers for the equation.
    Example:

    $result = gmp_gcdext("48", "18");
    // Output: ([gcd => 6, s => -1, t => 3])

    gmp_hamdist()
    Description: Finds the Hamming distance between two GMP numbers.
    Example:

    $result = gmp_hamdist("5", "7");
    // Output: (Hamming distance is 1)

    gmp_import()
    Description: Imports a GMP number from a binary string.
    Example:

    $result = gmp_import("\xFF");
    // Output: (GMP number is 255)

    gmp_intval()
    Description: Converts a GMP number to an integer.
    Example:

    $result = gmp_intval("123456789");
    // Output: (integer is 123456789)

    gmp_invert()
    Description: Finds the modular inverse of a GMP number.
    Example:

    $result = gmp_invert("3", "7");
    // Output: (modular inverse is 5)

    gmp_jacobi()
    Description: Computes the Jacobi symbol of two GMP numbers.
    Example:

    $result = gmp_jacobi("3", "7");
    // Output: (Jacobi symbol is 1)

    gmp_legendre()
    Description: Computes the Legendre symbol of two GMP numbers.
    Example:

    $result = gmp_legendre("3", "7");
    // Output: (Legendre symbol is 1)

    gmp_mod()
    Description: Finds the modulo of a GMP number.
    Example:

    $result = gmp_mod("10", "3");
    // Output: (modulo is 1)

    gmp_mul()
    Description: Multiplies two GMP numbers.
    Example:

    $result = gmp_mul("123", "456");
    // Output: (product is 56088)

    gmp_neg()
    Description: Returns the negative of a GMP number.
    Example:

    $result = gmp_neg("42");
    // Output: (-42)

    gmp_nextprime()
    Description: Finds the smallest prime number greater than the given GMP number.
    Example:

    $result = gmp_nextprime("100");
    // Output: (next prime is 101)

    Gmagick::medianFilterImage()
    Description: Reduces noise in the image using a median filter.
    Example:

    $image->medianFilterImage(2);
    // Output: (reduces image noise with a radius of 2)

    gmp_perfect_square()
    Description: Checks if the given GMP number is a perfect square.
    Example:

    $result = gmp_perfect_square("49");
    // Output: (true, because 49 is a perfect square)

    gmp_popcount()
    Description: Finds the population count (number of set bits) in a GMP number.
    Example:

    $result = gmp_popcount("15");
    // Output: (population count is 4)

    gmp_pow()
    Description: Raises a GMP number to the power of an integer.
    Example:

    $result = gmp_pow("2", "10");
    // Output: (1024)

    gmp_prob_prime()
    Description: Checks the probability of a GMP number being prime.
    Example:

    $result = gmp_prob_prime("101");
    // Output: (value 2, meaning 101 is definitely prime)

    gmp_random_bits()
    Description: Generates a random number with a specified number of bits.
    Example:

    $result = gmp_random_bits(10);
    // Output: (random number with up to 10 bits, e.g., 745)

    gmp_random_range()
    Description: Generates a random number within a specified range.
    Example:

    $result = gmp_random_range("1", "100");
    // Output: (random number between 1 and 100)

    gmp_random_seed()
    Description: Sets the seed for the random number generator.
    Example:

    gmp_random_seed("123");
    // Output: (sets the RNG seed to 123)

    gmp_root()
    Description: Calculates the integer part of the N-th root of a GMP number.
    Example:

    $result = gmp_root("27", "3");
    // Output: (cube root is 3)

    gmp_rootrem()
    Description: Calculates the N-th root and the remainder.
    Example:

    list($root, $remainder) = gmp_rootrem("28", "3");
    // Output: (root is 3, remainder is 1)

    gmp_scan0()
    Description:
    Scans for the first occurrence of the bit “0” in a GMP number.
    Example:

    $result = gmp_scan0("15", 1);
    // Output: (first 0-bit is at index 4)

    gmp_scan1()
    Description:
    Scans for the first occurrence of the bit “1” in a GMP number.
    Example:

    $result = gmp_scan1("8", 0);
    // Output: (first 1-bit is at index 3)

    gmp_setbit()
    Description: Sets a specific bit in a GMP number.
    Example:

    $num = gmp_init("0");
    gmp_setbit($num, 3);
    // Output: (number becomes 8)

    gmp_sign()
    Description: Checks the sign of a GMP number.
    Example:

    $result = gmp_sign("-42");
    // Output: (-1 because the number is negative)

    gmp_sqrt()
    Description: Calculates the square root of a GMP number.
    Example:

    $result = gmp_sqrt("49");
    // Output: (square root is 7)

    gmp_sqrtrem()
    Description: Calculates the square root and remainder of a GMP number.
    Example:

    list($sqrt, $remainder) = gmp_sqrtrem("50");
    // Output: (square root is 7, remainder is 1)

    gmp_strval()
    Description: Returns the string representation of a GMP number.
    Example:

    $result = gmp_strval("12345");
    // Output: ("12345")

    gmp_sub()
    Description: Subtracts one GMP number from another.
    Example:

    $result = gmp_sub("100", "42");
    // Output: (difference is 58)

    gmp_testbit()
    Description: Checks if a specific bit in a GMP number is set.
    Example:

    $result = gmp_testbit("8", 3);
    // Output: (true because bit 3 is set)

    gmp_xor()
    Description: Calculates the XOR of two GMP numbers.
    Example:

    $result = gmp_xor("10", "5");
    // Output: (result is 15)

    gmp_random()
    Description: Generates a random GMP number.
    Example:

    $result = gmp_random(5);
    // Output: (random number with 5 limbs)
  • Gmagick Functions

    Basic Gmagick Functions

    The Gmagick class provides a wrapper to the GraphicsMagick library, which is used to create, edit, and manipulate images.

    Gmagick::addImage()
    Description: Adds a new image to the Gmagick object image list.
    Example:

    $image->addImage($newImage);
    // Output: (adds a new image to the current Gmagick image list)

    Gmagick::blurImage()
    Description: Applies a blur filter to the image.
    Example:

    $image->blurImage(5, 3);
    // Output: (applies a blur with radius 5 and sigma 3 to the image)

    Gmagick::cropImage()
    Description: Extracts a specific region from the image.
    Example:

    $image->cropImage(100, 100, 50, 50);
    // Output: (crops a 100x100 region starting at coordinates (50, 50))

    Gmagick::flipImage()
    Description: Creates a mirror image by reflecting the pixels along the x-axis.
    Example:

    $image->flipImage();
    // Output: (creates a flipped version of the image along the x-axis)

    Gmagick::rotateImage()
    Description: Rotates the image by the specified number of degrees.
    Example:

    $image->rotateImage('#FFFFFF', 90);
    // Output: (rotates the image by 90° with a white background)

    Gmagick::resizeImage()
    Description: Scales the image to specified dimensions using a filter.
    Example:

    $image->resizeImage(200, 150, Gmagick::FILTER_LANCZOS, 1);
    // Output: (resizes the image to 200x150 pixels using Lanczos filter)

    Gmagick::oilPaintImage()
    Description: Applies a special effect filter that simulates an oil painting.
    Example:

    $image->oilPaintImage(3);
    // Output: (applies an oil-paint effect to the image with radius 3)

    Gmagick::addNoiseImage()
    Description: Adds noise to the image.
    Example:

    $image->addNoiseImage(Gmagick::NOISE_POISSON);
    // Output: (adds Poisson noise to the image)

    Gmagick::annotateImage()
    Description: Annotates an image with text.
    Example:

    $image->annotateImage($draw, 10, 50, 0, 'Sample Text');
    // Output: (adds the text "Sample Text" to the image at coordinates (10, 50))

    Gmagick::borderImage()
    Description: Draws a border around the image.
    Example:

    $image->borderImage('#000000', 10, 10);
    // Output: (adds a black border of 10px width to the image)

    Gmagick::charcoalImage()
    Description: Simulates a charcoal sketch effect.
    Example:

    $image->charcoalImage(2, 1);
    // Output: (applies a charcoal effect to the image with radius 2 and sigma 1)

    Gmagick::chopImage()
    Description: Removes a region from the image and trims it.
    Example:

    $image->chopImage(100, 100, 50, 50);
    // Output: (removes a 100x100 region starting at coordinates (50, 50))

    Gmagick::clear()
    Description: Clears all resources associated with the Gmagick object.
    Example:

    $image->clear();
    // Output: (releases memory and clears all resources)

    Gmagick::commentImage()
    Description: Adds a comment to the image.
    Example:

    $image->commentImage('This is a sample comment.');
    // Output: (attaches the comment "This is a sample comment" to the image)

    Gmagick::cropThumbnailImage()
    Description: Creates a fixed-size thumbnail by scaling and cropping the image.
    Example:

    $image->cropThumbnailImage(150, 150);
    // Output: (creates a 150x150 thumbnail of the image)

    Gmagick::drawImage()
    Description: Renders a GmagickDraw object onto the image.
    Example:

    $image->drawImage($draw);
    // Output: (draws the specified shapes or text on the image)

    Gmagick::edgeImage()
    Description: Enhances the edges of the image using a convolution filter.
    Example:

    $image->edgeImage(1);
    // Output: (enhances the edges of the image with radius 1)

    Gmagick::embossImage()
    Description: Applies a three-dimensional grayscale emboss effect.
    Example:

    $image->embossImage(2, 1);
    // Output: (applies an emboss effect with radius 2 and sigma 1)

    Gmagick::enhanceImage()
    Description: Applies a digital filter to improve image quality.
    Example:

    $image->enhanceImage();
    // Output: (improves the quality of the image)

    Gmagick::equalizeImage()
    Description: Equalizes the histogram of the image.
    Example:

    $image->equalizeImage();
    // Output: (adjusts image contrast by equalizing its histogram)

    Gmagick::flopImage()
    Description: Creates a mirror image along the y-axis.
    Example:

    $image->flopImage();
    // Output: (creates a mirrored version of the image along the y-axis)

    Gmagick::gammaImage()
    Description: Applies gamma correction to the image.
    Example:

    $image->gammaImage(1.5);
    // Output: (applies a gamma correction with a factor of 1.5)

    Gmagick::implodeImage()
    Description: Creates an implosion effect on the image.
    Example:

    $image->implodeImage(0.5);
    // Output: (creates an imploded version of the image with a factor of 0.5)

    Gmagick::medianFilterImage()
    Description: Reduces noise in the image using a median filter.
    Example:

    $image->medianFilterImage(2);
    // Output: (reduces image noise with a radius of 2)

    Gmagick::motionBlurImage()
    Description: Simulates motion blur in the image.
    Example:

    $image->motionBlurImage(5, 3, 45);
    // Output: (applies a motion blur with radius 5, sigma 3, and angle 45°)

    Gmagick::normalizeImage()
    Description: Enhances the contrast of the image by adjusting pixel colors to span the entire color range.
    Example:

    $image->normalizeImage();
    // Output: (enhances the image contrast by normalizing its pixel colors)

    Gmagick::raiseImage()
    Description: Adds a simulated three-dimensional effect by raising the edges of the image.
    Example:

    $image->raiseImage(10, 10, 5, 5, true);
    // Output: (creates a raised edge effect with a 10x10 rectangle inset by 5 pixels)

    Gmagick::reduceNoiseImage()
    Description: Smooths contours of the image while preserving edges.
    Example:

    $image->reduceNoiseImage(3);
    // Output: (reduces noise in the image with a threshold of 3)

    Gmagick::resampleImage()
    Description: Resamples the image to the desired resolution.
    Example:

    $image->resampleImage(300, 300, Gmagick::FILTER_LANCZOS, 1);
    // Output: (resamples the image to 300 DPI with Lanczos filter)

    Gmagick::rollImage()
    Description: Rolls the image horizontally and vertically.
    Example:

    $image->rollImage(50, 25);
    // Output: (shifts the image 50 pixels horizontally and 25 pixels vertically)

    Gmagick::scaleImage()
    Description: Scales the image to the specified dimensions.
    Example:

    $image->scaleImage(300, 200);
    // Output: (resizes the image to 300x200 pixels)

    Gmagick::setImageDepth()
    Description: Sets the bit depth for the image.
    Example:

    $image->setImageDepth(16);
    // Output: (sets the image depth to 16 bits)

    Gmagick::setImageResolution()
    Description: Sets the resolution of the image.
    Example:

    $image->setImageResolution(300, 300);
    // Output: (sets the image resolution to 300 DPI)

    Gmagick::shearImage()
    Description: Slides the edges of an image to create a parallelogram effect.
    Example:

    $image->shearImage('#FFFFFF', 30, 0);
    // Output: (applies a shear transformation with a 30° x-axis slope and a white background)

    Gmagick::solarizeImage()
    Description: Applies a solarize effect to the image.
    Example:

    $image->solarizeImage(128);
    // Output: (creates a solarized version of the image with a threshold of 128)

    Gmagick::spreadImage()
    Description: Randomly displaces pixels in the image.
    Example:

    $image->spreadImage(5);
    // Output: (displaces pixels randomly within a 5-pixel block)

    Gmagick::stripImage()
    Description: Removes all profiles and comments from the image.
    Example:

    $image->stripImage();
    // Output: (removes metadata such as profiles and comments from the image)

    Gmagick::swirlImage()
    Description: Swirls the pixels around the center of the image.
    Example:

    $image->swirlImage(90);
    // Output: (creates a swirl effect with a 90° rotation)

    Gmagick::magnifyImage()
    Description: Scales the image to twice its original size.
    Example:

    $image->magnifyImage();
    // Output: (enlarges the image to 2x its original size)

    Gmagick::minifyImage()
    Description: Scales the image to half its original size.
    Example:

    $image->minifyImage();
    // Output: (reduces the image size to 50% of its original dimensions)

    Gmagick::modulateImage()
    Description: Adjusts brightness, saturation, and hue of the image.
    Example:

    $image->modulateImage(100, 50, 120);
    // Output: (adjusts brightness to 100%, saturation to 50%, and hue to 120%)

    Gmagick::motionBlurImage()
    Description: Simulates motion blur in the image.
    Example:

    $image->motionBlurImage(10, 5, 45);
    // Output: (adds a motion blur effect with radius 10, sigma 5, and angle 45°)

    Gmagick::implodeImage()
    Description:
    Creates an implosion effect on the image.
    Example:

    $image->implodeImage(-1);
    // Output: (creates an exploded effect on the image)

    Gmagick::oilPaintImage()
    Description: Applies an oil painting effect to the image.
    Example:

    $image->oilPaintImage(3);
    // Output: (applies an oil painting effect with radius 3)

    Gmagick::rotateImage()
    Description: Rotates the image by the specified degrees.
    Example:

    $image->rotateImage('#FFFFFF', 45);
    // Output: (rotates the image by 45° with a white background fill)

    Gmagick::resizeImage()
    Description: Resizes the image to the given dimensions using a specific filter.
    Example:

    $image->resizeImage(200, 100, Gmagick::FILTER_LANCZOS, 1);
    // Output: (resizes the image to 200x100 pixels with the Lanczos filter)

    Gmagick::setImageChannelDepth()
    Description: Sets the depth of a specific image channel.
    Example:

    $image->setImageChannelDepth(Gmagick::CHANNEL_RED, 8);
    // Output: (sets the red channel depth to 8 bits)

    Gmagick::implodeImage()
    Description: Creates an implosion effect on the image.
    Example:

    $image->implodeImage(0.5);
    // Output: (applies an implosion effect with a factor of 0.5)

    Gmagick::spreadImage()
    Description: Randomly displaces pixels within a defined block size.
    Example:

    $image->spreadImage(5);
    // Output: (applies a random pixel displacement with a block size of 5)