On Tuesday, 1 May 2012 at 02:49:19 UTC, bioinfornatics wrote:
Le lundi 30 avril 2012 à 14:52 +0200, bioinfornatics a écrit :
Anyone know how to know the position of 3 token in one sequence in one
pass?

tok1 = a
tok2 = b
tok3 = c
seq =  blah count me
b=> 0 a=>2 c=>5
iterate over sequence if token not yet seen count the number of
iteration for know in which column it is located. Example own written on
the fly)

I would say you are trying to way overcomplicated the solution. For simplicity I would use an AA, then a foreach and it will do it in 1 pass (2 if you count the inner ones). I'm not sure if there's a better solution already in phobos, so I can't refer to that :( This could be modified to be more generic so..

//returns AA of tokens that were found and the offset of their first occurrence.
int[char] findTokens(const char[] input, char[] tok ...) {
  int[char] offs;
  foreach(i, ch; input) {
    foreach(t; tok) {
      if (ch == t && t !in offs)
        offs[t] = i;
    }
  }
  return offs;
}
unittest {
assert(findTokens("blah count me", 'a', 'b', 'c') == ['a':2, 'b' : 0, 'c':5]);
}

Reply via email to