Beispiel #1
0
        /// <summary>
        ///     If word already exists anywhere in map, just increase its occurrence.
        ///     Otherwise inserts word into word map as the child of the node we're currently working on.
        /// </summary>
        /// <param name="word">Word to insert</param>
        private void InsertWord(string word)
        {
            _previouslyWorkingOn = _currentlyWorkingOn;

            // If map doesn't already contain word, add it
            if (AllNodes.Exists(x => x.Word == word) == false)
            {
                _currentlyWorkingOn = new WordMapNode(word, _nextNodeId);
                _nextNodeId++;

                if (_previouslyWorkingOn != null)
                {
                    //Debug.Log($"Inserted {_currentlyWorkingOn.Word}");
                    _previouslyWorkingOn.Next.Add(_currentlyWorkingOn);
                    _currentlyWorkingOn.Previous.Add(_previouslyWorkingOn);
                }
                else
                {
                    StartingNodes.Add(_currentlyWorkingOn);
                }

                AllNodes.Add(_currentlyWorkingOn);
            }
            else
            {
                // If map already contains word, find it and increase its occurrence rate
                _currentlyWorkingOn = AllNodes.Find(x => x.Word == word);

                if (_currentlyWorkingOn == null)
                {
                    return;
                }

                _currentlyWorkingOn.Occurrences++;

                if (_previouslyWorkingOn == null)
                {
                    return;
                }

                //Debug.Assert(_previouslyWorkingOn.Next.Contains(_currentlyWorkingOn) == false, $"{_previouslyWorkingOn.Word} already had {_currentlyWorkingOn.Word} as NEXT");
                //Debug.Assert(_currentlyWorkingOn.Previous.Contains(_previouslyWorkingOn) == false, $"{_currentlyWorkingOn.Word} already had {_previouslyWorkingOn.Word} as PREVIOUS");

                if (_previouslyWorkingOn.Next.Contains(_currentlyWorkingOn) == false)
                {
                    _previouslyWorkingOn.Next.Add(_currentlyWorkingOn);
                }

                if (_currentlyWorkingOn.Previous.Contains(_previouslyWorkingOn) == false)
                {
                    _currentlyWorkingOn.Previous.Add(_previouslyWorkingOn);
                }
            }
        }