Data Structure & algorithm (PHP description) triple Triplet

Source: Internet
Author: User
Data Structure & amp; Algorithm (PHP description) triple Triplet
Data Structure & algorithm (PHP description) triple Triplet

  1. /**
  2. * Triple Triplet
  3. */
  4. Class Triplet
  5. {
  6. Private $ _ data = null;
  7. // Initialize the productkey, devicename, and devicesecret
  8. Public function init ($ val1, $ val2, $ val3)
  9. {
  10. $ This-> _ data [0] = $ val1;
  11. $ This-> _ data [1] = $ val2;
  12. $ This-> _ data [2] = $ val3;
  13. Return true;
  14. }
  15. // Destroy the productkey, devicename, and devicesecret
  16. Public function destroy ()
  17. {
  18. Unset ($ this-> _ data );
  19. Return true;
  20. }
  21. // Returns the value of $ key.
  22. Public function get ($ key)
  23. {
  24. If ($ key <1 | $ key> 3) return false;
  25. Return $ this-> _ data [$ key-1];
  26. }
  27. // Set the value of $ key to $ val.
  28. Public function put ($ key, $ val)
  29. {
  30. If ($ key <1 | $ key> 3) return false;
  31. $ This-> _ data [$ key-1] = $ val;
  32. Return true;
  33. }
  34. // Whether to sort data in ascending order
  35. Public function isAscending ()
  36. {
  37. Return ($ this-> _ data [0] <= $ this-> _ data [1]) & ($ this-> _ data [1] <= $ this-> _ data [2]);
  38. }
  39. // Sort by descending order
  40. Public function isDescending ()
  41. {
  42. Return ($ this-> _ data [0] >=$ this-> _ data [1]) & ($ this-> _ data [1] >=$ this-> _ data [2]);
  43. }
  44. // Obtain the maximum value
  45. Public function max ()
  46. {
  47. 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];
  48. }
  49. // Obtain the minimum value
  50. Public function min ()
  51. {
  52. 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];
  53. }
  54. }
  55. $ ObjTriplet = new Triplet ();
  56. Echo "init :";
  57. Var_dump ($ objTriplet-> init (1, 2, 3 ));
  58. Echo"
    ";
  59. Echo "get 1 :";
  60. Var_dump ($ objTriplet-> get (1 ));
  61. Echo"
    ";
  62. Echo "get 4 :";
  63. Var_dump ($ objTriplet-> get (4 ));
  64. Echo"
    "; // False
  65. Echo "put 3, 4 :";
  66. Var_dump ($ objTriplet-> put (3, 4 ));
  67. Echo"
    ";
  68. Echo "max :";
  69. Var_dump ($ objTriplet-> max ());
  70. Echo"
    ";
  71. Echo "min :";
  72. Var_dump ($ objTriplet-> min ());
  73. Echo"
    ";
  74. Echo "isAscending :";
  75. Var_dump ($ objTriplet-> isAscending ());
  76. Echo"
    ";
  77. Echo "isDescending :";
  78. Var_dump ($ objTriplet-> isDescending ());
  79. Echo"
    ";
  80. ?>

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.