Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Sentence.cs @ 16022

Last change on this file since 16022 was 15974, checked in by bburlacu, 6 years ago

#2886: implement LRU cache for storing search nodes, introduce SortedSet for handling priorities, fix serialization and cloning

File size: 2.8 KB
RevLine 
[15712]1using System;
[15827]2using System.Collections;
[15712]3using System.Collections.Generic;
[15827]4using System.Diagnostics;
[15712]5using System.Linq;
[15960]6using HeuristicLab.Common;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[15712]8
9namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration {
[15960]10  [StorableClass]
11  public class SymbolString : DeepCloneable, IEnumerable<Symbol> {
12    [Storable]
[15827]13    private readonly Symbol[] symbols;
[15712]14
[15827]15    public Symbol this[int index] {
16      get { return symbols[index]; }
17    }
[15723]18
[15827]19    public SymbolString(IEnumerable<Symbol> s) {
20      symbols = s.ToArray();
21    }
[15712]22
[15827]23    public SymbolString(params Symbol[] s) {
24      symbols = s;
25    }
26
[15963]27    [StorableConstructor]
28    protected SymbolString(bool deserializing) { }
29
[15960]30    protected SymbolString(SymbolString original, Cloner cloner) : base(original, cloner) {
[15974]31      symbols = original.symbols.Select(cloner.Clone).ToArray();
[15960]32    }
33
34    public override IDeepCloneable Clone(Cloner cloner) {
35      return new SymbolString(this, cloner);
36    }
37
[15712]38    public bool IsSentence() {
[15827]39      return !this.Any(sym => sym is NonterminalSymbol);
[15712]40    }
41
42    public int[] GetNonterminalSymbolIndexes() {
[15827]43      return Enumerable.Range(0, symbols.Length)
44        .Where(i => symbols[i] is NonterminalSymbol)
[15712]45        .ToArray();
46    }
47
[15827]48    public int NextNonterminalIndex() {
49      Debug.Assert(!IsSentence());
50      int firstNonTerminalIndex = 0;
51      while (firstNonTerminalIndex < symbols.Length && symbols[firstNonTerminalIndex] is TerminalSymbol) {
52        firstNonTerminalIndex++;
53      }
54      return firstNonTerminalIndex;
55    }
56
57    public SymbolString DerivePhrase(int nonterminalSymbolIndex, Production production) {
58      int productionLength = production.Count;
59      int newStringLength = Count() + productionLength - 1;
60      Symbol[] newPhrase = new Symbol[newStringLength];
61
62      // Copy "left" part of the phrase
63      Array.Copy(symbols, 0, newPhrase, 0, nonterminalSymbolIndex);
64      // Copy production and overwrite nonterminalsymbol
65      production.CopyTo(0, newPhrase, nonterminalSymbolIndex, productionLength);
66      // Copy "right" part of the original phrase
67      Array.Copy(symbols, nonterminalSymbolIndex + 1, newPhrase, nonterminalSymbolIndex + productionLength, symbols.Length - nonterminalSymbolIndex - 1);
68
69      return new SymbolString(newPhrase);
70    }
71
[15723]72    public override string ToString() {
[15827]73      return string.Join<Symbol>(" ", symbols);
[15712]74    }
[15827]75
76    #region Enumerable interface
77    IEnumerator IEnumerable.GetEnumerator() {
78      return GetEnumerator();
79    }
80
81    public IEnumerator<Symbol> GetEnumerator() {
82      return ((IEnumerable<Symbol>)symbols).GetEnumerator();
83    }
84
85    public int Count() {
86      return symbols.Length;
87    }
88    #endregion
[15712]89  }
90}
Note: See TracBrowser for help on using the repository browser.