using System.Collections.Generic; using Swarmops.Basic.Interfaces; namespace Swarmops.Logic.Support { public class PluralBase : List, IHasIdentities where TPlural : PluralBase, new() where TBasic : IHasIdentity where TSingular : IHasIdentity { public int[] Identities { get { List result = new List(); foreach (TSingular singular in this) { result.Add(singular.Identity); } return result.ToArray(); } } public static TPlural FromArray(TBasic[] basicArray) { TPlural result = new TPlural {Capacity = basicArray.Length*11/10}; foreach (TBasic basic in basicArray) { result.Add((TSingular) SingularFactory.FromBasic(basic)); } return result; } public static TPlural FromSingle(TSingular singular) { TPlural result = new TPlural(); result.Add(singular); return result; } public static TPlural FromSingle(TBasic basic) { TPlural result = new TPlural(); result.Add((TSingular) SingularFactory.FromBasic(basic)); return result; } public static TPlural LogicalOr(TPlural set1, TPlural set2) { // If either set is invalid, return the other // (a null is different from an empty set) if (set1 == null) { return set2; } if (set2 == null) { return set1; } // Build table, eliminating duplicates Dictionary table = new Dictionary(); foreach (TSingular singular in set1) { table[singular.Identity] = singular; } foreach (TSingular singular in set2) { table[singular.Identity] = singular; } // Assemble result TPlural result = new TPlural(); foreach (TSingular singular in table.Values) { result.Add(singular); } return result; } public static TPlural LogicalAnd(TPlural set1, TPlural set2) { // If either set is invalid, return the other // (a null is different from an empty set) if (set1 == null) { return set2; } if (set2 == null) { return set1; } Dictionary set2Lookup = new Dictionary(); // Build set2's lookup table foreach (TSingular singular in set2) { set2Lookup[singular.Identity] = true; } // Build result TPlural result = new TPlural(); foreach (TSingular singular in set1) { if (set2Lookup.ContainsKey(singular.Identity)) { result.Add(singular); } } return result; } public new void Remove(TSingular objectToRemove) { for (int index = 0; index < Count; index++) { if (this[index].Identity == objectToRemove.Identity) { RemoveAt(index); index--; } } } } }