Hoa central
Gamma.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\Iterator;
40 
53 class Gamma implements Iterator
54 {
60  protected $_n = 0;
61 
67  protected $_k = 0;
68 
74  protected $_current = null;
75 
81  protected $_key = -1;
82 
88  protected $_tmp = null;
89 
95  protected $_i = 0;
96 
102  protected $_o = 0;
103 
110  protected $_last = false;
111 
112 
113 
121  public function __construct($n, $k)
122  {
123  $this->_n = $n;
124  $this->_k = $k;
125 
126  return;
127  }
128 
134  public function current()
135  {
136  return $this->_current;
137  }
138 
144  public function key()
145  {
146  return $this->_key;
147  }
148 
154  public function next()
155  {
156  return;
157  }
158 
164  public function rewind()
165  {
166  $this->_current = [];
167  $this->_tmp = null;
168  $this->_i = 0;
169  $this->_o = 0 === $this->_n
170  ? [0]
171  : array_fill(0, $this->_n, 0);
172  $this->_o[0] = $this->_k;
173  $this->_last = false;
174 
175  return;
176  }
177 
183  public function valid()
184  {
185  if (true === $this->_last) {
186  return false;
187  }
188 
189  if (0 === $this->_n) {
190  return false;
191  }
192 
193  if ($this->_k == $this->_o[$this->_i = $this->_n - 1]) {
194  $this->_last = true;
195  $this->_current = $this->_o;
196  ++$this->_key;
197 
198  return true;
199  }
200 
201  $this->_current = $this->_o;
202  ++$this->_key;
203 
204  $this->_tmp = $this->_o[$this->_i];
205  $this->_o[$this->_i] = 0;
206 
207  while ($this->_o[$this->_i] == 0) {
208  --$this->_i;
209  }
210 
211  --$this->_o[$this->_i];
212  $this->_o[$this->_i + 1] = $this->_tmp + 1;
213 
214  return true;
215  }
216 }