1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration {
|
---|
6 |
|
---|
7 | public class StoredSymbolString {
|
---|
8 | public readonly int Hash;
|
---|
9 | public readonly SymbolString SymbolString;
|
---|
10 |
|
---|
11 | public StoredSymbolString(int hash, SymbolString symbolString) {
|
---|
12 | Hash = hash;
|
---|
13 | SymbolString = symbolString;
|
---|
14 | }
|
---|
15 | }
|
---|
16 |
|
---|
17 | public enum StorageType {
|
---|
18 | Stack, Queue, RandomList
|
---|
19 | }
|
---|
20 |
|
---|
21 | class SearchDataStore : IEnumerable<SymbolString> {
|
---|
22 |
|
---|
23 | private Dictionary<int, SymbolString> storedValues; // Store hash-references and associated, actual values
|
---|
24 | private Action<int> storeInternal; // Store hash-references
|
---|
25 | private Func<int> fetchInternal; // Fetch hash-reference
|
---|
26 |
|
---|
27 | public SearchDataStore(StorageType storageType) {
|
---|
28 | storedValues = new Dictionary<int, SymbolString>();
|
---|
29 |
|
---|
30 | switch (storageType) {
|
---|
31 | case StorageType.Stack:
|
---|
32 | InitStack();
|
---|
33 | break;
|
---|
34 | case StorageType.Queue:
|
---|
35 | InitQueue();
|
---|
36 | break;
|
---|
37 | case StorageType.RandomList:
|
---|
38 | InitRandomList();
|
---|
39 | break;
|
---|
40 | }
|
---|
41 |
|
---|
42 | }
|
---|
43 |
|
---|
44 | #region SearchStrategies
|
---|
45 |
|
---|
46 | private void InitStack() {
|
---|
47 | Stack<int> stack = new Stack<int>();
|
---|
48 |
|
---|
49 | storeInternal = hash => stack.Push(hash);
|
---|
50 | fetchInternal = () => stack.Pop();
|
---|
51 | }
|
---|
52 |
|
---|
53 | private void InitQueue() {
|
---|
54 | Queue<int> queue = new Queue<int>();
|
---|
55 |
|
---|
56 | storeInternal = hash => queue.Enqueue(hash);
|
---|
57 | fetchInternal = () => queue.Dequeue();
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void InitRandomList() {
|
---|
61 | List<int> list = new List<int>();
|
---|
62 | System.Random rand = new System.Random(999);
|
---|
63 |
|
---|
64 | storeInternal = hash => list.Add(hash);
|
---|
65 | fetchInternal = () => {
|
---|
66 | int indexOfHash = rand.Next(list.Count);
|
---|
67 | int result = list[indexOfHash];
|
---|
68 | list.RemoveAt(indexOfHash); // TODO: beware this is O(n), at some point in time we should fix this
|
---|
69 | return result;
|
---|
70 | };
|
---|
71 | }
|
---|
72 |
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | #region Interface
|
---|
76 |
|
---|
77 | public StoredSymbolString GetNext() {
|
---|
78 | int hash = fetchInternal.Invoke();
|
---|
79 | SymbolString result = storedValues[hash];
|
---|
80 | storedValues.Remove(hash);
|
---|
81 | return new StoredSymbolString(hash, result);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void Store(int hash, SymbolString s) {
|
---|
85 | storeInternal(hash);
|
---|
86 | storedValues[hash] = s;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public bool Contains(int hash) {
|
---|
90 | return storedValues.ContainsKey(hash);
|
---|
91 | }
|
---|
92 |
|
---|
93 | #endregion
|
---|
94 |
|
---|
95 | #region Collection-Interface
|
---|
96 |
|
---|
97 | public int Count {
|
---|
98 | get { return storedValues.Count; }
|
---|
99 | }
|
---|
100 |
|
---|
101 | public IEnumerator<SymbolString> GetEnumerator() {
|
---|
102 | return storedValues.Values.GetEnumerator();
|
---|
103 | }
|
---|
104 |
|
---|
105 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
106 | return GetEnumerator();
|
---|
107 | }
|
---|
108 |
|
---|
109 | #endregion
|
---|
110 | }
|
---|
111 | }
|
---|