Une simple Pile (Stack)

Administrateur PHPfrance
Administrateur PHPfrance | 3088 Messages

11 déc. 2006, 04:00

Voici une classe que j'utilise assez souvent, à chaque fois que j'ai besoin d'une pile en fait.
class stack
{
	private $stack = array();

	/**
	* Pop the top element off the stack
	*
	* @return	mixed			The top element
	*/
	public function pop()
	{
		return array_pop($this->stack);
	}

	/**
	* Shift an element off the bottom of the stack
	*
	* @return	mixed			The bottom element
	*/
	public function shift()
	{
		return array_shift($this->stack);
	}

	/**
	* Add an element at the top of the stack
	*
	* @param	mixed	$elem	The element to add
	* @return	void
	*/
	public function push($elem)
	{
		array_push($this->stack, $elem);
	}

	/**
	* Add an element at the bottom of the stack
	*
	* @param	mixed	$elem	The elementt to add
	* @return	void
	*/
	public function unshift($elem)
	{
		array_unshift($this->stack, $elem);
	}

	/**
	* Return the top element without removing from the stack
	*
	* @return	mixed			The top element, or FALSE if the array is empty
	*/
	public function peek()
	{
		return (!empty($this->stack)) ? end($this->stack) : FALSE;
	}

	/**
	* Return the contents of the stack
	*
	* @return	array			The stack, ordered from the bottom up
	*/
	public function get()
	{
		return $this->stack;
	}

	/**
	* Test whether given element is in the stack
	*
	* @param	mixed	$elem	The element
	* @return	bool
	*/
	public function search($elem)
	{
		return in_array($elem, $this->stack, TRUE);
	}

	/**
	* Return the number of elements in the stack
	*
	* @return	integer
	*/
	public function count()
	{
		return count($this->stack);
	}

	/**
	* Return the position of the element within the stack
	*
	* Returns the 0-based position of the element (starting from the top) if it is within the stack,
	* or FALSE otherwise
	*
	* @param	mixed	$elem	The element
	* @return	mixed			Position of the element, or FALSE
	*/
	public function pos($elem)
	{
		$pos = array_search($elem, $this->stack, TRUE);

		if (!$pos && is_bool($pos))
		{
			return FALSE;
		}

		return count($this->stack) - $pos - 1;
	}
}
En général on n'utilise que push(), pop() et peek() mais j'ai ajouter les autres fonctions par soucis de perfection. Exemples rapides :
$stack = new stack;

$stack->push('un');
$stack->push('deux');

// affiche 'deux'
echo $stack->peek(), "\n";

// affiche '0'
echo $stack->pos('deux'), "\n";

// affiche '1'
echo $stack->pos('un'), "\n";

// affiche 'deux' aussi
echo $stack->pop(), "\n";

// affiche 'un'
echo $stack->peek(), "\n";

// affiche '1'
echo $stack->count(), "\n";

Eléphant du PHP | 281 Messages

11 déc. 2006, 21:32

---
Modifié en dernier par Ouaibou le 07 juil. 2007, 11:12, modifié 1 fois.

Administrateur PHPfrance
Administrateur PHPfrance | 3088 Messages

12 déc. 2006, 13:48

Une file... c'est pas faux, en plus ça m'est déjà arrivé de l'utiliser comme ça sans pour autant faire l'association avec le concept de file (queue).

Mammouth du PHP | 1511 Messages

12 déc. 2006, 14:08

Juste comme ca, qu'est ce qu'une pile en php? :?

Administrateur PHPfrance
Administrateur PHPfrance | 3088 Messages

12 déc. 2006, 14:16

Via Wikipédia : Pile et File.
Modifié en dernier par Hubert Roksor le 02 mai 2007, 01:15, modifié 1 fois.

Mammouth du PHP | 1511 Messages

12 déc. 2006, 14:43

Faudra que je m'y mette a l'occasion, bien que pour le moment, ce concept ne me soit pas utile...
@+

