singrdk/base/Windows/AppReader/AppNode.cs

120 lines
2.4 KiB
C#
Raw Permalink Normal View History

2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
namespace AppReader
{
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
public class AppNode
{
ArrayList children;
string nodeName;
AppNode parent;
int count;
// whether or not we've already added our childrens' weights to ours
bool normalized;
public AppNode(string nodeName, AppNode parent)
{
this.nodeName = nodeName;
this.parent = parent;
if (parent != null)
parent.AddChild(this);
count = 0;
this.children = new ArrayList();
normalized = false;
}
public string Name
{
get { return nodeName; }
}
public int NumChildren
{
get { return children.Count; }
}
public AppNode Parent
{
get { return parent; }
}
public void Dump(string indent)
{
Console.WriteLine(indent + nodeName + ": " + count);
2008-11-17 18:29:00 -05:00
for (int i = 0; i < children.Count; i++)
2008-03-05 09:52:00 -05:00
{
((AppNode)children[i]).Dump(indent + "\t");
}
}
public AppNode Copy(AppNode inParent)
{
AppNode outnode = new AppNode(this.nodeName, inParent);
outnode.count = this.count;
outnode.normalized = this.normalized;
2008-11-17 18:29:00 -05:00
for (int i = 0; i < this.children.Count; i++)
2008-03-05 09:52:00 -05:00
{
// this looks like a throwaway, but in the constructor it will attach to us
((AppNode)this.children[i]).Copy(outnode);
}
return outnode;
}
public void AddChild(AppNode child)
{
children.Add(child);
return;
}
public void Increment(int amount)
{
this.count += amount;
return;
}
public AppNode this[int index]
{
get
{
return (AppNode)children[index];
}
set
{
children[index] = value;
return;
}
}
public int Weight()
{
if (normalized) return this.count;
normalized = true;
2008-11-17 18:29:00 -05:00
for (int i = 0; i < this.children.Count; i++)
2008-03-05 09:52:00 -05:00
{
this.count += ((AppNode)this.children[i]).Weight();
}
return this.count;
}
public void Flatten(ArrayList ar)
{
// put all our children in it before we go in (post-order)
2008-11-17 18:29:00 -05:00
for (int i = 0; i < this.children.Count; i++)
2008-03-05 09:52:00 -05:00
{
((AppNode)this.children[i]).Flatten(ar);
}
ar.Add(this);
return;
}
}
}