Data Structure & Algorithm (PHP description) ternary group Triplet

Source: Internet
Author: User
Data Structure & Algorithm (PHP description) ternary group Triplet
  1. /**
  2. * Ternary Group Triplet
  3. */
  4. Class Triplet
  5. {
  6. Private $_data = null;
  7. Initialize ternary groups
  8. Public Function init ($val 1, $val 2, $val 3)
  9. {
  10. $this-_data[0] = $val 1;
  11. $this-_data[1] = $val 2;
  12. $this-_data[2] = $val 3;
  13. return true;
  14. }
  15. Destroying ternary groups
  16. Public function Destroy ()
  17. {
  18. Unset ($this-_data);
  19. return true;
  20. }
  21. Returns the value of section $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 in ascending order
  35. Public Function isascending ()
  36. {
  37. Return ($this, _data[0] <= $this, _data[1]) && ($this, _data[1] <= $this, _data[2]);
  38. }
  39. Whether to sort by descending
  40. Public Function isdescending ()
  41. {
  42. Return ($this, _data[0] >= $this, _data[1]) && ($this, _data[1] >= $this, _data[2]);
  43. }
  44. Get 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. Get 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. ?>
Copy Code
  • 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.