Mammouth du PHP | 1511 Messages

02 janv. 2007, 19:43

Bon ben finalement je me sers de cette classe et je me rends compte que c'est fort utile :)
Mais j'ai un petit probz, c'est que j'aimeras savoir comment limiter la taille de la pile... car sinon, elle est beaucoup trop longue, et sachant que ma pile est stockée dans une variable de session, ca le fait pas trop d'avoir des variables de session trop longues...
Merci d'avance ;)

Mammouth du PHP | 1511 Messages

03 janv. 2007, 02:38

Bon, ben vu que j'ai réussi a trouver une solution de limitation de la pile, je me permet de poster le tout ici :)
<?php
class stack
{
	private $stack = array();
	private $stack_limit;
	/**
    * Set optionnal limitation of stack's size
    *
    * @param    integer    $stack_limit    stack's maximal size
    * @return    void
    */
	public function __construct($stack_limit=false)
	{
		if(!is_numeric($stack_limit))
		{
			trigger_error('Stack\'s limit must be integer');
		}
		$this->stack_limit = $stack_limit;
	}

	/**
    * Pop the top element off the stack
    *
    * @return    mixed            The top element
    */
	public function pop()
	{
		return array_pop($this->stack);
	}

	/**
    * Shift an element off the bottom of the stack
    *
    * @return    mixed            The bottom element
    */
	public function shift()
	{
		return array_shift($this->stack);
	}

	/**
    * Add an element at the top of the stack
    *
    * @param    mixed    $elem    The element to add
    * @return    void
    */
	public function push($elem)
	{
		array_push($this->stack, $elem);
		if($this->stack_limit != false)
		{
			if($this->count() > $this->stack_limit)
			{
				array_shift($this->stack);
			}
		}
	}
	/**
    * Add an element at the bottom of the stack
    *
    * @param    mixed    $elem    The elementt to add
    * @return    void
    */
	public function unshift($elem)
	{
		array_unshift($this->stack, $elem);
	}

	/**
    * Return the top element without removing from the stack
    *
    * @return    mixed            The top element, or FALSE if the array is empty
    */
	public function peek()
	{
		return (!empty($this->stack)) ? end($this->stack) : FALSE;
	}

	/**
    * Return the contents of the stack
    *
    * @return    array            The stack, ordered from the bottom up
    */
	public function get()
	{
		return $this->stack;
	}

	/**
    * Test whether given element is in the stack
    *
    * @param    mixed    $elem    The element
    * @return    bool
    */
	public function search($elem)
	{
		return in_array($elem, $this->stack, TRUE);
	}

	/**
    * Return the number of elements in the stack
    *
    * @return    integer
    */
	public function count()
	{
		return count($this->stack);
	}

	/**
    * Return the position of the element within the stack
    *
    * Returns the 0-based position of the element (starting from the top) if it is within the stack,
    * or FALSE otherwise
    *
    * @param    mixed    $elem    The element
    * @return    mixed            Position of the element, or FALSE
    */
	public function pos($elem)
	{
		$pos = array_search($elem, $this->stack, TRUE);

		if (!$pos && is_bool($pos))
		{
			return FALSE;
		}

		return count($this->stack) - $pos - 1;
	}
}
?>

ViPHP
fab
ViPHP | 2657 Messages

03 janv. 2007, 02:57

J'ai bien compris le principe d'utilisation d'une pile mais j'en vois vraiment pas l'usage en php, ça vous sert quand?
Seul l'intelligent a le pouvoir de se trouver con
try { work(); } catch(FlemmeExeption $e) { sleep(84600); }

Mammouth du PHP | 1885 Messages

03 janv. 2007, 03:40

On peut s'en servir pour plein de chose. *























* Un appel Allopass est nécessaire pour avoir la suite de cette réponse.
La programmation est l'expression de la poésie d'un programmeur
Génération PHP