Data Structure & Algorithm (PHP description) ternary group Triplet
- /**
- * Ternary Group Triplet
- */
- Class Triplet
- {
- Private $_data = null;
- Initialize ternary groups
- Public Function init ($val 1, $val 2, $val 3)
- {
- $this-_data[0] = $val 1;
- $this-_data[1] = $val 2;
- $this-_data[2] = $val 3;
- return true;
- }
- Destroying ternary groups
- Public function Destroy ()
- {
- Unset ($this-_data);
- return true;
- }
- Returns the value of section $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 in ascending order
- Public Function isascending ()
- {
- Return ($this, _data[0] <= $this, _data[1]) && ($this, _data[1] <= $this, _data[2]);
- }
- Whether to sort by descending
- Public Function isdescending ()
- {
- Return ($this, _data[0] >= $this, _data[1]) && ($this, _data[1] >= $this, _data[2]);
- }
- Get 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];
- }
- Get 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
";
- ?>
Copy Code |