예제 #1
0
        /// <summary>
        /// If text is null, throws an ArgumentNullException.
        ///
        /// Otherwise, if name is null or invalid, throws an InvalidNameException.
        ///
        /// Otherwise, the contents of the named cell becomes text.  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, string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException();
            }

            //if its a valid name create a cell add it to a dictionary and adds it name to the set
            isValidName(name);
            cell          cell = new cell();
            ISet <string> set  = new HashSet <string>();

            if (!dictionary.ContainsKey(name))
            {
                cell.setContent(text);
                dictionary.Add(name, cell);
                set.Add(name);
                //gets any of the cells that depend on this one and recalculates
                RecursivlyGetDependents(name, set);
                GetCellsToRecalculate(set);
                return(set);
            }
            //if cell already exists removes it from the dictionary, and removes its dependencies from the graph
            else if (dictionary.ContainsKey(name))
            {
                dictionary.TryGetValue(name, out cell);
                bool isFormula = cell.getContent().GetType() == typeof(Formula);
                if (isFormula)
                {
                    Formula f = (Formula)cell.getContent();
                    foreach (string dependee in f.GetVariables())
                    {
                        graph.RemoveDependency(dependee, name);
                    }
                }
                set.Add(name);
                //gets any of the cells that depend on this one and recalculates
                RecursivlyGetDependents(name, set);
                GetCellsToRecalculate(set);
                dictionary.Remove(name);
                cell.setContent(text);
                dictionary.Add(name, cell);
                return(set);
            }
            return(set);
        }
예제 #2
0
        /// <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 (formula == null)
            {
                throw new ArgumentNullException();
            }
            //checks if its a valid name
            isValidName(name);
            name = Normalize(name);
            cell          cell = new cell();
            ISet <string> set  = new HashSet <string>();

            //if the cell isnt already created for that name create it.
            if (!dictionary.ContainsKey(name))
            {
                //create cell and add the formula to it
                cell.setContent(formula);
                //add the cell to the dictionary the name is the key.
                dictionary.Add(name, cell);
                set.Add(name);

                //sets the variables in the formula as a dependee of the new cell
                foreach (string dependees in formula.GetVariables())
                {
                    graph.AddDependency(dependees, name);
                }
                //checks for circular dependency and recalculates the cells nesseccary
                GetCellsToRecalculate(set);
                //gets all the dependents of the cell and adds to the set
                RecursivlyGetDependents(name, set);
                //checks for circular dependency and recalculates the cells nesseccary
                GetCellsToRecalculate(set);
                return(set);
            }
            //if the cell is already created remove it from the dictionary
            else if (dictionary.ContainsKey(name))
            {
                //gets the cell out and removes any dependees from the cell
                dictionary.TryGetValue(name, out cell);
                bool isFormula = cell.getContent().GetType() == typeof(Formula);
                if (isFormula)
                {
                    Formula f = (Formula)cell.getContent();
                    foreach (string dependee in f.GetVariables())
                    {
                        graph.RemoveDependency(dependee, name);
                    }
                }
                set.Add(name);
                //addes new dependees to the cell if any
                foreach (string dependee in formula.GetVariables())
                {
                    graph.AddDependency(dependee, name);
                }
                //gets all the dependents of the cell and adds to the set
                GetCellsToRecalculate(set);
                RecursivlyGetDependents(name, set);
                //checks for circular dependency and recalculates the cells nesseccary
                GetCellsToRecalculate(set);
                dictionary.Remove(name);
                //sets the new formula
                cell.setContent(formula);
                //adds the new cell to the dictionary
                dictionary.Add(name, cell);
                return(set);
            }
            return(set);
        }