PHP Data structure algorithm, triple, Triplet. For more information about php learning, see.
The code is as follows:
/**
* Triple Triplet
*
*/
Class Triplet
{
Private $ _ data = null;
// Initialize the productkey, devicename, and devicesecret
Public function init ($ val1, $ val2, $ val3)
{
$ This-> _ data [0] = $ val1;
$ This-> _ data [1] = $ val2;
$ This-> _ data [2] = $ val3;
Return true;
}
// Destroy the productkey, devicename, and devicesecret
Public function destroy ()
{
Unset ($ this-> _ data );
Return true;
}
// Returns the value of $ 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;
}
// Whether to sort data in ascending order
Public function isAscending ()
{
Return ($ this-> _ data [0] <= $ this-> _ data [1]) & ($ this-> _ data [1] <= $ this-> _ data [2]);
}
// Sort by descending order
Public function isDescending ()
{
Return ($ this-> _ data [0] >=$ this-> _ data [1]) & ($ this-> _ data [1] >=$ this-> _ data [2]);
}
// Obtain the 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];
}
// Obtain the 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"
";
Echo "get 1:"; var_dump ($ objTriplet-> get (1); echo"
";
Echo "get 4:"; var_dump ($ objTriplet-> get (4); echo"
"; // False
Echo "put 3, 4:"; var_dump ($ objTriplet-> put (3, 4); echo"
";
Echo "max:"; var_dump ($ objTriplet-> max (); echo"
";
Echo "min:"; var_dump ($ objTriplet-> min (); echo"
";
Echo "isAscending:"; var_dump ($ objTriplet-> isAscending (); echo"
";
Echo "isDescending:"; var_dump ($ objTriplet-> isDescending (); echo"
";
?>
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.