public WordTrieNode(WordNodeConfig config, WordTrieNode parent, Word wordToAdd, bool isNodeValue) : this(config) { _parent = parent; _word = isNodeValue ? wordToAdd : null; _mostPopularWords.Add(wordToAdd); }
public void AddWordWithNullValueTest() { var root = new WordTrieNode(_config); var word = new Word(); root.Add(word); Assert.IsFalse(root.HasChildNodes); }
public WordTrieNode Add(Word word) { if (!this.IsRoot) return null; if (word == null || string.IsNullOrWhiteSpace(word.Value)) return null; WordTrieNode node = this.Get(word.Value); if (node != null) word = node.UpdateValue(word)._word; return AddChildNode(word); }
public void Add(Word word) { if (IsNotMostPopularWord(word)) return; for (int i = 0; i < _mostPopularWords.Count; i++) if (_mostPopularWords[i].Value.Equals(word.Value, System.StringComparison.OrdinalIgnoreCase)) { _mostPopularWords.Remove(_mostPopularWords[i]); break; } if (TryInsertWord(word)) ApplyMostPopularWordsLimit(); }
private int GetIndexToInsert(Word word) { for (int index = 0; index < _mostPopularWords.Count; index++) { Word popularWord = _mostPopularWords[index]; if (popularWord.Count > word.Count) continue; if (popularWord.Count == word.Count && popularWord.Value.CompareTo(word.Value) == -1) continue; return index; } if (_mostPopularWords.Count != _mostPopularWordsLimit) return _mostPopularWords.Count; return UNKNOWN_INDEX_TO_INSERT; }
public new void Add(Word word) { base.Add(word); }
private bool IsExists(Word word) { Word existingWord = _mostPopularWords.FirstOrDefault(w => w.Value.Equals(word.Value, System.StringComparison.OrdinalIgnoreCase)); return existingWord != null; }
private bool TryInsertWord(Word word) { int indexToInsert = GetIndexToInsert(word); bool isMostPopularWord = indexToInsert != UNKNOWN_INDEX_TO_INSERT; if (isMostPopularWord) _mostPopularWords.Insert(indexToInsert, word); return isMostPopularWord; }
private bool IsNotMostPopularWord(Word word) { return _mostPopularWords.Count == _mostPopularWordsLimit && _mostPopularWords.All(w => w.Count > word.Count); }
private static void AddWord(Word word) { _wordCollection.Add(word); }
private WordTrieNode UpdateValue(Word word) { if (this.HasValue) { _word.Count += word.Count; } else { _word = word; } return this; }
private WordTrieNode UpdateMostPopularWords(Word word) { _mostPopularWords.Add(word); return this; }
private WordTrieNode DelValue(int index, string value) { bool isNodeValue = index == value.Length - 1; if (isNodeValue && !this.HasChildNodes) this._parent._childNodes.Remove(value[index]); else if (isNodeValue) this._word = null; this._mostPopularWords.Del(value); return this._parent; }
private WordTrieNode AddChildNode(char symbol, Word word, bool isNodeValue) { _childNodes.Add(symbol, new WordTrieNode(_config, this, word, isNodeValue)); return _childNodes[symbol]; }
private WordTrieNode AddChildNode(int index, Word word) { char symbol = word.Value[index]; bool isNodeValue = index == word.Value.Length - 1; if (!_childNodes.ContainsKey(symbol)) return AddChildNode(symbol, word, isNodeValue); else return _childNodes[symbol].UpdateMostPopularWords(word); }
private WordTrieNode AddChildNode(Word word) { WordTrieNode node = this; for (int i = 0; i < word.Value.Length; i++) node = node.AddChildNode(i, word); return node; }