Data structure & algorithm (PHP description) Triplet Triplet

WBOY
Release: 2016-07-25 08:50:56
Original
944 people have browsed it
Data structure & algorithm (PHP description) Triplet Triplet
  1. /**
  2. * Triplet
  3. */
  4. class Triplet
  5. {
  6. private $_data = null;
  7. // Initialize triplet
  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 triple
  16. public function destroy()
  17. {
  18. unset($this -> _data);
  19. return true;
  20. }
  21. // Return 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 the $key element 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 in descending order
  40. public function isDescending()
  41. {
  42. return ($this -> _data[0] > ;= $this -> _data[1]) && ($this -> _data[1] >= $this -> _data[2]);
  43. }
  44. // Get 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. // Get 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. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!