Hoa central
Boundinteger.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Realdom;
38 
39 use Hoa\Iterator;
40 use Hoa\Math;
41 
51  extends Integer
52  implements IRealdom\Interval,
56 {
62  const NAME = 'boundinteger';
63 
69  protected $_arguments = [
70  'Constinteger lower' => PHP_INT_MIN,
71  'Constinteger upper' => PHP_INT_MAX
72  ];
73 
79  protected $_discredited = [];
80 
81 
82 
88  protected function construct()
89  {
90  $lower = $this['lower']->getConstantValue();
91  $upper = $this['upper']->getConstantValue();
92 
93  if ($lower > $upper) {
94  throw new Exception\InvalidArgument(
95  '$lower must be strictly lower than $upper; given %d and %d.',
96  0,
97  [$lower, $upper]
98  );
99  }
100 
101  return;
102  }
103 
110  protected function _predicate($q)
111  {
112  return
113  parent::_predicate($q) &&
114  $q >= $this['lower']->getConstantValue() &&
115  $q <= $this['upper']->getConstantValue();
116  }
117 
124  protected function _sample(Math\Sampler $sampler)
125  {
126  return $sampler->getInteger(
127  $this['lower']->sample($sampler),
128  $this['upper']->sample($sampler),
129  $this->_discredited
130  );
131  }
132 
138  public function getLowerBound()
139  {
140  return $this['lower']->getConstantValue();
141  }
142 
148  public function getUpperBound()
149  {
150  return $this['upper']->getConstantValue();
151  }
152 
159  public function reduceRightTo($value)
160  {
161  $lower = $this['lower']->getConstantValue();
162  $upper = min($this['upper']->getConstantValue(), $value);
163 
164  if ($lower > $upper) {
165  return false;
166  }
167 
168  $this['upper'] = new Constinteger($value);
169 
170  return true;
171  }
172 
179  public function reduceLeftTo($value)
180  {
181  $lower = max($this['lower']->getConstantValue(), $value);
182  $upper = $this['upper']->getConstantValue();
183 
184  if ($lower > $upper) {
185  return false;
186  }
187 
188  $this['lower'] = new Constinteger($value);
189 
190  return true;
191  }
192 
199  public function discredit($value)
200  {
201  if (true === in_array($value, $this->_discredited) ||
202  false === $this->predicate($value)) {
203  return $this;
204  }
205 
206  $this->_discredited[] = $value;
207 
208  return $this;
209  }
210 
216  public function getSize()
217  {
218  return
219  $this['upper']->getConstantValue() -
220  $this['lower']->getConstantValue() -
221  count($this->_discredited) +
222  1;
223  }
224 
230  public function getIterator()
231  {
232  $discredited = &$this->_discredited;
233 
234  return
236  new Iterator\Counter(
237  $this['lower']->getConstantValue(),
238  $this['upper']->getConstantValue() + 1,
239  1
240  ),
241  function ($value) use (&$discredited) {
242  return false === in_array($value, $discredited);
243  }
244  );
245  }
246 }
_sample(Math\Sampler $sampler)
sample(Math\Sampler $sampler=null)
Definition: Realdom.php:686