<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://htyp.org/mw/index.php?action=history&amp;feed=atom&amp;title=User%3AWoozle%2Ftree.php</id>
	<title>User:Woozle/tree.php - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://htyp.org/mw/index.php?action=history&amp;feed=atom&amp;title=User%3AWoozle%2Ftree.php"/>
	<link rel="alternate" type="text/html" href="https://htyp.org/mw/index.php?title=User:Woozle/tree.php&amp;action=history"/>
	<updated>2026-07-05T12:46:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://htyp.org/mw/index.php?title=User:Woozle/tree.php&amp;diff=16545&amp;oldid=prev</id>
		<title>Woozle: saving current version before rework attempt</title>
		<link rel="alternate" type="text/html" href="https://htyp.org/mw/index.php?title=User:Woozle/tree.php&amp;diff=16545&amp;oldid=prev"/>
		<updated>2011-12-25T14:51:36Z</updated>

		<summary type="html">&lt;p&gt;saving current version before rework attempt&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==About==&lt;br /&gt;
This implements a hierarchy (&amp;quot;acyclic directional graph&amp;quot;, I think...) where each node can have zero or more children and zero or one parent.&lt;br /&gt;
&lt;br /&gt;
This is the version where node names are only kept in the parent, which can make debugging difficult.&lt;br /&gt;
==Code==&lt;br /&gt;
&amp;lt;php&amp;gt;&amp;lt;?php&lt;br /&gt;
/*&lt;br /&gt;
  FILE: tree.php -- general tree-handling classes&lt;br /&gt;
  HISTORY:&lt;br /&gt;
    2010-10-13 Extricated from shop.php&lt;br /&gt;
    2011-12-18 added access counter ($intRefs)&lt;br /&gt;
*/&lt;br /&gt;
class clsTreeNode {&lt;br /&gt;
    public $vVal;	// data value&lt;br /&gt;
    protected $arSubs;	// sub-nodes&lt;br /&gt;
    protected $objParent;&lt;br /&gt;
    protected $intRefs;&lt;br /&gt;
&lt;br /&gt;
    /*----&lt;br /&gt;
      ACTION: Creates node with a set of subnodes&lt;br /&gt;
      USED BY: clsShopCart::AddrShipObj() etc.&lt;br /&gt;
      DEPRECATED:&lt;br /&gt;
	- difficult to search for&lt;br /&gt;
	- blocks other possible initializations (PHP has no function overloading)&lt;br /&gt;
    */&lt;br /&gt;
    public function __construct($iNodes=NULL) {&lt;br /&gt;
	if (is_array($iNodes)) {&lt;br /&gt;
	    $this-&amp;gt;arSubs = $iNodes;&lt;br /&gt;
&lt;br /&gt;
	    foreach ($iNodes as $name =&amp;gt; $node) {&lt;br /&gt;
		$node-&amp;gt;Parent($this);&lt;br /&gt;
	    }&lt;br /&gt;
	}&lt;br /&gt;
	$this-&amp;gt;objParent = NULL;&lt;br /&gt;
    }&lt;br /&gt;
    public function Parent(clsTreeNode $iNode=NULL) {&lt;br /&gt;
	if (!is_null($iNode)) {&lt;br /&gt;
	    $this-&amp;gt;objParent = $iNode;&lt;br /&gt;
	}&lt;br /&gt;
	return $this-&amp;gt;objParent;&lt;br /&gt;
    }&lt;br /&gt;
    public function HasParent() {&lt;br /&gt;
	return (!is_null($this-&amp;gt;objParent));&lt;br /&gt;
    }&lt;br /&gt;
    protected function NodeAdd($iName,clsTreeNode $iNode) {&lt;br /&gt;
	$this-&amp;gt;arSubs[$iName] = $iNode;&lt;br /&gt;
	$iNode-&amp;gt;Parent($this);&lt;br /&gt;
    }&lt;br /&gt;
    public function Node($iName,clsTreeNode $iNode=NULL) {&lt;br /&gt;
	if (!is_null($iNode)) {&lt;br /&gt;
	    $this-&amp;gt;NodeAdd($iName,$iNode);&lt;br /&gt;
	}&lt;br /&gt;
	if (isset($this-&amp;gt;arSubs[$iName])) {&lt;br /&gt;
	    return $this-&amp;gt;arSubs[$iName];&lt;br /&gt;
	} else {&lt;br /&gt;
	    return NULL;&lt;br /&gt;
	}&lt;br /&gt;
    }&lt;br /&gt;
    public function Exists($iName) {&lt;br /&gt;
	if (isset($this-&amp;gt;arSubs[$iName])) {&lt;br /&gt;
	    return isset($this-&amp;gt;arSubs[$iName]);&lt;br /&gt;
	} else {&lt;br /&gt;
	    return FALSE;&lt;br /&gt;
	}&lt;br /&gt;
    }&lt;br /&gt;
    public function Loaded($iName=NULL) {&lt;br /&gt;
	if (is_null($iName)) {&lt;br /&gt;
	    return isset($this-&amp;gt;vVal);&lt;br /&gt;
	} else {&lt;br /&gt;
	    if ($this-&amp;gt;Exists($iName)) {&lt;br /&gt;
		return $this-&amp;gt;Node($iName)-&amp;gt;Loaded();&lt;br /&gt;
	    } else {&lt;br /&gt;
		return FALSE;&lt;br /&gt;
	    }&lt;br /&gt;
	}&lt;br /&gt;
    }&lt;br /&gt;
    /*----&lt;br /&gt;
      RETURNS: TRUE if there&amp;#039;s a value by that name that is loaded.&lt;br /&gt;
      NOTE: The value may exist, but if it&amp;#039;s not loaded this will still be FALSE.&lt;br /&gt;
	Why do we do it that way? DOCUMENT!! Might not be the way to do it.&lt;br /&gt;
    */&lt;br /&gt;
    public function Filled($iName=NULL) {&lt;br /&gt;
	if (is_null($iName)) {&lt;br /&gt;
	    if (!$this-&amp;gt;Loaded()) {&lt;br /&gt;
		$this-&amp;gt;vVal = $this-&amp;gt;Value();	// is this in danger of being recursive?&lt;br /&gt;
	    }&lt;br /&gt;
	    return !empty($this-&amp;gt;vVal);&lt;br /&gt;
	} else {&lt;br /&gt;
	    if ($this-&amp;gt;Loaded($iName)) {&lt;br /&gt;
		return $this-&amp;gt;Node($iName)-&amp;gt;Filled();&lt;br /&gt;
	    } else {&lt;br /&gt;
		return FALSE;&lt;br /&gt;
	    }&lt;br /&gt;
	}&lt;br /&gt;
    }&lt;br /&gt;
    public function Spawn() {&lt;br /&gt;
	$obj = new clsTreeNode();&lt;br /&gt;
	return $obj;&lt;br /&gt;
    }&lt;br /&gt;
    public function Value($iVal=NULL) {&lt;br /&gt;
	if (!is_null($iVal)) {&lt;br /&gt;
	    $this-&amp;gt;vVal = $iVal;&lt;br /&gt;
	}&lt;br /&gt;
	return $this-&amp;gt;vVal;&lt;br /&gt;
    }&lt;br /&gt;
    /*----&lt;br /&gt;
      FUTURE: For consistency, should probably be renamed NodeValue()&lt;br /&gt;
    */&lt;br /&gt;
    public function SubValue($iName,$iVal=NULL) {&lt;br /&gt;
	if (!is_null($iVal)) {&lt;br /&gt;
	    $obj = $this-&amp;gt;Spawn();&lt;br /&gt;
	    $obj-&amp;gt;Value($iVal);&lt;br /&gt;
	} else {&lt;br /&gt;
	    $obj = NULL;&lt;br /&gt;
	}&lt;br /&gt;
	return $this-&amp;gt;Node($iName,$obj)-&amp;gt;Value();&lt;br /&gt;
    }&lt;br /&gt;
    public function Nodes($iNodes=NULL) {&lt;br /&gt;
	if (is_array($iNodes)) {&lt;br /&gt;
	    $this-&amp;gt;arSubs = $iNodes;&lt;br /&gt;
	}&lt;br /&gt;
	return $this-&amp;gt;arSubs;&lt;br /&gt;
    }&lt;br /&gt;
    public function HasNodes() {&lt;br /&gt;
	if (isset($this-&amp;gt;arSubs)) {&lt;br /&gt;
	    return (count($this-&amp;gt;arSubs) &amp;gt; 0);&lt;br /&gt;
	} else {&lt;br /&gt;
	    return FALSE;&lt;br /&gt;
	}&lt;br /&gt;
    }&lt;br /&gt;
    public function AccessCount() {&lt;br /&gt;
	return $this-&amp;gt;intRefs;&lt;br /&gt;
    }&lt;br /&gt;
    public function IncCount() {&lt;br /&gt;
	$this-&amp;gt;intRefs++;&lt;br /&gt;
    }&lt;br /&gt;
    public function ResetCount() {&lt;br /&gt;
	$this-&amp;gt;intRefs = 0;&lt;br /&gt;
	if ($this-&amp;gt;HasNodes()) {&lt;br /&gt;
	    foreach ($this-&amp;gt;Nodes() as $name =&amp;gt; $node) {&lt;br /&gt;
		$node-&amp;gt;ResetCount();&lt;br /&gt;
	    }&lt;br /&gt;
	}&lt;br /&gt;
    }&lt;br /&gt;
    public function DumpHTML() {&lt;br /&gt;
	$out = NULL;&lt;br /&gt;
	if ($this-&amp;gt;intRefs &amp;gt; 0) {&lt;br /&gt;
	    $out = &amp;#039;[&amp;lt;b&amp;gt;&amp;lt;font color=#aa6600&amp;gt;x&amp;#039;.$this-&amp;gt;intRefs.&amp;#039;&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;]&amp;#039;;&lt;br /&gt;
	}&lt;br /&gt;
	$this-&amp;gt;intRefs++;&lt;br /&gt;
	if ($this-&amp;gt;HasNodes()) {&lt;br /&gt;
	    $out = &amp;#039;&amp;lt;ul&amp;gt;&amp;#039;;&lt;br /&gt;
	    foreach ($this-&amp;gt;Nodes() as $name =&amp;gt; $node) {&lt;br /&gt;
		$isAlias = ($node-&amp;gt;Parent() != $this);&lt;br /&gt;
		if ($isAlias) {&lt;br /&gt;
		    $out .= &amp;#039;&amp;lt;i&amp;gt;&amp;#039;;&lt;br /&gt;
		}&lt;br /&gt;
		$strCls = get_class($node);&lt;br /&gt;
		$out .= &amp;#039;&amp;lt;li&amp;gt; [&amp;#039;.$strCls.&amp;#039;] &amp;lt;b&amp;gt;&amp;#039;.$name.&amp;#039;&amp;lt;/b&amp;gt; = [&amp;#039;.$node-&amp;gt;Value().&amp;#039;]&amp;#039;;&lt;br /&gt;
		$out .= $node-&amp;gt;DumpHTML();&lt;br /&gt;
		if ($isAlias) {&lt;br /&gt;
		    $out .= &amp;#039;&amp;lt;/i&amp;gt;&amp;#039;;&lt;br /&gt;
		}&lt;br /&gt;
		if (!$node-&amp;gt;HasParent()) {&lt;br /&gt;
		    $out .= &amp;#039;&amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;&amp;lt;b&amp;gt;INTERNAL ERROR&amp;lt;/b&amp;gt;: parent not set&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&amp;#039;;&lt;br /&gt;
		}&lt;br /&gt;
	    }&lt;br /&gt;
	    $out .= &amp;#039;&amp;lt;/ul&amp;gt;&amp;#039;;&lt;br /&gt;
	}&lt;br /&gt;
	return $out;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/php&amp;gt;&lt;/div&gt;</summary>
		<author><name>Woozle</name></author>
	</entry>
</feed>