Category: PHP

  • DOM Function

    DOMAttr

    __construct()
    Description: Constructs a new DOMAttr object. This is used to create an attribute node with a specified name and value.
    Example:

    $doc = new DOMDocument();
    $attr = new DOMAttr("id", "unique-value");
    echo $attr->name;  // Output: id
    echo $attr->value; // Output: unique-value

    isId()
    Description: Checks if this attribute is an ID attribute.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement("example");
    $attr = $doc->createAttribute("id");
    $attr->value = "unique-id";
    $element->setAttributeNode($attr);
    echo $attr->isId() ? "true" : "false";
    // Output: true (if the attribute is declared as an ID in a DTD)

    DOMCdataSection

    __construct()
    Description: Constructs a new DOMCdataSection object. This is used to create a CDATA section within the document.
    Example:

    $doc = new DOMDocument();
    $cdata = $doc->createCDATASection("This is a CDATA section.");
    $doc->appendChild($cdata);
    echo $doc->saveXML();
    // Output: <!DOCTYPE example><![CDATA[This is a CDATA section.]]&gt;

    DOMDocument

    __construct()
    Description: Constructs a new DOMAttr object. This is used to create an attribute node with a specified name and value.
    Example:

    $doc = new DOMDocument();
    $attr = new DOMAttr("id", "unique-value");
    echo $attr->name;  // Output: id
    echo $attr->value; // Output: unique-value

    createAttribute()
    Description: Creates a new attribute node with the specified name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement("example");
    $attr = $doc->createAttribute("id");
    $attr->value = "unique-id";
    $element->setAttributeNode($attr);
    echo $attr->isId() ? "true" : "false";
    // Output: true (if the attribute is declared as an ID in a DTD)

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

    $doc = new DOMDocument();
    $attr = $doc->createAttribute("class");
    $attr->value = "my-class";
    echo $attr->name;  // Output: class

    createAttributeNS()
    Description: Creates a new attribute with the specified namespace URI and qualified name.
    Example:

    $doc = new DOMDocument();
    $attr = $doc->createAttributeNS("http://example.com", "ex:attr");
    $attr->value = "value";
    echo $attr->name;  // Output: ex:attr

    createCDATASection()
    Description: Creates a new CDATA section.
    Example:

    $doc = new DOMDocument();
    $cdata = $doc->createCDATASection("CDATA Content");
    $doc->appendChild($cdata);
    echo $doc->saveXML();
    // Output: <!DOCTYPE example><![CDATA[CDATA Content]]&gt;

    createComment()
    Description: Creates a new comment node.
    Example:

    $doc = new DOMDocument();
    $comment = $doc->createComment("This is a comment.");
    $doc->appendChild($comment);
    echo $doc->saveXML();
    // Output: <!--This is a comment.-->

    createDocumentFragment()
    Description: Creates an empty DOMDocumentFragment object.
    Example:

    $doc = new DOMDocument();
    $fragment = $doc->createDocumentFragment();
    $fragment->appendXML("<example>Content</example>");
    $doc->appendChild($fragment);
    echo $doc->saveXML();
    // Output: <example>Content</example>

    createElement()
    Description: Creates a new element with the given tag name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement("example", "This is content.");
    $doc->appendChild($element);
    echo $doc->saveXML();
    // Output: <example>This is content.</example>

    createElementNS()
    Description: Creates a new element with a namespace URI and qualified name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElementNS("http://example.com", "ex:example", "Content");
    $doc->appendChild($element);
    echo $doc->saveXML();
    // Output: <ex:example xmlns:ex="http://example.com">Content</ex:example>

    createEntityReference()
    Description: Creates a new entity reference.
    Example:

    $doc = new DOMDocument();
    $entity = $doc->createEntityReference("copy");
    $doc->appendChild($entity);
    echo $doc->saveXML();
    // Output: &copy;

    createProcessingInstruction()
    Description: Creates a new processing instruction.
    Example:

    $doc = new DOMDocument();
    $pi = $doc->createProcessingInstruction("php", "echo 'Hello';");
    $doc->appendChild($pi);
    echo $doc->saveXML();
    // Output: <?php echo 'Hello';?>

    createTextNode()
    Description: Creates a new text node.
    Example:

    $doc = new DOMDocument();
    $text = $doc->createTextNode("This is text.");
    $doc->appendChild($text);
    echo $doc->saveXML();
    // Output: This is text.

    getElementById()
    Description: Returns an element by its ID.
    Example:

    $doc = new DOMDocument();
    $doc->loadXML('<example id="unique">Content</example>');
    echo $doc->getElementById("unique")->nodeValue;
    // Output: Content

    getElementsByTagName()
    Description: Returns a list of elements by tag name.
    Example:

    $doc = new DOMDocument();
    $doc->loadXML("<root><child>One</child><child>Two</child></root>");
    $elements = $doc->getElementsByTagName("child");
    foreach ($elements as $element) {
        echo $element->nodeValue . "\n";
    }
    // Output: One Two

    getElementsByTagNameNS()
    Description: Returns a list of elements by tag name within a namespace.
    Example:

    $doc = new DOMDocument();
    $doc->loadXML('<root xmlns:ex="http://example.com"><ex:child>Content</ex:child></root>');
    $elements = $doc->getElementsByTagNameNS("http://example.com", "child");
    foreach ($elements as $element) {
        echo $element->nodeValue . "\n";
    }
    // Output: Content

    importNode()
    Description: Imports a node from another document.
    Example: 

    $doc1 = new DOMDocument();
    $doc1->loadXML("<example>Content</example>");
    $doc2 = new DOMDocument();
    $imported = $doc2->importNode($doc1->documentElement, true);
    $doc2->appendChild($imported);
    echo $doc2->saveXML();
    // Output: <example>Content</example>

    load()
    Description: Loads an XML document from a file.
    Example:

    $doc = new DOMDocument();
    $doc->load("file.xml");
    echo $doc->saveXML();

    DOMDocumentFragment

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

    $doc = new DOMDocument();
    $fragment = $doc->createDocumentFragment();
    
    $xml = "<item>First Item</item><item>Second Item</item>";
    $fragment->appendXML($xml);
    
    $root = $doc->createElement("root");
    $doc->appendChild($root);
    $root->appendChild($fragment);
    
    echo $doc->saveXML();
    // Output:
    // <root><item>First Item</item><item>Second Item</item></root>

    __construct()
    Description: Initializes a new DOMElement object.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example", "Hello World");
    echo $element->nodeName;
    // Output: example

    DOMElement

    __construct()

    Description: Initializes a new DOMElement object.
    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 )

    getAttribute()
    Description: Retrieves the value of an attribute by name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement('example');
    $element->setAttribute('id', '123');
    echo $element->getAttribute('id');
    // Output: 123

    getAttributeNode()
    Description: Retrieves the attribute node by name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement('example');
    $element->setAttribute('id', '123');
    $attrNode = $element->getAttributeNode('id');
    echo $attrNode->name . ' = ' . $attrNode->value;
    // Output: id = 123

    getAttributeNodeNS()
    Description: Retrieves an attribute node by its namespace URI and name.
    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 )

    getAttributeNS()
    Description: Retrieves the value of an attribute by namespace URI and name.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement('example');
    $element->setAttributeNS('http://example.com/ns', 'ex:id', '789');
    echo $element->getAttributeNS('http://example.com/ns', 'id');
    // Output: 789

    DOMEntityReference

    __construct()
    Description: Initializes a new DOMEntityReference object, which represents an entity reference in the DOM. Entity references are placeholders for entities that are replaced by their defined content in the XML or HTML document.

    Example:

    $doc = new DOMDocument();
    $entityRef = $doc->createEntityReference('copy');
    $doc->appendChild($entityRef);
    echo $doc->saveXML();
    // Output: &copy;

    DOMImplementation

    __construct()

    Description: Initializes a new DOMImplementation object, which provides methods for operations independent of the document instance.

    Example:

    $domImpl = new DOMImplementation();
    var_dump($domImpl instanceof DOMImplementation);
    // Output: bool(true)

    createDocument()
    Description: Creates a new DOMDocument object with an optional namespace URI, qualified name, and document type.
    Example:

    $domImpl = new DOMImplementation();
    $doc = $domImpl->createDocument("http://www.example.com", "example:root");
    echo $doc->saveXML();
    // Output: <?xml version="1.0"?><example:root xmlns:example="http://www.example.com"/>

    createDocumentType()
    Description: Creates a new DOMDocumentType object. It is used to define the document type declaration.
    Example:

    $domImpl = new DOMImplementation();
    $doctype = $domImpl->createDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
    echo $doctype->name; // Output: html

    hasFeature()
    Description: Checks if the DOMImplementation object supports a specific feature and version.
    Example:

    $doc = new DOMDocument();
    $attr = $doc->createAttribute('id');
    $attr->value = '456';
    $element = $doc->createElement('example');
    $element->setAttributeNode($attr);
    echo $element->getAttribute('id');
    // Output: 456

    setAttributeNodeNS()
    Description: Adds a new attribute node with a namespace to the element.
    Example:

    $domImpl = new DOMImplementation();
    if ($domImpl->hasFeature("XML", "1.0")) {
        echo "Feature XML 1.0 is supported.";
    } else {
        echo "Feature not supported.";
    }
    // Output: Feature XML 1.0 is supported.

    DOMNamedNodeMap

    count()
    Description: Returns the number of items in the DOMNamedNodeMap.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root attr1="value1" attr2="value2" attr3="value3"/>');
    $attributes = $dom->documentElement->attributes;
    echo $attributes->count();
    // Output: 3

    getNamedItem()
    Description: Retrieves a node specified by name from the DOMNamedNodeMap.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root attr1="value1" attr2="value2"/>');
    $attributes = $dom->documentElement->attributes;
    $attr = $attributes->getNamedItem('attr1');
    echo $attr->nodeValue;
    // Output: value1

    item()
    Description: Returns a node at the specified index in the DOMNamedNodeMap.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root attr1="value1" attr2="value2"/>');
    $attributes = $dom->documentElement->attributes;
    $attr = $attributes->item(0);
    echo $attr->nodeName . ": " . $attr->nodeValue;
    // Output: attr1: value1

    DOMNode

    appendChild()
    Description: Appends a new child node to the DOM node. This function adds the node to the list of children of the current node.
    Example:

    $doc = new DOMDocument();
    $element = $doc->createElement('example');
    $element->setAttributeNS('http://example.com/ns', 'ex:id', 'unique');
    $element->setIdAttributeNS('http://example.com/ns', 'id', true);
    echo $doc->getElementById('unique')->nodeName;
    // Output: example

    C14N()
    Description: Canonicalizes the XML of the current node and returns it as a string.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example", "Hello World!");
    $dom->appendChild($element);
    $element->C14NFile("example.xml");
    // The file "example.xml" will contain the canonicalized XML.

    cloneNode()
    Description: Creates a duplicate of the current node. It can clone the node with or without its children.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example", "Clone me!");
    $dom->appendChild($element);
    $clone = $element->cloneNode(true);
    $dom->appendChild($clone);
    echo $dom->saveXML();
    // Output: <example>Clone me!</example><example>Clone me!</example>

    getLineNo()
    Description: Returns the line number of the current node within the document.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root><child>Content</child></root>');
    $child = $dom->getElementsByTagName("child")->item(0);
    echo $child->getLineNo();
    // Output: 2 (This depends on the line where the <child> element is located)

    getNodePath()
    Description: Returns the XPath of the node.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root><parent><child>Value</child></parent></root>');
    $child = $dom->getElementsByTagName("child")->item(0);
    echo $child->getNodePath();
    // Output: /root/parent/child

    hasAttributes()
    Description: Checks if the current node has attributes.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example");
    $element->setAttribute("attr", "value");
    echo $element->hasAttributes() ? "Has attributes" : "No attributes";
    // Output: Has attributes

    hasChildNodes()
    Description: Checks if the current node has child nodes.
    Example:

    $dom = new DOMDocument();
    $parent = $dom->createElement("parent");
    $child = $dom->createElement("child");
    $parent->appendChild($child);
    echo $parent->hasChildNodes() ? "Has child nodes" : "No child nodes";
    // Output: Has child nodes

    insertBefore()
    Description: Inserts a new node before a specified reference node.
    Example:

    $dom = new DOMDocument();
    $parent = $dom->createElement("parent");
    $child1 = $dom->createElement("child1");
    $child2 = $dom->createElement("child2");
    $parent->appendChild($child1);
    $parent->insertBefore($child2, $child1);
    echo $dom->saveXML();
    // Output: <parent><child2/><child1/></parent>

    isDefaultNamespace()
    Description: Checks if the given namespace URI is the default namespace for the current node.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElementNS("http://www.example.com", "example:element");
    echo $element->isDefaultNamespace("http://www.example.com") ? "Default namespace" : "Not default namespace";
    // Output: Default namespace

    isSameNode()
    Description: Checks if two nodes are the same node.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example");
    $clone = $element->cloneNode();
    echo $element->isSameNode($clone) ? "Same node" : "Different nodes";
    // Output: Different nodes

    isSupported()
    Description: Checks if the current node supports the specified feature and version.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElement("example");
    echo $element->isSupported("core", "1.0") ? "Supported" : "Not supported";
    // Output: Supported

    lookupNamespaceUri()
    Description: Looks up the URI associated with a namespace prefix.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElementNS("http://www.example.com", "example:element");
    echo $element->lookupNamespaceUri("example");
    // Output: http://www.example.com

    lookupPrefix()
    Description: Looks up the prefix associated with a given namespace URI.
    Example:

    $dom = new DOMDocument();
    $element = $dom->createElementNS("http://www.example.com", "example:element");
    echo $element->lookupPrefix("http://www.example.com");
    // Output: example

    normalize()
    Description: Normalizes the current node by merging adjacent text nodes and removing empty text nodes.
    Example:

    $dom = new DOMDocument();
    $textNode = $dom->createTextNode("Hello");
    $space = $dom->createTextNode(" ");
    $otherTextNode = $dom->createTextNode("World!");
    $element = $dom->createElement("example");
    $element->appendChild($textNode);
    $element->appendChild($space);
    $element->appendChild($otherTextNode);
    $element->normalize();
    echo $dom->saveXML();
    // Output: <example>Hello World!</example>

    removeChild()
    Description: Removes a child node from the current node.
    Example:

    $dom = new DOMDocument();
    $parent = $dom->createElement("parent");
    $child = $dom->createElement("child");
    $parent->appendChild($child);
    $parent->removeChild($child);
    echo $dom->saveXML();
    // Output: <parent/>

    removeChild()
    Description: Removes a child node from the current node.
    Example:

    $dom = new DOMDocument();
    $parent = $dom->createElement("parent");
    $child = $dom->createElement("child");
    $parent->appendChild($child);
    $parent->removeChild($child);
    echo $dom->saveXML();
    // Output: <parent/>

    replaceChild()
    Description: Replaces a child node with another node.
    Example:

    $dom = new DOMDocument();
    $parent = $dom->createElement("parent");
    $child1 = $dom->createElement("child1");
    $child2 = $dom->createElement("child2");
    $parent->appendChild($child1);
    $parent->replaceChild($child2, $child1);
    echo $dom->saveXML();
    // Output: <parent><child2/></parent>

    DOMProcessingInstruction

    DOMProcessingInstruction __construct()
    Description: Constructs a new processing instruction. This is typically used to add instructions for applications like XML stylesheets.
    Example:

    $dom = new DOMDocument();
    $pi = $dom->createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"style.xsl\"");
    $dom->appendChild($pi);
    echo $dom->saveXML();
    // Output: <?xml-stylesheet type="text/xsl" href="style.xsl"?>

    DOMProcessingInstruction data()
    Description: Retrieves or sets the content of the processing instruction.
    Example:

    $dom = new DOMDocument();
    $pi = $dom->createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"style.xsl\"");
    $dom->appendChild($pi);
    echo $pi->data;
    // Output: type="text/xsl" href="style.xsl"

    DOMProcessingInstruction target()
    Description: Retrieves the target of the processing instruction, such as the application it is intended for.
    Example:

    DOMText

    PHP DOMText __construct() Function
    Description: Constructs a new DOMText object. A DOMText node is used to represent text within an element or attribute in the DOM.
    Example:

    $dom = new DOMDocument();
    $textNode = new DOMText("Hello, World!");
    $dom->appendChild($textNode);
    echo $dom->saveXML();
    // Output: Hello, World!

    PHP DOMText isElementContentWhitespace() Function
    Description: Checks if the text node contains only whitespace and is part of the element’s content.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML("<root>   <child>Content</child>   </root>");
    $textNode = $dom->documentElement->firstChild;
    echo $textNode->isElementContentWhitespace() ? "Yes" : "No";
    // Output: Yes

    PHP DOMText isWhitespaceInElementContent() Function
    Description: An alias for isElementContentWhitespace(). Checks if the text node contains only whitespace and is part of the element’s content.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML("<root>   <child>Content</child>   </root>");
    $textNode = $dom->documentElement->firstChild;
    echo $textNode->isWhitespaceInElementContent() ? "Yes" : "No";
    // Output: Yes

    PHP DOMText splitText() Function
    Description: Splits a text node into two at the specified offset. The original node contains the text before the offset, and a new node is created with the text after the offset.
    Example:

    $dom = new DOMDocument();
    $textNode = $dom->createTextNode("Hello, World!");
    $dom->appendChild($textNode);
    
    $newNode = $textNode->splitText(7);
    echo $textNode->wholeText . "\n"; // Output: Hello,
    echo $newNode->wholeText;        // Output: World!

    DOMXPath

    PHP DOMXPath __construct() Function
    Description: Creates a new DOMXPath object for querying and evaluating XPath expressions on a DOMDocument.

    $dom = new DOMDocument();
    $dom->loadXML("<root><item>Value</item></root>");
    $xpath = new DOMXPath($dom);
    echo "DOMXPath object created.";

    PHP DOMXPath evaluate() Function
    Description: Evaluates an XPath expression and returns the result. The result can be a DOMNodeList, a string, a number, or a boolean, depending on the expression.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML("<root><item>Value</item></root>");
    $xpath = new DOMXPath($dom);
    $result = $xpath->evaluate("string(/root/item)");
    echo $result; // Output: Value

    PHP DOMXPath query() Function
    Description: Executes an XPath query and returns a DOMNodeList containing all nodes matching the expression.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML("<root><item>Value1</item><item>Value2</item></root>");
    $xpath = new DOMXPath($dom);
    $nodes = $xpath->query("//item");
    
    foreach ($nodes as $node) {
        echo $node->nodeValue . "\n";
    }
    // Output:
    // Value1
    // Value2

    PHP DOMXPath registerNamespace() Function
    Description: Registers a namespace with a prefix for use in XPath queries.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root xmlns:ns="http://example.com"><ns:item>Value</ns:item></root>');
    $xpath = new DOMXPath($dom);
    $xpath->registerNamespace("ns", "http://example.com");
    $result = $xpath->evaluate("string(//ns:item)");
    echo $result; // Output: Value

    PHP DOMXPath registerPhpFunctions() Function
    Description: Enables PHP functions to be used within XPath expressions. By default, PHP functions are not available in XPath queries.
    Example:

    $dom = new DOMDocument();
    $dom->loadXML('<root><item>Value</item></root>');
    $xpath = new DOMXPath($dom);
    $xpath->registerPhpFunctions();
    $result = $xpath->evaluate("php:function('strtoupper', /root/item)");
    echo $result; // Output: VALUE

    DOM Functions

    PHP dom_import_simplexml() Function
    Description:The dom_import_simplexml() function converts a SimpleXMLElement object into a DOMElement object, allowing you to work with the node using the DOM extension methods.
    Example:

    // Create a SimpleXMLElement object
    $simpleXml = simplexml_load_string("<root><item>Value</item></root>");
    
    // Import the SimpleXMLElement into a DOMElement
    $domElement = dom_import_simplexml($simpleXml);
    
    if ($domElement) {
        echo $domElement->nodeName; // Output: root
    }
  • Ds\Vector Functions

    Ds\Vector Functions

    allocate()
    Description: Provides the custom size of the vector to allocate space.
    Example:

    $vector = new Vector();
    $vector->allocate(10);
    echo $vector->capacity();
    // Output: 10

    apply()
    Description: Updates all values in the array by applying the callback function to each value of the vector.
    Example:

    $vector = new Vector([1, 2, 3]);
    $vector->apply(fn($value) => $value * 2);
    print_r($vector->toArray());
    // Output: [2, 4, 6]

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

    $vector = new Vector();
    echo $vector->capacity();
    // Output: 0

    clear()
    Description: Clears the vector elements by removing all the elements.
    Example:

    $vector = new Vector([1, 2, 3]);
    $vector->clear();
    echo $vector->isEmpty();
    // Output: 1 (true)

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

    $vector = new Vector([1, 2, 3]);
    print_r($vector->toArray());
    // Output: [1, 2, 3]

    contains()
    Description: Checks whether the vector contains the given value.
    Example:

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

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

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

    copy()
    Description: Creates a copy of the given vector.
    Example:

    $vector = new Vector([1, 2, 3]);
    $copy = $vector->copy();
    print_r($copy->toArray());
    // Output: [1, 2, 3]

    count()
    Description: Counts the number of elements in the vector.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->count();
    // Output: 3

    filter()
    Description: Filters out elements that satisfy the condition defined in the callback function.
    Example:

    $vector = new Vector([1, 2, 3, 4]);
    $result = $vector->filter(fn($value) => $value > 2);
    print_r($result->toArray());
    // Output: [3, 4]

    find()
    Description: Finds the index of the element in the vector.
    Example:

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

    first()
    Description: Returns the first element in the vector.
    Example:

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

    get()
    Description: Returns the element at the given index.
    Example:

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

    insert()
    Description: Inserts the element into the vector at the given index.
    Example:

    $vector = new Vector([1, 3]);
    $vector->insert(1, 2);
    print_r($vector->toArray());
    // Output: [1, 2, 3]

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

    $vector = new Vector();
    echo $vector->isEmpty();
    // Output: 1 (true)

    join()
    Description: Joins elements of the vector into a string using the specified separator.
    Example:

    $vector = new Vector(["a", "b", "c"]);
    echo $vector->join(",");
    // Output: a,b,c

    jsonSerialize()
    Description: Returns elements that can be converted to JSON.
    Example:

    $vector = new Vector(["a", "b", "c"]);
    echo json_encode($vector);
    // Output: ["a","b","c"]

    last()
    Description: Returns the last element of the vector.
    Example:

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

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

    $vector = new Vector([1, 2, 3]);
    $result = $vector->map(fn($value) => $value * 2);
    print_r($result->toArray());
    // Output: [2, 4, 6]

    merge()
    Description: Merges all the elements into the vector.
    Example:

    $vector = new Vector([1, 2]);
    $vector->merge([3, 4]);
    print_r($vector->toArray());
    // Output: [1, 2, 3, 4]

    pop()
    Description: Removes the last element of the vector and returns it.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->pop();
    // Output: 3

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

    $vector = new Vector([1, 2]);
    $vector->push(3);
    print_r($vector->toArray());
    // Output: [1, 2, 3]

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

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

    remove()
    Description: Removes and returns a value by index.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->remove(1);
    // Output: 2

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

    $vector = new Vector([1, 2, 3]);
    $vector->reverse();
    print_r($vector->toArray());
    // Output: [3, 2, 1]

    rotate()
    Description: Rotates the array elements by a given number of rotations. Rotations happen in place.
    Example:

    $vector = new Vector([1, 2, 3, 4]);
    $vector->rotate(2);
    print_r($vector->toArray());
    // Output: [3, 4, 1, 2]

    set()
    Description: Sets the value in the vector at the given index.
    Example:

    $vector = new Vector([1, 2, 3]);
    $vector->set(1, 5);
    print_r($vector->toArray());
    // Output: [1, 5, 3]

    shift()
    Description: Removes the first element from the vector and returns it.
    Example:

    $vector = new Vector([1, 2, 3]);
    echo $vector->shift();
    // Output: 1

    slice()
    Description: Returns the sub-vector of the given vector.
    Example:

    $vector = new Vector([1, 2, 3, 4]);
    $subVector = $vector->slice(1, 3);
    print_r($subVector->toArray());
    // Output: [2, 3]

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

    $vector = new Vector([3, 1, 2]);
    $vector->sort();
    print_r($vector->toArray());
    // Output: [1, 2, 3]
  • 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