Skip to content

Commit

Permalink
Merge pull request #12 from uzimaru0000/get-kids-IEnumreable
Browse files Browse the repository at this point in the history
Chagne argument for Node
  • Loading branch information
uzimaru0000 authored Jul 14, 2021
2 parents e44c769 + 315187c commit 02e079e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
33 changes: 21 additions & 12 deletions Lib/VTree/Node.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Veauty.VTree
Expand All @@ -13,7 +14,7 @@ public abstract class NodeBase<T> : IVTree, IParent
public readonly string tag;
public readonly Attributes<T> attrs;

protected NodeBase(string tag, IAttribute<T>[] attrs)
protected NodeBase(string tag, IEnumerable<IAttribute<T>> attrs)
{
this.tag = tag;
this.attrs = new Attributes<T>(attrs);
Expand All @@ -31,7 +32,7 @@ public class BaseNode<T> : NodeBase<T>
public readonly IVTree[] kids;
private readonly int descendantsCount;

protected BaseNode(string tag, IAttribute<T>[] attrs, params IVTree[] kids): base(tag, attrs)
protected BaseNode(string tag, IEnumerable<IAttribute<T>> attrs, params IVTree[] kids): base(tag, attrs)
{
this.kids = kids;
this.descendantsCount = 0;
Expand All @@ -43,6 +44,8 @@ protected BaseNode(string tag, IAttribute<T>[] attrs, params IVTree[] kids): bas
this.descendantsCount += kids.Length;
}

protected BaseNode(string tag, IEnumerable<IAttribute<T>> attrs, IEnumerable<IVTree> kids): this(tag, attrs, kids.ToArray()) {}

public override int GetDescendantsCount() => this.descendantsCount;
public override IVTree[] GetKids() => this.kids;

Expand Down Expand Up @@ -77,22 +80,24 @@ public override int GetHashCode()
}
}

