Example #1
0
        internal void TransferSuccessorsToRunOn(ITile selected)
        {
            // Find the selected tile.
            var index = 0;

            while (!ReferenceEquals(selected, _headItems[index]))
            {
                index++;
            }
            Debug.Assert(index <= _selectedIndex);

            var predecessor = _headItems[index];

            if (index != _selectedIndex)
            {
                var subContext = new List <int>(Context);
                subContext.RemoveRange(_selectedIndex + 1, subContext.Count - _selectedIndex - 1);
                for (var i = _selectedIndex; index < i; i--)
                {
                    AddSequenceTail(subContext, LiveSequenceWeight);
                    subContext.RemoveAt(i);
                }
            }

            var ghostIndex = index + 1;
            var ghostLimit = _headItems.Count;

            if (_headItems[ghostLimit - 1] is GhostStopItem)
            {
                ghostLimit--;
            }
            for (; ghostIndex < ghostLimit; ghostIndex++)
            {
                var oldItem  = _headItems[ghostIndex];
                var encoding = oldItem.Content;
                var newItem  = new GhostWordItem(predecessor, this, encoding);
                _headItems[ghostIndex] = newItem;
                predecessor            = newItem;
            }

            SetSelectedIndex(index);

            if (ghostLimit < _headItems.Count)
            {
                Debug.Assert(ghostLimit + 1 == _headItems.Count);

                var ghostStop = new GhostStopItem(predecessor, this);
                _headItems[ghostLimit] = ghostStop;
            }

            SetSuggestionsView();

            ParanoidAssertValid();
        }
Example #2
0
        internal void AddWords(IEnumerable <string> words)
        {
            var newWords = false;

            ParanoidAssertValid();

            foreach (var word in words)
            {
                if (_tokens.IsNewWord(word))
                {
                    newWords = true;
                    _spellingSource.AddNewWord(word);
                }

                var item = CreateHeadWordItem(word);
                _headItems.Insert(_selectedIndex + 1, item);
                SetSelectedIndex(_selectedIndex + 1);

                AddSequenceTail(Context, PersistedSequenceWeight);
            }

            if (newWords)
            {
                PopulateVocabularyList();
            }

            var ghostLimit = _headItems.Count;

            if (_headItems[ghostLimit - 1] is GhostStopItem)
            {
                ghostLimit--;
            }

            var predecessor = _headItems[_selectedIndex];

            for (var i = _selectedIndex + 1; i < ghostLimit; i++)
            {
                var oldGhost = _headItems[i].Content;
                var newGhost = new GhostWordItem(predecessor, this, oldGhost);
                _headItems[i] = newGhost;
                predecessor   = newGhost;
            }

            if (ghostLimit != _headItems.Count)
            {
                _headItems[ghostLimit] = new GhostStopItem(predecessor, this);
            }

            ParanoidAssertValid();
        }
Example #3
0
        internal void ShowTestCard()
        {
            // Truncate head items.
            for (var i = _headItems.Count - 1; 0 < i; i--)
            {
                _headItems.RemoveAt(i);
            }

            var headWordItem = new HeadWordItem(_headItems[0], this, "Word");

            _headItems.Add(headWordItem);

            var ghostWordItem = new GhostWordItem(_headItems[1], this, "Ghost");

            _headItems.Add(ghostWordItem);

            var ghostStopItem = new GhostStopItem(_headItems[2], this);

            _headItems.Add(ghostStopItem);
        }
Example #4
0
        internal void ContinueRunOnSuggestions()
        {
            var context = new List <int>
            {
                0
            };
            ITile predecessor = _headItems[0];

            for (var i = 1; i <= _selectedIndex; i++)
            {
                var word = (WordItem)_headItems[i];
                Debug.Assert(predecessor == word.Predecessor);
                predecessor = word;
                var token = _tokens.GetToken(word.Content);
                context.Add(token);
            }

            var count = _headItems.Count;

            if (_headItems[count - 1] is GhostStopItem)
            {
                count--;
                _headItems.RemoveAt(count);
            }


            for (var i = _selectedIndex + 1; i < count; i++)
            {
                var word  = (GhostWordItem)_headItems[i];
                var token = _tokens.GetToken(word.Content);
                context.Add(token);

                word          = new GhostWordItem(predecessor, this, word.Content);
                _headItems[i] = word;
                predecessor   = word;
            }

            var more = true;

            while (more && _headItems.Count - _selectedIndex - 1 < MaxRunOnSuggestionsCount)
            {
                var token = GetTopToken(context.ToArray());
                if (0 < token)
                {
                    var word = _tokens.GetString(token);
                    var item = CreateGhostWordItem(predecessor, word);
                    _headItems.Add(item);

                    context.Add(token);

                    predecessor = item;
                }
                else
                {
                    if (token == 0)
                    {
                        var item = new GhostStopItem(predecessor, this);
                        _headItems.Add(item);
                    }
                    more = false;
                }
            }

            {
                // TODO: Old code seemed to just remove terminal GhostStopItem if it was present! Was that necessary?
                // was _runOnSuggestions.RemoveAt(_runOnSuggestions.Count - 1);
                // could be: _headItems.RemoveAt(headItems.Count - 1);
            }

            ParanoidAssertValid();
        }
Example #5
0
        private GhostWordItem CreateGhostWordItem(ITile predecessor, string word)
        {
            var item = new GhostWordItem(predecessor, this, word);

            return(item);
        }