/// <summary> /// Returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. /// </summary> /// <param name="name">The name of a cell</param> /// <returns>A list with named cells and its direct or indirect dependents</returns> protected override IEnumerable <string> GetDirectDependents(string name) { if (name is null) { throw new ArgumentNullException(); } exceptionHelper(name); IEnumerable <string> directDependentsWitoutName = dependencyGraph.GetDependents(name); return(directDependentsWitoutName); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> protected override IEnumerable <string> GetDirectDependents(string name) { if (name == null) { throw new ArgumentNullException(); } if (!IsValidName(name)) { throw new InvalidNameException(); } return(dependencyGraph.GetDependents(name)); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> protected override IEnumerable <string> GetDirectDependents(string name) { if (name == null) { throw new ArgumentNullException(); } if (!Regex.IsMatch(name, @"^[a-zA-Z_](?: [a-zA-Z_]|\d)*")) { throw new InvalidNameException(); } return(dependency_graph.GetDependents(name)); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> protected override IEnumerable <string> GetDirectDependents(string name) { if (name == null) { throw new ArgumentNullException(); } name = name.ToUpper(); if (isInvalid(name.ToUpper())) { throw new InvalidNameException(); } return(dp.GetDependents(name)); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> protected override IEnumerable <string> GetDirectDependents(string name) { if (ReferenceEquals(name, null)) { throw new ArgumentNullException(); } if (!variablechecker(name)) { throw new InvalidNameException(); } return(graph.GetDependents(name)); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> /// <param name="name"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="InvalidNameException"></exception> protected override IEnumerable <string> GetDirectDependents(string name) { if (name == null) { throw new ArgumentNullException(name); } if (isValidName(Normalize(name))) { return(cellGraph.GetDependents(Normalize(name))); } throw new InvalidNameException(); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> protected override IEnumerable <string> GetDirectDependents(string name) { if (name == null) { throw new ArgumentNullException(); } if (!ValidVariable(name)) { throw new InvalidNameException(); } //dependency graph's get dependents enumerates all unique dependents //and returns an empty list if the cell does not have dependents return(dependencyGraph.GetDependents(Normalize(name))); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> protected override IEnumerable <String> GetDirectDependents(String name) { if (ReferenceEquals(name, null)) { throw new ArgumentNullException(); } if (!(IsValidName(name))) { throw new InvalidNameException(); } // GetDependents returns a HashSet ensuring there won't be duplicates return(dg.GetDependents(name)); // changed this from GetDependees to GetDependents and fixed most of my tests }
protected override IEnumerable <String> GetDirectDependents(String name) { if (name.Equals(null)) { throw new ArgumentNullException(); } else if (!IsValidName(name)) { throw new InvalidNameException(); } else { return(graph.GetDependents(Normalize(name))); } }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// </summary> protected override IEnumerable <string> GetDirectDependents(string name) { if (name == null)// Check if name is null. { throw new ArgumentNullException(); } else if (!IsValid(name.ToUpper()))// Check if name is invalid. { throw new InvalidNameException(); } else // Return the dependents of the named cell. { return(_dependencies.GetDependents(name.ToUpper())); } }
protected override IEnumerable <string> GetDirectDependents(string name) { if (name == null) { throw new ArgumentNullException("name"); } if (!IsValidCellName(name)) { throw new InvalidNameException(); } name = Normalize(name); return(myCellDependencyGraph.GetDependents(name)); }
/// <inheritdoc/> /// <param name="name">The name of the cell that is the dependee of all cells returned.</param> /// <returns>Enumeration of direct dependents of given cell</returns> protected override IEnumerable <string> GetDirectDependents(string name) { //If the cell does not have dependents, do not return anything if (!dg.HasDependents(name)) { yield break; } //Otherwise, enumerate each dependent else { foreach (String n in dg.GetDependents(name)) { yield return(n); } } }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> protected override IEnumerable <string> GetDirectDependents(string name) { CheckParameters(name); HashSet <string> list = new HashSet <string>(); // Make sure the named cell has any dependent before adding it to the list // Otherwise, return an empty list. if (graph.HasDependents(name)) { foreach (string dependent in graph.GetDependents(name)) { list.Add(dependent); } } return(list); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> protected override IEnumerable <String> GetDirectDependents(String name) { //check for null if (name == null) { throw new ArgumentNullException(); } //check for valid cell name if (!Regex.IsMatch(name, @"^[a-zA-Z]+[0-9]+$")) { throw new InvalidNameException(); } //return its dependents return(new HashSet <string>(depGraph.GetDependents(name))); }
/// <summary> /// A recursive helper method for GetAllDepeGetAllDependentsndees, to obtain all direct and /// indirect cells, on which named cells value depends on. /// </summary> /// <param name="name">Current cell</param> /// <param name="AllDependees">Set of cells on which we depend, and this method /// will add more to if found</param> /// <returns>An empty set.</returns> private ISet <string> GetDependentsRecursive(string name, HashSet <string> AllDependents) { if (!Graph.HasDependents(name)) { return(new HashSet <string>()); } else { HashSet <string> CurrentDependents = Graph.GetDependents(name).ToHashSet <string>(); AllDependents.UnionWith(CurrentDependents); foreach (string x in CurrentDependents) { GetDependentsRecursive(x, AllDependents); } } return(new HashSet <string>()); }
/// <summary> /// If the formula parameter is null, throws an ArgumentNullException. /// /// Otherwise, if name is null or invalid, throws an InvalidNameException. /// /// Otherwise, if changing the contents of the named cell to be the formula would cause a /// circular dependency, throws a CircularException. (No change is made to the spreadsheet.) /// /// Otherwise, the contents of the named cell becomes formula. The method returns a /// Set consisting of name plus the names of all other cells whose value depends, /// directly or indirectly, on the named cell. /// /// For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the /// set {A1, B1, C1} is returned. /// </summary> protected override ISet <string> SetCellContents(string name, Formula formula) { // if changing the contents of the named cell to be the formula would cause a // circular dependency, throws a CircularException. (No change is made to the spreadsheet.) IEnumerable <string> dents = myDG.GetDependents(name); myDG.ReplaceDependents(name, new HashSet <string>()); foreach (string var in formula.GetVariables()) { myDG.AddDependency(name, var); } // if the ss has had the cell name, replace if (mySS.ContainsKey(name)) { mySS[name].Contents = formula; } // otherwise, add this name,cell key value pair to ss if (!mySS.ContainsKey(name)) { Cell theCell = new Cell(name, formula, my_Lookup); mySS.Add(name, theCell); } // iterate through the ss by GetCellsToRecalculate and make sure there is no // CircularException foreach (string s in GetCellsToRecalculate(name)) { mySS[s].Contents = mySS[s].Contents; } // the ss has been changed, set changed to true. Changed = true; // iterate through the ss by GetCellsToRecalculate and make sure there is no CircularException, // and The names are enumerated in the order in which the calculations should be done. // create a new set to equal to the ordered set HashSet <string> set = new HashSet <string>(GetCellsToRecalculate(name)); // add the name to the set and return set.Add(name); return(set); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 protected override IEnumerable <string> GetDirectDependents(string cell_name) { string name = Normalize(cell_name); if (name == null) { throw new ArgumentNullException(); } if (IsValid(name) == false) { throw new InvalidNameException(); } ISet <string> depends = new HashSet <string> { }; foreach (string item in dependency_graph.GetDependents(name)) { depends.Add(item); } return(depends); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> protected override IEnumerable <string> GetDirectDependents(string name) { if (name == null) { throw new ArgumentNullException(); } bool valid = ValidCellCheck(name); List <string> set = new List <string>(); if (!valid) //If the name of the cell is null or invalid { throw new InvalidNameException(); } //TRYING THIS set = new List <string>(dependencies.GetDependents(name)); /* foreach (KeyValuePair<string, object[]> p in allCells.GetSheet())//Look at all of the cells we have. * { * object o = GetCellContents(p.Key); * if (o is Formula) * { * Formula contents = (Formula) o; * * //If any of the variables of the formula we're looking at contain the name of the cell. * if (contents.GetVariables().Contains(name)) * { * set.Add(p.Key); * } * } * * } * */ return(set); }
/// <summary> /// way of getting the direct dependents of a given cell. Helper method for getCellsToRecalculate /// </summary> /// <param name="name">Cell name</param> /// <returns>Direct Dependents</returns> protected override IEnumerable <string> GetDirectDependents(string name) { if (name == null) { throw new ArgumentNullException(); } if (!couldBeAVariable(name)) { throw new InvalidNameException(); } HashSet <string> dependents = new HashSet <string>(); //to hold all the direct dependents if (dg.HasDependents(name)) { foreach (string el in dg.GetDependents(name)) { dependents.Add(el); } } return(dependents); }
/// <summary> /// Retrieves direct dependents of the cell /// </summary> /// <param name="name">Name of the cell to retrieve dependents from</param> /// <returns>Enumerable collection of all the direct dependents</returns> protected override IEnumerable <string> GetDirectDependents(string name) { return(cellDependency.GetDependents(name)); }
/// <summary> /// If name is null, throws an ArgumentNullException. /// /// Otherwise, if name isn't a valid cell name, throws an InvalidNameException. /// /// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose /// values depend directly on the value of the named cell. In other words, returns /// an enumeration, without duplicates, of the names of all cells that contain /// formulas containing name. /// /// For example, suppose that /// A1 contains 3 /// B1 contains the formula A1 * A1 /// C1 contains the formula B1 + A1 /// D1 contains the formula B1 - C1 /// The direct dependents of A1 are B1 and C1 /// </summary> protected override IEnumerable <string> GetDirectDependents(string name) { // Error checking has already been done. return(graph.GetDependents(name.ToUpper())); }
/// <summary> /// Requires that all of the variables in formula are valid cell names. /// /// If name is null or invalid, throws an InvalidNameException. /// /// Otherwise, if changing the contents of the named cell to be the formula would cause a /// circular dependency, throws a CircularException. /// /// Otherwise, the contents of the named cell becomes formula. The method returns a /// Set consisting of name plus the names of all other cells whose value depends, /// directly or indirectly, on the named cell. /// /// For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the /// set {A1, B1, C1} is returned. /// </summary> protected override ISet <String> SetCellContents(String name, Formula formula) { foreach (string s in formula.GetVariables()) { if (!IsValidCell(s)) { throw new InvalidNameException(); } } if (name == null || !IsValidCell(name)) { throw new InvalidNameException(); } DependencyGraph backup = new DependencyGraph(dependencies); dependencies.ReplaceDependees(name, formula.GetVariables()); try { GetCellsToRecalculate(name); } catch (CircularException e) { dependencies = new DependencyGraph(backup); throw e; } bool undefined = false; string reason = ""; foreach (string s in formula.GetVariables()) { if (!GetNamesOfAllNonemptyCells().Contains(s) || !(GetCellValue(s).GetType() == typeof(double))) { undefined = true; reason = s + " is undefined."; } } Cell cell; if (undefined == true) { cell = new Cell { Contents = formula, Value = new FormulaError(reason) }; } else { bool error = false; cell = new Cell { Contents = formula, }; try { formula.Evaluate(s => Convert.ToDouble(GetCellValue(s))); } catch { error = true; cell = new Cell { Contents = formula, Value = new FormulaError("Formula Evaluation Error.") }; } if (!error) { cell.Value = formula.Evaluate(s => Convert.ToDouble(GetCellValue(s))); } } if (cells.ContainsKey(name)) { cells[name] = cell; } else { cells.Add(name, cell); } MethodInfo method = typeof(Formula).GetMethod("Evaluate"); Lookup o = new Lookup(n => Convert.ToDouble(GetCellValue(n))); bool zero = false; foreach (string s in GetCellsToRecalculate(name)) { try { method.Invoke(cells[s].Contents, new object[] { o }); } catch { zero = true; } if (zero == true) { cell = new Cell { Contents = cells[s].Contents, Value = new FormulaError("Undefined.") }; } else { cell = new Cell { Contents = cells[s].Contents, Value = method.Invoke(cells[s].Contents, new object[] { o }) }; } cells[s] = cell; } HashSet <string> names = new HashSet <string> { name }; HashSet <string> temp; int counter = 0; while (counter != names.Count) { counter = names.Count; temp = new HashSet <string>(); foreach (string s in names) { foreach (string t in dependencies.GetDependents(s)) { temp.Add(t); } } foreach (string x in temp) { names.Add(x); } } Changed = true; return(names); }
/// <summary> /// If name is null or invalid, throws an InvalidNameException. /// /// Otherwise, the contents of the named cell becomes number. The method returns a /// set consisting of name plus the names of all other cells whose value depends, /// directly or indirectly, on the named cell. /// /// For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the /// set {A1, B1, C1} is returned. /// </summary> protected override ISet <String> SetCellContents(String name, double number) { if (name == null || !IsValidCell(name)) { throw new InvalidNameException(); } if (cells.ContainsKey(name)) { Cell cell = new Cell { Contents = number, Value = number }; cells[name] = cell; } else { Cell cell = new Cell { Contents = number, Value = number }; cells.Add(name, cell); } MethodInfo method = typeof(Formula).GetMethod("Evaluate"); Lookup o = new Lookup(n => Convert.ToDouble(GetCellValue(n))); bool zero = false; foreach (string s in GetCellsToRecalculate(name)) { if (cells[s].Contents.GetType() == typeof(Formula)) { try { method.Invoke(cells[s].Contents, new object[] { o }); } catch { zero = true; } Cell cell; if (zero == true) { cell = new Cell { Contents = cells[s].Contents, Value = new FormulaError("Undefined.") }; } else { cell = new Cell { Contents = cells[s].Contents, Value = method.Invoke(cells[s].Contents, new object[] { o }) }; } cells[s] = cell; } } HashSet <string> names = new HashSet <string> { name }; HashSet <string> temp; int counter = 0; while (counter != names.Count) { counter = names.Count; temp = new HashSet <string>(); foreach (string s in names) { foreach (string t in dependencies.GetDependents(s)) { temp.Add(t); } } foreach (string x in temp) { names.Add(x); } } Changed = true; return(names); }
protected override ISet <String> SetCellContents(String name, Formula formula) { //EXCEPTION HANDELING\\ if (System.String.IsNullOrWhiteSpace(name)) { throw new InvalidNameException(); } if (!isValid(name)) { throw new InvalidNameException(); } if (formula == null) { throw new ArgumentNullException(); } this.temp = new HashSet <string>(); foreach (string s in formula.GetVariables()) //GET ALL THE VARIABLES FROM THE FORMULA AND ADD THEM TO A SET { if (!isValid(s)) { throw new FormulaFormatException("One or more variables names do not follow the proper syntax expected"); } this.temp.Add(s); } foreach (string s in this.temp) //TRACK DEPENDENCIES { if (ssDep.HasDependents(s)) { foreach (string ss in ssDep.GetDependents(s)) { if (ss == s || ss == name) { throw new CircularException(); } } } ssDep.AddDependency(name, s); } Cell v; if (Cells.TryGetValue(name, out v)) //Get the right cell { if (v.Content is Formula) { Formula check = (Formula)v.Content; foreach (string s in check.GetVariables()) { ssDep.RemoveDependency(name, s); //Remove old dependencies } } } ISet <string> set = Set(name, formula); foreach (string s in set) //Recalculate Cells that depend on new cells content { calculateV(s); } return(set); }