using System; using System.Collections.Concurrent; using System.Collections.Generic; namespace SearchBox { public class InvertedIndex { private ConcurrentDictionary>> invertedIndex = new ConcurrentDictionary>>(); public InvertedIndex() { } public bool AddIndex(int pageId, Index newIndex) { foreach (KeyValuePair> token in newIndex) { if (!invertedIndex.ContainsKey(token.Key) && !invertedIndex.TryAdd(token.Key, new ConcurrentDictionary>())) return false; if (!invertedIndex[token.Key].TryAdd(pageId, token.Value)) return false; } return true; } public bool RemoveIndex(int pageId, Index newIndex) { foreach (string token in newIndex.Tokens()) { if (!invertedIndex.ContainsKey(token) || !invertedIndex[token].ContainsKey(pageId)) continue; if (!invertedIndex[token].TryRemove(pageId, out List noop)) return false; } return false; } } }