public static bool IsEmpty <T>(this SCG.IEnumerable <T> enumerable) { #region Code Contracts // Argument must be non-null Requires(enumerable != null, ArgumentMustBeNonNull); // Returns true if Count is zero, otherwise false Ensures(Result <bool>() != enumerable.Any()); #endregion var collectionValue = enumerable as ICollectionValue <T>; if (collectionValue != null) { return(collectionValue.IsEmpty); } var collection = enumerable as SCG.ICollection <T>; if (collection != null) { return(collection.Count == 0); } return(!enumerable.Any()); }
/* * this whole thing is meant for fully qualified type names * since c# has inner types, both A and A.B could be a valid target for a completion with payload * -> an optional payload on inner nodes must be supported * -> leafs must be non-empty */ public bool Add(SCG.IEnumerable <string> edges, Pay payload) { // TODO: exception on null payload, want null to be used internally and get away with not doing the maybe<T> dance if (!edges.Any()) { throw new InterfaceE(); } if (edges.Any(e => string.IsNullOrEmpty(e) /*|| string.IsNullOrWhiteSpace(e)*/)) { throw new InterfaceE(); // <- empty strings as keys destroy pretty much all assumptions } // TODO !!! ^._____ not available in Unity string first = edges.First(); var Rest = edges.Skip(1); if (!Rest.Any()) { return(AddLeaf(first, payload)); } if (!D.Contains(first)) { D[first] = new SuggestionTree <Pay>(); } // D[first] is guaranteed to exist at this point, but it might be a leaf -> convert to inner node // TODO this is prob. still not enough, depending on how this is supposed to behave upon input in which a path is duplicate // for example when simulating "using directives", overrriding paths and even replacing whole subtrees must be supported // ( das braucht wahrscheinlich einen Join( SuggTree1 , SuggTree2 ) siehe lustig bunte A4 blaetter ) if (D[first] is SuggestionLeaf <Pay> ) { var nu_tree = new SuggestionTree <Pay>(); nu_tree.payload = D[first].payload; D[first] = nu_tree; } return(D[first].Add(Rest, payload)); }
public static bool Find <T>(this SCG.IEnumerable <T> enumerable, Func <T, bool> predicate, out T item) { #region Code Contracts // Argument must be non-null Requires(enumerable != null, ArgumentMustBeNonNull); // Argument must be non-null Requires(predicate != null, ArgumentMustBeNonNull); // Returns true if p(x) returned true for some item x; otherwise false Ensures(Result <bool>() == enumerable.Any(predicate)); // Result item equals the first (or default) item satisfying the predicate Ensures(ValueAtReturn(out item).IsSameAs(enumerable.FirstOrDefault(predicate))); #endregion bool result; // Use Enumerable.Where() to be able to retrieve value for result using (var enumerator = enumerable.Where(predicate).GetEnumerator()) { item = (result = enumerator.MoveNext()) ? enumerator.Current : default(T); } return(result); }