Contents
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 )