Tries everywhere!
This commit is contained in:
83
Trie.php
83
Trie.php
@ -2,73 +2,66 @@
|
||||
|
||||
|
||||
class Trie implements ArrayAccess, IteratorAggregate, Countable {
|
||||
public array $noeud = [];
|
||||
public array $branches = [];
|
||||
private $nb_branches = 0;
|
||||
|
||||
public function offsetSet($cles, $valeur): void {
|
||||
if (!count($cles)) {
|
||||
throw new \OutOfBoundsException("Liste de clés vide.");
|
||||
}
|
||||
$cle = array_shift($cles);
|
||||
if (!isset($this->noeud[$cle])) $this->noeud[$cle] = new Trie();
|
||||
$this->nb_branches++;
|
||||
if (count($cles)) {
|
||||
$this->noeud[$cle]->offsetSet($cles, $valeur);
|
||||
} else {
|
||||
$this->noeud[$cle] = $valeur;
|
||||
}
|
||||
}
|
||||
|
||||
// ArrayAccess
|
||||
public function offsetExists($cles): bool {
|
||||
if (!count($cles)) {
|
||||
return false;
|
||||
}
|
||||
$cle = array_shift($cles);
|
||||
if (count($cles)) {
|
||||
return $this->noeud[$cle]->offsetExists($cles);
|
||||
public function offsetSet($cles, $valeur): void {
|
||||
$this->nb_branches++;
|
||||
$cle = $cles[0];
|
||||
$cles = array_slice($cles, 1);
|
||||
if ($cles == []) {
|
||||
$this->branches[$cle] = $valeur;
|
||||
} else {
|
||||
return isset($this->noeud[$cles[0]]);
|
||||
if (!isset($this->branches[$cle])) $this->branches[$cle] = new Trie();
|
||||
$this->branches[$cle]->offsetSet($cles, $valeur);
|
||||
}
|
||||
}
|
||||
|
||||
public function offsetGet($cles): mixed {
|
||||
if (!count($cles)) {
|
||||
throw new \OutOfBoundsException("Liste de clés vide.");
|
||||
}
|
||||
$cle = array_shift($cles);
|
||||
if (!isset($this->noeud[$cle])) $this->noeud[$cle] = new Trie();
|
||||
if (count($cles)) {
|
||||
return $this->noeud[$cle]->offsetGet($cles);
|
||||
public function offsetExists($cles): bool {
|
||||
$cle = $cles[0];
|
||||
$cles = array_slice($cles, 1);
|
||||
if ($cles == []) {
|
||||
return isset($this->branches[$cle]);
|
||||
} else {
|
||||
return $this->noeud[$cle];
|
||||
return isset($this->branches[$cle]) && $this->branches[$cle]->offsetExists($cles);
|
||||
}
|
||||
}
|
||||
|
||||
public function &offsetGet($cles): mixed {
|
||||
$cle = $cles[0];
|
||||
$cles = array_slice($cles, 1);
|
||||
if ($cles == []) {
|
||||
return $this->branches[$cle];
|
||||
} else {
|
||||
return $this->branches[$cle]->offsetGet($cles);
|
||||
}
|
||||
}
|
||||
|
||||
public function offsetUnset($cles): void {
|
||||
if ($this->offsetExists($cles)) {
|
||||
$cle = array_shift($cles);
|
||||
$cle = $cles[0];
|
||||
$cles = array_slice($cles, 1);
|
||||
if ($cles == []) {
|
||||
unset($this->branches[$cle]);
|
||||
$this->nb_branches--;
|
||||
if (count($cles)) {
|
||||
$this->noeud[$cle]->offsetUnset($cles);
|
||||
if (count($this->noeud[$cle]) == 0) {
|
||||
unset($this->noeud[$cle]);
|
||||
}
|
||||
} else {
|
||||
unset($this->noeud[$cle]);
|
||||
} else {
|
||||
$this->branches[$cle]->offsetUnset($cles);
|
||||
$this->nb_branches--;
|
||||
if (count($this->branches[$cle]) == 0) {
|
||||
unset($this->branches[$cle]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IteratorAggregate
|
||||
public function getIterator(): Generator {
|
||||
foreach ($this->noeud as $cle => $branche) {
|
||||
public function getIterator(): Traversable {
|
||||
foreach ($this->branches as $cle => $branche) {
|
||||
if ($branche instanceof Trie) {
|
||||
foreach($branche as $sous_cles => $feuille) {
|
||||
yield [$cle, ...$sous_cles] => $feuille;
|
||||
yield array_merge([$cle], $sous_cles) => $feuille;
|
||||
}
|
||||
} else {
|
||||
yield $cle => $branche;
|
||||
yield [$cle] => $branche;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user