public static IEnumerable <interval> _dfs(interval interval, interval blocked = null) { var stack = new Stack <Tuple <interval, int> >(); stack.Push(new Tuple <interval, int>(interval, 0)); while (stack.Count > 0) { var si = stack.Pop(); if (si.Item1 != blocked) { if (si.Item2 < si.Item1.children.Count) { stack.Push(new Tuple <interval, int>(si.Item1, si.Item2 + 1)); stack.Push(new Tuple <interval, int>(si.Item1.children[si.Item2], 0)); } else { yield return(si.Item1); } } } yield break; }
public interval(int length, int left, interval interval) { this.length = length; this.left = left; right = -1; children = new List <interval>(); if (interval != null) { children.Add(interval); } }
private bool isrle(int i) { bool output = false; if (i > 1) { interval lcpinterval = litarray[isa[i]]; output = i > 1 && isa[i] >= lcpinterval.left && isa[i - 1] >= lcpinterval.left && isa[i] <= lcpinterval.right && isa[i - 1] <= lcpinterval.right; } return(output); }
public interval(interval interval, int right) { length = interval.length; left = interval.left; this.right = right; children = interval.children; foreach (interval cinterval in children) { cinterval.parent = this; } }
static public interval[] _litarray(interval lit) { var output = new interval[lit.right + 1]; foreach (interval interval in _dfs(lit)) { foreach (int n in interval.indexes()) { output[n] = interval; } } return(output); }
public esa(byte[] input) { this.input = input; sa = SuffixArray.SAIS.sa(input); isa = _isa(sa); lcp = _lcp(input, sa, isa); l1o = _l1o(input); lit = _lit(lcp); litarray = _litarray(lit); spsv = _psv(sa); snsv = _nsv(sa); splv = _plv(sa); snlv = _nlv(sa); }
//lcp interval tree static private interval _lit(int[] lcp) { interval interval = null; var stack = new Stack <interval>(); stack.Push(new interval(0, 0, null)); for (int i = 1; i < lcp.Length; i++) { int left = i - 1; while (lcp[i] < stack.Peek().length) { interval = new interval(stack.Pop(), i - 1); //process left = interval.left; if (lcp[i] <= stack.Peek().length) { stack.Peek().add(interval); interval = null; } } if (lcp[i] > stack.Peek().length) { stack.Push(new interval(lcp[i], left, interval)); interval = null; } } while (stack.Count > 1) { interval = stack.Pop(); interval = new interval(interval, lcp.Length - 1); //process if (interval.length > stack.Peek().length) { stack.Peek().add(interval); } } interval = new interval(stack.Pop(), lcp.Length - 1); return(interval); }
public void add(interval interval) { interval.parent = this; children.Add(interval); }
public IEnumerable <Tuple <bool, int, int> > tedges(int i) { interval lcpinterval = litarray[isa[i]]; int length = lcpinterval.length; int slp = spsv[isa[i]]; int srp = snsv[isa[i]]; int minoffset = int.MaxValue; bool this_rle = isrle(i); bool former_rle = isrle(i - 1); bool yieldminimaloffsetedges = true; while (lcpinterval.length > 1) { while (slp > -1 && slp >= lcpinterval.left) { int offset = i - sa[slp]; minoffset = Math.Min(minoffset, offset); int src = i - offset; bool leftmaximal = src == 0 || input[i - 1] != input[src - 1]; if (leftmaximal) { yield return(new Tuple <bool, int, int>(leftmaximal, lcpinterval.length, offset)); } if (splv[slp] > -1 && sa[splv[slp]] > i && (slp > 0 && spsv[slp] != -1 && splv[spsv[slp]] == spsv[slp])) { slp = spsv[slp]; } else if (spsv[slp] < 0 || slp - splv[slp] < slp - spsv[slp]) { slp = splv[slp]; while (slp > -1 && sa[slp] > i) { slp = spsv[slp]; } } else { slp = spsv[slp]; } } while (srp > -1 && srp <= lcpinterval.right) { int offset = i - sa[srp]; minoffset = Math.Min(minoffset, offset); int src = i - offset; bool leftmaximal = src == 0 || input[i - 1] != input[src - 1]; if (leftmaximal) { yield return(new Tuple <bool, int, int>(leftmaximal, lcpinterval.length, offset)); } if (snlv[srp] > -1 && sa[snlv[srp]] > i && (srp > 0 && snsv[srp] != -1 && snlv[snsv[srp]] == snsv[srp])) { srp = snsv[srp]; } else if (snsv[srp] < 0 || snlv[srp] - srp < snsv[srp] - srp) { srp = snlv[srp]; while (srp > -1 && srp < sa.Length && sa[srp] > i) { srp = snsv[srp]; } } else { srp = snsv[srp]; } } lcpinterval = lcpinterval.parent; if (minoffset < int.MaxValue) { while (length > lcpinterval.length && yieldminimaloffsetedges) { yield return(new Tuple <bool, int, int>(false, length, minoffset)); length--; yieldminimaloffsetedges = !(this_rle && former_rle); } } else { length = lcpinterval.length; } if (length > constant.threshold_greedy_length) { break; } } if (minoffset < int.MaxValue) { while (length > 1 && yieldminimaloffsetedges) { yield return(new Tuple <bool, int, int>(false, length, minoffset)); length--; if (length > constant.threshold_greedy_length) { break; } } } yield return(new Tuple <bool, int, int>(false, 1, l1o[i])); yield break; }