Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/ReadonlySequence.cs @ 11842

Last change on this file since 11842 was 11793, checked in by gkronber, 9 years ago

#2283 fixed compile errors and refactoring

File size: 3.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Text;
6
7namespace HeuristicLab.Problems.GrammaticalOptimization {
8  public class ReadonlySequence : Sequence {
9    private int symbolsOffset = 0; // the sequence does not have to start with the first symbol of the symbols array (when we reuse these arrays)
10
11    // cloning constructor for readonly sequences
12    // does not allocate the symbols array of the base class
13    // instead: reuse the symbols array (both sequences are readonly)
14    private ReadonlySequence(ReadonlySequence original)
15      : base() {
16      base.symbols = original.symbols;
17      this.symbolsOffset = original.symbolsOffset;
18    }
19    public ReadonlySequence(string s)
20      : base(s, s.Length) {
21    }
22
23    public ReadonlySequence(char ch)
24      : base(ch, 1) {
25    }
26
27    public ReadonlySequence(Sequence s)
28      : base(s, s.Length) {
29    }
30
31    public override void ReplaceAt(int position, int len, Sequence replacement) {
32      throw new NotSupportedException();
33    }
34
35    public override char this[int idx] {
36      get {
37        return base[idx + symbolsOffset];
38      }
39      set {
40        throw new NotSupportedException();
41      }
42    }
43
44    public override IEnumerator<char> GetEnumerator() {
45      return symbols.Skip(symbolsOffset).Take(Length).GetEnumerator();
46    }
47
48    public override string ToString() {
49      var sb = new StringBuilder(Length);
50      sb.Append(symbols, symbolsOffset, Length);
51      return sb.ToString();
52    }
53
54    public new ReadonlySequence Subsequence(int startIdx, int len) {
55      if (startIdx < 0 || len < 0) throw new ArgumentException();
56      if (startIdx >= this.Length) throw new ArgumentException();
57      if (startIdx + len > this.Length) throw new ArgumentException();
58      var subsequence = new ReadonlySequence(this) { symbolsOffset = startIdx + this.symbolsOffset, Length = len };
59
60
61      if (FirstNonTerminalIndex < 0) {
62        subsequence.FirstNonTerminalIndex = -1;
63      } else if (FirstNonTerminalIndex < startIdx) {
64        // need to find first nt in subsequence
65        subsequence.FirstNonTerminalIndex = -1;
66        for (int i = 0; subsequence.FirstNonTerminalIndex == -1 && i < len; i++) {
67          if (subsequence[i] >= 'A' && subsequence[i] <= 'Z') subsequence.FirstNonTerminalIndex = i;
68        }
69      } else if (FirstNonTerminalIndex >= startIdx && FirstNonTerminalIndex < startIdx + len) {
70        subsequence.FirstNonTerminalIndex = FirstNonTerminalIndex - startIdx;
71      } else {
72        Debug.Assert(FirstNonTerminalIndex >= startIdx + len);
73        subsequence.FirstNonTerminalIndex = -1;
74      }
75      return subsequence;
76    }
77
78    public override bool Equals(object obj) {
79      var other = obj as ReadonlySequence;
80      if (other == null) return false;
81      if (other.Length != this.Length) return false;
82      for (int i = 0; i < Length; i++)
83        if (other[i] != this[i]) return false;
84
85      // length and all symbols are the same
86      return true;
87    }
88
89    public override int GetHashCode() {
90      int h = 31 * Length;
91      for (int i = 0; i < Length; i++) {
92        h += 31 * (byte)this[i];
93      }
94      return h;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.