📄 tree2.cs
字号:
if ( !bDeepCopy )
newChildNode._Data = oldChildNode.Data;
else
newChildNode._Data = DeepCopyData( oldChildNode.Data );
// if ( ! bDeepCopy )
// OnCopying( oldChildNode, newChildNode );
// else
// OnDeepCopying( oldChildNode, newChildNode );
if ( oldChildNode.Previous == null ) newNode._Child = newChildNode;
newChildNode._Parent = newNode;
newChildNode._Previous = previousNewChildNode;
if ( previousNewChildNode != null ) previousNewChildNode._Next = newChildNode;
// if ( ! bDeepCopy )
// OnCopied( oldChildNode, newChildNode );
// else
// OnDeepCopied( oldChildNode, newChildNode );
CopyChildNodes( oldChildNode, newChildNode, bDeepCopy );
previousNewChildNode = newChildNode;
}
}
protected virtual void RemoveCore()
{
if ( this.IsRoot ) throw new InvalidOperationException( "This is a Root" );
CutCore();
}
//-----------------------------------------------------------------------------
public bool CanMoveToParent
{
get
{
if ( this.IsRoot ) return false;
if ( this.IsTop ) return false;
return true;
}
}
public bool CanMoveToPrevious
{
get
{
if ( this.IsRoot ) return false;
if ( this.IsFirst ) return false;
return true;
}
}
public bool CanMoveToNext
{
get
{
if ( this.IsRoot ) return false;
if ( this.IsLast ) return false;
return true;
}
}
public bool CanMoveToChild
{
get
{
if ( this.IsRoot ) return false;
if ( this.IsFirst ) return false;
return true;
}
}
public bool CanMoveToFirst
{
get
{
if ( this.IsRoot ) return false;
if ( this.IsFirst ) return false;
return true;
}
}
public bool CanMoveToLast
{
get
{
if ( this.IsRoot ) return false;
if ( this.IsLast ) return false;
return true;
}
}
//-----------------------------------------------------------------------------
public void MoveToParent()
{
if ( !CanMoveToParent ) throw new InvalidOperationException( "Cannot move to Parent" );
NodeTree<T> parentNode = GetNodeTree( this.Parent );
NodeTree<T> thisNode = this.CutCore();
parentNode.InsertNextCore( thisNode );
}
public void MoveToPrevious()
{
if ( !CanMoveToPrevious ) throw new InvalidOperationException( "Cannot move to Previous" );
NodeTree<T> previousNode = GetNodeTree( this.Previous );
NodeTree<T> thisNode = this.CutCore();
previousNode.InsertPreviousCore( thisNode );
}
public void MoveToNext()
{
if ( !CanMoveToNext ) throw new InvalidOperationException( "Cannot move to Next" );
NodeTree<T> nextNode = GetNodeTree( this.Next );
NodeTree<T> thisNode = this.CutCore();
nextNode.InsertNextCore( thisNode );
}
public void MoveToChild()
{
if ( !CanMoveToChild ) throw new InvalidOperationException( "Cannot move to Child" );
NodeTree<T> previousNode = GetNodeTree( this.Previous );
NodeTree<T> thisNode = this.CutCore();
previousNode.AddChildCore( thisNode );
}
public void MoveToFirst()
{
if ( !CanMoveToFirst ) throw new InvalidOperationException( "Cannot move to first" );
NodeTree<T> firstNode = GetNodeTree( this.First );
NodeTree<T> thisNode = this.CutCore();
firstNode.InsertPreviousCore( thisNode );
}
public void MoveToLast()
{
if ( !CanMoveToLast ) throw new InvalidOperationException( "Cannot move to last" );
NodeTree<T> lastNode = GetNodeTree( this.Last );
NodeTree<T> thisNode = this.CutCore();
lastNode.InsertNextCore( thisNode );
}
//-----------------------------------------------------------------------------
// Enumerators
public virtual IEnumerableCollection<INode<T>> Nodes
{
get
{
return All.Nodes;
}
}
public virtual IEnumerableCollection<T> Values
{
get
{
return All.Values;
}
}
//-----------------------------------------------------------------------------
// BaseEnumerableCollectionPair
protected abstract class BaseEnumerableCollectionPair : IEnumerableCollectionPair<T>
{
private NodeTree<T> _Root = null;
protected NodeTree<T> Root
{
get { return _Root; }
set { _Root = value; }
}
protected BaseEnumerableCollectionPair( NodeTree<T> root )
{
_Root = root;
}
// Nodes
public abstract IEnumerableCollection<INode<T>> Nodes { get; }
protected abstract class BaseNodesEnumerableCollection : IEnumerableCollection<INode<T>>, IEnumerator<INode<T>>
{
private int _Version = 0;
private object _SyncRoot = new object();
private NodeTree<T> _Root = null;
protected NodeTree<T> Root
{
get { return _Root; }
set { _Root = value; }
}
private INode<T> _CurrentNode = null;
protected INode<T> CurrentNode
{
get { return _CurrentNode; }
set { _CurrentNode = value; }
}
private bool _BeforeFirst = true;
protected bool BeforeFirst
{
get { return _BeforeFirst; }
set { _BeforeFirst = value; }
}
private bool _AfterLast = false;
protected bool AfterLast
{
get { return _AfterLast; }
set { _AfterLast = value; }
}
protected BaseNodesEnumerableCollection( NodeTree<T> root )
{
_Root = root;
_CurrentNode = root;
_Version = _Root.Version;
}
~BaseNodesEnumerableCollection()
{
Dispose( false );
}
protected abstract BaseNodesEnumerableCollection CreateCopy();
protected virtual bool HasChanged { get { return _Root.HasChanged( _Version ); } }
// IDisposable
public void Dispose()
{
Dispose( true );
GC.SuppressFinalize( this );
}
protected virtual void Dispose( bool disposing )
{
}
// IEnumerable
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
// IEnumerable<INode<T>>
public virtual IEnumerator<INode<T>> GetEnumerator()
{
return this;
}
// ICollection
public virtual int Count
{
get
{
BaseNodesEnumerableCollection e = CreateCopy();
int i = 0;
foreach ( INode<T> o in e ) i++;
return i;
}
}
public virtual bool IsSynchronized { get { return false; } }
public virtual object SyncRoot { get { return _SyncRoot; } }
void ICollection.CopyTo( Array array, int index )
{
if ( array == null ) throw new ArgumentNullException( "array" );
if ( array.Rank > 1 ) throw new ArgumentException( "array is multidimensional", "array" );
if ( index < 0 ) throw new ArgumentOutOfRangeException( "index" );
int count = Count;
if ( count > 0 )
if ( index >= array.Length ) throw new ArgumentException( "index is out of bounds", "index" );
if ( index + count > array.Length ) throw new ArgumentException( "Not enough space in array", "array" );
BaseNodesEnumerableCollection e = CreateCopy();
foreach ( INode<T> n in e )
array.SetValue( n, index++ );
}
public virtual void CopyTo( T[] array, int index )
{
( ( ICollection ) this ).CopyTo( array, index );
}
// ICollectionEnumerable<INode<T>>
public virtual bool Contains( INode<T> item )
{
BaseNodesEnumerableCollection e = CreateCopy();
IEqualityComparer<INode<T>> comparer = EqualityComparer<INode<T>>.Default;
foreach ( INode<T> n in e )
if ( comparer.Equals( n, item ) )
return true;
return false;
}
// IEnumerator
object IEnumerator.Current
{
get { return Current; }
}
// IEnumerator<INode<T>>
public virtual void Reset()
{
if ( HasChanged ) throw new InvalidOperationException( "Tree has been modified." );
_CurrentNode = _Root;
_BeforeFirst = true;
_AfterLast = false;
}
public virtual bool MoveNext()
{
if ( HasChanged ) throw new InvalidOperationException( "Tree has been modified." );
_BeforeFirst = false;
return true;
}
public virtual INode<T> Current
{
get
{
if ( _BeforeFirst ) throw new InvalidOperationException( "Enumeration has not started." );
if ( _AfterLast ) throw new InvalidOperationException( "Enumeration has finished." );
return _CurrentNode;
}
}
}
// Values
public virtual IEnumerableCollection<T> Values
{
get
{
return new ValuesEnumerableCollection( _Root.DataComparer, this.Nodes );
}
}
private class ValuesEnumerableCollection : IEnumerableCollection<T>, IEnumerator<T>
{
IEqualityComparer<T> _DataComparer;
private IEnumerableCollection<INode<T>> _Nodes;
private IEnumerator<INode<T>> _Enumerator;
public ValuesEnumerableCollection( IEqualityComparer<T> dataComparer, IEnumerableCollection<INode<T>> nodes )
{
_DataComparer = dataComparer;
_Nodes = nodes;
_Enumerator = _Nodes.GetEnumerator();
}
protected ValuesEnumerableCollection( ValuesEnumerableCollection o )
{
_Nodes = o._Nodes;
_Enumerator = _Nodes.GetEnumerator();
}
protected virtual ValuesEnumerableCollection CreateCopy()
{
return new ValuesEnumerableCollection( this );
}
// IDisposable
~ValuesEnumerableCollection()
{
Dispose( false );
}
public void Dispose()
{
Dispose( true );
GC.SuppressFinalize( this );
}
protected virtual void Dispose( bool disposing )
{
}
// IEnumerable
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
// IEnumerable<T>
public virtual IEnumerator<T> GetEnumerator()
{
return this;
}
// ICollection
public virtual int Count
{
get
{
return _Nodes.Count;
}
}
public virtual bool IsSynchronized { get { return false; } }
public virtual object SyncRoot { get { return _Nodes.SyncRoot; } }
public virtual void CopyTo( Array array, int index )
{
if ( array == null ) throw new ArgumentNullException( "array" );
if ( array.Rank > 1 ) throw new ArgumentException( "array is multidimensional", "array" );
if ( index < 0 ) throw new ArgumentOutOfRangeException( "index" );
int count = Count;
if ( count > 0 )
if ( index >= array.Length ) throw new ArgumentException( "index is out of bounds", "index" );
if ( index + count > array.Length ) throw new ArgumentException( "Not enough space in array", "array" );
ValuesEnumerableCollection e = CreateCopy();
foreach ( T n in e )
array.SetValue( n, index++ );
}
// IEnumerableCollection<T>
public virtual bool Contains( T item )
{
ValuesEnumerableCollection e = CreateCopy();
foreach ( T n in e )
if ( _DataComparer.Equals( n, item ) )
return true;
return false;
}
// IEnumerator
object IEnumerator.Current
{
get { return Current; }
}
// IEnumerator<T> Members
public virtual void Reset()
{
_Enumerator.Reset();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -