using System; using System.Collections; using System.Collections.Generic; namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration { public class StoredSymbolString { public readonly int Hash; public readonly SymbolString SymbolString; public StoredSymbolString(int hash, SymbolString symbolString) { Hash = hash; SymbolString = symbolString; } } public enum StorageType { Stack, Queue, RandomList } class SearchDataStore : IEnumerable { private Dictionary storedValues; // Store hash-references and associated, actual values private Action storeInternal; // Store hash-references private Func fetchInternal; // Fetch hash-reference public SearchDataStore(StorageType storageType) { storedValues = new Dictionary(); switch (storageType) { case StorageType.Stack: InitStack(); break; case StorageType.Queue: InitQueue(); break; case StorageType.RandomList: InitRandomList(); break; } } #region SearchStrategies private void InitStack() { Stack stack = new Stack(); storeInternal = hash => stack.Push(hash); fetchInternal = () => stack.Pop(); } private void InitQueue() { Queue queue = new Queue(); storeInternal = hash => queue.Enqueue(hash); fetchInternal = () => queue.Dequeue(); } private void InitRandomList() { List list = new List(); System.Random rand = new System.Random(999); storeInternal = hash => list.Add(hash); fetchInternal = () => { int indexOfHash = rand.Next(list.Count); int result = list[indexOfHash]; list.RemoveAt(indexOfHash); // TODO: beware this is O(n), at some point in time we should fix this return result; }; } #endregion #region Interface public StoredSymbolString GetNext() { int hash = fetchInternal.Invoke(); SymbolString result = storedValues[hash]; storedValues.Remove(hash); return new StoredSymbolString(hash, result); } public void Store(int hash, SymbolString s) { storeInternal(hash); storedValues[hash] = s; } public bool Contains(int hash) { return storedValues.ContainsKey(hash); } #endregion #region Collection-Interface public int Count { get { return storedValues.Count; } } public IEnumerator GetEnumerator() { return storedValues.Values.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion } }