Data Structure & amp; Algorithm (PHP description) triple Triplet
Data Structure & algorithm (PHP description) triple Triplet
- /**
- * 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"
";
- ?>
|