public List <OperatorAsset> MoreEfficientAlgorithm() { // Build an OperatorID -> List<Assignments> dictionary var opToAssignments = new Dictionary <int, List <Assignment> >(); foreach (Assignment assignment in _assignments) { if (!opToAssignments.ContainsKey(assignment.OperatorId)) { opToAssignments.Add(assignment.OperatorId, new List <Assignment>()); } opToAssignments[assignment.OperatorId].Add(assignment); } // Build the return list of operators var retList = new List <OperatorAsset>(); foreach (Operator @operator in _operators) { var operatorAsset = new OperatorAsset(@operator); if (opToAssignments.ContainsKey(@operator.OperatorId)) { operatorAsset.Assignments = opToAssignments[@operator.OperatorId]; } retList.Add(operatorAsset); } return(retList); }
/// <summary> /// Hiding the horrific run time with LINQ /// </summary> /// <returns></returns> public List <OperatorAsset> IneffiecientLinqAlgorithm() { var retList = new List <OperatorAsset>(); foreach (Operator @operator in _operators) { var operatorAsset = new OperatorAsset(@operator); var assignments = _assignments.Where(a => a.OperatorId == @operator.OperatorId); operatorAsset.Assignments = assignments.ToList(); retList.Add(operatorAsset); } return(retList); }
/// <summary> /// Inputs: /// 1. _operators (list of operators), call it n /// 2. _assignments (list of assignments), call it m /// Outputs: /// Return back a list of OperatorAssets. Each OperatorAsset /// contains an Operator and his assignments for the day. /// </summary> public List <OperatorAsset> InefficientAlgorithm() { var retList = new List <OperatorAsset>(); // What is the run time of this algorithm?? foreach (Operator @operator in _operators) { var operatorAsset = new OperatorAsset(@operator); foreach (Assignment assignment in _assignments) { if (assignment.OperatorId == @operator.OperatorId) { operatorAsset.Assignments.Add(assignment); } } retList.Add(operatorAsset); } return(retList); }