PHP data structure Algorithm ternary group triplet_php technique

Source: Internet
Author: User
Copy Code code as follows:

<?php
/**
* Ternary Group Triplet
*
*/
Class Triplet
{
Private $_data = null;
Initializing ternary groups
Public Function init ($val 1, $val 2, $val 3)
{
$this->_data[0] = $val 1;
$this->_data[1] = $val 2;
$this->_data[2] = $val 3;
return true;
}
Destroy Ternary group
Public function Destroy ()
{
unset ($this->_data);
return true;
}
Returns the value of the first $key
Public function Get ($key)
{
if ($key < 1 | | | $key > 3) return false;
Return $this->_data[$key-1];
}
Set the value of $key to $val
Public function put ($key, $val)
{
if ($key < 1 | | | $key > 3) return false;
$this->_data[$key-1] = $val;
return true;
}
Do you want to sort by ascending
Public Function isascending ()
{
Return ($this->_data[0] <= $this->_data[1]) && ($this->_data[1] <= $this->_data[2]);
}
Whether to sort by descending order
Public Function isdescending ()
{
Return ($this->_data[0] >= $this->_data[1]) && ($this->_data[1] >= $this->_data[2]);
}
Get maximum Value
Public Function Max ()
{
Return ($this->_data[0] >= $this->_data[1])? ($this->_data[0] >= $this->_data[2])? $this->_data[0]: $this->_data[2]: ($this->_data[1] >= $this->_data[2])? $this->_data[1]: $this->_data[2];
}
Get Minimum value
Public Function min ()
{
Return ($this->_data[0] <= $this->_data[1])? ($this->_data[0] <= $this->_data[2])? $this->_data[0]: $this->_data[2]: ($this->_data[1] <= $this->_data[2])? $this->_data[1]: $this->_data[2];
}
}
//
$objTriplet = new Triplet ();
echo "init:"; Var_dump ($objTriplet->init (1,2,3)); echo "<br/>";
echo "Get 1:"; Var_dump ($objTriplet->get (1)); echo "<br/>";
echo "Get 4:"; Var_dump ($objTriplet->get (4)); echo "<br/>"; False
echo "Put 3, 4:"; Var_dump ($objTriplet->put (3,4)); echo "<br/>";
echo "Max:"; Var_dump ($objTriplet->max ()); echo "<br/>";
echo "min:"; Var_dump ($objTriplet->min ()); echo "<br/>";
echo "isascending:" Var_dump ($objTriplet->isascending ()); echo "<br/>";
echo "isdescending:" Var_dump ($objTriplet->isdescending ()); echo "<br/>";
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.