Page MenuHomePhabricator
Authored By
tstarling
May 1 2019, 2:10 AM
Size
1 KB
Referenced Files
None
Subscribers
None
<?php
class Templates {
private $treesByLength = [];
public function findConflict( $node, $parts, $index = 0 ) {
if ( $index >= count( $parts ) ) {
// If we reached the leaf node then a conflict is detected
return '';
}
$part = $parts[$index];
$result = false;
if ( $part === '*' ) {
foreach ( $node as $key => $childNode ) {
$result = $this->findConflict( $childNode, $parts, $index + 1 );
if ( $result !== false ) {
$result = "$key/$result";
}
}
} else {
if ( isset( $node[$part] ) ) {
$result = $this->findConflict( $node[$part], $parts, $index + 1 );
if ( $result !== false ) {
$result = "$part/$result";
}
}
if ( $result === false && isset( $node['*'] ) ) {
$result = $this->findConflict( $node['*'], $parts, $index + 1 );
if ( $result !== false ) {
$result = "*/$result";
}
}
}
return $result;
}
public function add( $template ) {
$parts = explode( '/', $template );
$length = count( $parts );
if ( !isset( $this->treesByLength[$length] ) ) {
$this->treesByLength[$length] = [];
}
$tree =& $this->treesByLength[$length];
$conflict = $this->findConflict( $tree, $parts );
if ( $conflict !== false ) {
echo "Found conflict in template $template with existing template $conflict\n";
return false;
}
foreach ( $parts as $index => $part ) {
if ( !isset( $tree[$part] ) ) {
$tree[$part] = [];
}
$tree =& $tree[$part];
}
}
}
$templates = new Templates;
while ( false !== ( $line = fgets( STDIN ) ) ) {
$templates->add( trim( $line ) );
}

File Metadata

Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
7387715
Default Alt Text
raw.txt (1 KB)

Event Timeline