public class Node<T, U> : BaseNode<U>, ITypedNode
public class Node<T, U> : BaseNode<T>, ITypedNode
{
private System.Type componentType;

public Node(string tag, IAttribute<U>[] attrs, params IVTree[] kids) : base(tag, attrs, kids)
public Node(string tag, IEnumerable<IAttribute<T>> attrs, params IVTree[] kids) : base(tag, attrs, kids)
{
componentType = typeof(T);
componentType = typeof(U);
}

public Node(string tag, IEnumerable<IAttribute<T>> attrs, IEnumerable<IVTree> kids) : this(tag, attrs, kids.ToArray()) {}

public Type GetComponentType() => componentType;

public override bool Equals(object obj) => this.Equals(obj as Node<T, U>);

bool Equals(Node<T, U> obj)
{
return base.Equals(obj as BaseNode<U>) && this.componentType == obj.componentType;
return base.Equals(obj as BaseNode<T>) && this.componentType == obj.componentType;
}

public override int GetHashCode()
Expand All @@ -103,7 +108,8 @@ public override int GetHashCode()

public class Node<T> : BaseNode<T>
{
public Node(string tag, IAttribute<T>[] attrs, params IVTree[] kids) : base(tag, attrs, kids) { }
public Node(string tag, IEnumerable<IAttribute<T>> attrs, params IVTree[] kids) : base(tag, attrs, kids) { }
public Node(string tag, IEnumerable<IAttribute<T>> attrs, IEnumerable<IVTree> kids) : base(tag, attrs, kids) { }
}

// KeyedNode
Expand All @@ -114,7 +120,7 @@ public class BaseKeyedNode<T> : NodeBase<T>
private readonly int descendantsCount;
private readonly IVTree[] dekeyedKids;

protected BaseKeyedNode(string tag, IAttribute<T>[] attrs, params (string, IVTree)[] kids) : base(tag, attrs)
protected BaseKeyedNode(string tag, IEnumerable<IAttribute<T>> attrs, params (string, IVTree)[] kids) : base(tag, attrs)
{
this.kids = kids;
this.descendantsCount = 0;
Expand All @@ -129,6 +135,8 @@ protected BaseKeyedNode(string tag, IAttribute<T>[] attrs, params (string, IVTre
this.descendantsCount += kids.Length;
}

protected BaseKeyedNode(string tag, IEnumerable<IAttribute<T>> attrs, IEnumerable<(string, IVTree)> kids) : this(tag, attrs, kids.ToArray()) {}

public override int GetDescendantsCount() => this.descendantsCount;

public override IVTree[] GetKids() => this.dekeyedKids;
Expand Down Expand Up @@ -164,22 +172,23 @@ public override int GetHashCode()
}
}

public class KeyedNode<T, U> : BaseKeyedNode<U>, ITypedNode
public class KeyedNode<T, U> : BaseKeyedNode<T>, ITypedNode
{
private System.Type componentType;

public KeyedNode(string tag, IAttribute<U>[] attrs, params (string, IVTree)[] kids) : base(tag, attrs, kids)
public KeyedNode(string tag, IEnumerable<IAttribute<T>> attrs, params (string, IVTree)[] kids) : base(tag, attrs, kids)
{
componentType = typeof(T);
componentType = typeof(U);
}
public KeyedNode(string tag, IEnumerable<IAttribute<T>> attrs, IEnumerable<(string, IVTree)> kids) : this(tag, attrs, kids.ToArray()) {}

public Type GetComponentType() => componentType;

public override bool Equals(object obj) => this.Equals(obj as KeyedNode<T, U>);

bool Equals(KeyedNode<T, U> obj)
{
return base.Equals(obj as BaseNode<U>) && this.componentType == obj.componentType;
return base.Equals(obj as BaseNode<T>) && this.componentType == obj.componentType;
}

public override int GetHashCode()
Expand Down
7 changes: 5 additions & 2 deletions Lib/VTree/Widget.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System.Collections.Generic;
using System.Linq;

namespace Veauty.VTree
{
public abstract class Widget<T> : IVTree, IParent
{
protected IAttribute<T>[] attrs;
protected IEnumerable<IAttribute<T>> attrs;
protected IVTree[] kids;

public Widget(IAttribute<T>[] attrs, params IVTree[] kids)
public Widget(IEnumerable<IAttribute<T>> attrs, params IVTree[] kids)
{
this.attrs = attrs;
this.kids = kids;
}

public Widget(IEnumerable<IAttribute<T>> attrs, IEnumerable<IVTree> kids) : this(attrs, kids.ToArray()) {}

public VTreeType GetNodeType() => VTreeType.Widget;

public IVTree[] GetKids() => this.kids;
Expand Down
18 changes: 9 additions & 9 deletions Tests/Editor/TestVTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,33 @@ public void TestDiff(Veauty.IVTree oldTree, Veauty.IVTree newTree, Veauty.IPatch
static object[] TestCase =
{
new object[] {
new Node<object>("tag", new IAttribute<object>[] {}, new IVTree[] {}),
new Node<object>("tag", new IAttribute<object>[] {}, new IVTree[] {}),
new Node<object>("tag", new IAttribute<object>[] {}),
new Node<object>("tag", new IAttribute<object>[] {}),
new IPatch<object>[] {}
},
new object[] {
new Node<object>("tag", new IAttribute<object>[] {
new TestableAttribute(10)
}, new IVTree[] {}),
}),
new Node<object>("tag", new IAttribute<object>[] {
new TestableAttribute(10)
}, new IVTree[] {}),
}),
new IPatch<object>[] {}
},
new object[] {
new Node<object>("tag", new IAttribute<object>[] {}, new IVTree[] {
new Node<object>("child", new IAttribute<object>[] {}, new IVTree[] {})
new Node<object>("child", new IAttribute<object>[] {})
}),
new Node<object>("tag", new IAttribute<object>[] {}, new IVTree[] {
new Node<object>("child", new IAttribute<object>[] {}, new IVTree[] {})
new Node<object>("child", new IAttribute<object>[] {})
}),
new IPatch<object>[] {}
},
new object[] {
new Node<object>("tag", new IAttribute<object>[] {}, new IVTree[] {}),
new Node<object>("gat", new IAttribute<object>[] {}, new IVTree[] {}),
new Node<object>("tag", new IAttribute<object>[] {}),
new Node<object>("gat", new IAttribute<object>[] {}),
new IPatch<object>[] {
new Redraw<object>(0, new Node<object>("gat", new IAttribute<object>[] {}, new IVTree[] {}))
new Redraw<object>(0, new Node<object>("gat", new IAttribute<object>[] {}))
},
},
new object[] {
Expand Down

0 comments on commit 02e079e

Please sign in to comment.