public static Arrayx <T> New(int size = 1) // <3 { var x = new Arrayx <T>(); x.Size = size < 1 ? 1 : size; x.Elements = new T[size]; x.Length = 0; return(x); }
public Arrayx <T> Map(Func <T, T> callback) { var result = Arrayx <T> .New(Length); for (int i = 0; i < Length; i++) { result.Add(callback(Elements[i])); } return(result); }
public Arrayx <T> Filter(Func <T, bool> callback, bool first = false) { var result = Arrayx <T> .New(Length); for (int i = 0; i < Length; i++) { if (callback(Elements[i])) { result.Add(Elements[i]); if (first) { break; } } } return(result); }
// Returns the 'components' that belongs to the 'entities' intersected. // Let's assume that the first array on 'entities' is the one related // position by position to the 'components' in the result. public static Arrayx <T> Get <T>(Arrayx <int>[] entities, Arrayx <T> components) { // Just one array means just one source, no interception! if (entities.Length <= 1) { return(components); } // Create an array with enough space to contain the answers var maxSize = 0; for (int i = 0; i < entities.Length; i++) { maxSize += entities[i].Length; } var result = new Arrayx <T>(); // @todo Could I squeeze more performance by caching this stuff? result.Size = result.Size < maxSize ? maxSize : result.Size; result.Elements = new T[result.Size]; result.Length = 0; // The ids should repeat this much to detect being on each array var validCount = entities.Length; // Running over all the ids arrays for (int i = 0; i < entities.Length; i++) { // For each of element of that array for (int j = 0; j < entities[i].Length; j++) { var current = entities[i].Elements[j]; var currentFoundCount = 0; T matchedValue = default; // Let's run over all the ids arrays for (int k = 0; k < entities.Length; k++) { // To compare the elements repetition for (int l = 0; l < entities[k].Length; l++) { if (current == entities[k].Elements[l]) { currentFoundCount++; // Using 'i' because the match should happen only // once, also the ids related to the source are on 0 if (i == 0 && k == 0) { matchedValue = components.Elements[l]; } // We found what we are looking for break; } } } // Is the element repeated in all the arrays? Let's collect the // value if exists if (currentFoundCount >= validCount && matchedValue != null) { result.Elements[result.Length++] = matchedValue; } } } return(result); }