Contents

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 )