// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace EpicGames.BuildGraph.Expressions
{
///
/// Utility methods for lists
///
public static class BgList
{
///
/// Gets an empty list of the given type
///
///
///
public static BgList Empty() where T : BgExpr => BgList.Empty;
///
/// Crates a list from an array of values
///
/// Sequence to construct from
///
public static BgList Create(IEnumerable items) => BgList.Create(items.Select(x => (BgString)x));
///
/// Crates a list from an array of values
///
/// Sequence to construct from
///
public static BgList Create(params string[] items) => BgList.Create(items.Select(x => (BgString)x));
///
/// Crates a list from an array of values
///
/// Sequence to construct from
///
public static BgList Create(IEnumerable items) where T : BgExpr => BgList.Create(items);
///
/// Crates a list from an array of values
///
/// Sequence to construct from
///
public static BgList Create(params T[] items) where T : BgExpr => BgList.Create(items);
///
/// Concatenates two lists together
///
///
///
///
public static BgList Concat(BgList lhs, BgList rhs) where T : BgExpr => BgList.Concat(lhs, rhs);
///
/// Concatenates two lists together
///
///
///
public static BgList Concat(params BgList[] others) where T : BgExpr => BgList.Concat(others);
}
///
/// Abstract base class for expressions returning an immutable list of values
///
[BgType(typeof(BgListType<>))]
public abstract class BgList : BgExpr where T : BgExpr
{
///
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Type ElementType => typeof(T);
///
/// Constant representation of an empty list
///
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public static BgList Empty { get; } = new BgListEmptyExpr();
///
/// Constructor
///
/// Flags for this expression
protected BgList(BgExprFlags flags)
: base(flags)
{
}
///
public T this[BgInt index] => BgType.Wrap(new BgListElementExpr(this, index));
///
/// Implicit conversion operator from a single value
///
public static implicit operator BgList(T item) => Create(item);
///
/// Implicit conversion operator from an array of values
///
public static implicit operator BgList(T[] items) => Create(items);
///
/// Implicit conversion operator from a list of values
///
public static implicit operator BgList(List items) => Create(items);
///
/// Crates a list from an array of values
///
/// Sequence to construct from
///
public static BgList Create(IEnumerable items) => Empty.Add(items);
///
/// Crates a list from an array of values
///
/// Sequence to construct from
///
public static BgList Create(params T[] items) => Empty.Add(items);
///
/// Concatenates two lists together
///
///
///
///
public static BgList Concat(BgList lhs, BgList rhs) => new BgListConcatExpr(lhs, rhs);
///
/// Concatenates two lists together
///
///
///
public static BgList Concat(params BgList[] others)
{
BgList result = BgList.Empty;
for (int idx = 0; idx < others.Length; idx++)
{
result = Concat(result, others[idx]);
}
return result;
}
///
/// Gets the length of this list
///
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BgInt Count => new BgListCountExpr(this);
///
/// Adds an item to the end of the list, returning the new list
///
/// Items to add
/// New list containing the given items
public BgList Add(T item) => new BgListPushExpr(this, item);
///
/// Adds items to the end of the list, returning the new list
///
/// Items to add
/// New list containing the given items
public BgList Add(params T[] items) => Add((IEnumerable)items);
///
public BgList Add(BgList list) => Concat(this, list);
///
public BgList Add(IEnumerable items)
{
BgList list = this;
foreach (T item in items)
{
list = list.Add(item);
}
return list;
}
///
public BgList Union(params T[] items) => Union(Create(items));
///
public BgList Union(IEnumerable items) => Union(Create(items));
///
/// Creates the union of this list with another
///
/// Items to add
/// Union with the given items
public BgList Union(BgList items) => new BgListUnionExpr(this, items);
///
/// Removes the given items from this list
///
/// Items to remove
/// New list without the given items
public BgList Except(BgList items) => new BgListExceptExpr(this, items);
///
/// Removes any duplicate items from the list. The first item in the list is retained in its original order.
///
/// New list containing the distinct items.
public BgList Distinct() => new BgListDistinctExpr(this);
///
public BgList Select(Func function) where TResult : BgExpr => new BgListSelectExpr(this, function);
///
public BgList Select(BgFunc function) where TResult : BgExpr => new BgListSelectExpr(this, function);
///
public BgList Where(Func predicate) => new BgListWhereExpr(this, predicate);
///
public BgBool Contains(T item) => new BgListContainsExpr(this, item);
///
/// Creates a lazily evaluated copy of this list, if it's not constant
///
///
public BgList Lazy() => new BgListLazyEval(this);
///
public override BgString ToBgString() => "{List}";
}
///
/// Traits for a
///
class BgListType : BgType> where T : BgExpr
{
///
public override BgList Constant(object value)
{
IEnumerable