UniVRM/UniJSON/Scripts/ListTreeNode/ListTreeNodeArrayExtensions.cs
ousttrue df85433628 Merge commit 'df5f79584312cf4f6cc7ec1d73a78f6ba475c9ad' as 'UniJSON'
Co-authored-by: Deatrathias <dmailsec@gmail.com>
Co-authored-by: dj-kusuha <dj.kusuha+github@gmail.com>
Co-authored-by: ousttrue <ousttrue@gmail.com>
Co-authored-by: yutopp <yutopp@gmail.com>
2018-12-28 20:38:39 +09:00

52 lines
1.5 KiB
C#

using System.Collections.Generic;
using System.Linq;
namespace UniJSON
{
public static class ListTreeNodeArrayExtensions
{
public static IEnumerable<ListTreeNode<T>> ArrayItems<T>(this ListTreeNode<T> self) where T : IListTreeItem, IValue<T>
{
if (!self.IsArray()) throw new DeserializationException("is not array");
return self.Children;
}
public static ListTreeNode<T> GetArrrayItem<T>(this ListTreeNode<T> self, int index)
where T : IListTreeItem, IValue<T>
{
int i = 0;
foreach (var v in self.ArrayItems())
{
if (i++ == index)
{
return v;
}
}
throw new KeyNotFoundException();
}
public static int GetArrayCount<T>(this ListTreeNode<T> self)
where T : IListTreeItem, IValue<T>
{
if (!self.IsArray()) throw new DeserializationException("is not array");
return self.Children.Count();
}
public static int IndexOf<T>(this ListTreeNode<T> self, ListTreeNode<T> child)
where T : IListTreeItem, IValue<T>
{
int i = 0;
foreach (var v in self.ArrayItems())
{
if (v.ValueIndex == child.ValueIndex)
{
return i;
}
++i;
}
throw new KeyNotFoundException();
}
}
}