Compute in one pass 3 tokens position
bioinfornatics
bioinfornatics at fedoraproject.org
Mon Apr 30 19:48:12 PDT 2012
Le lundi 30 avril 2012 à 14:52 +0200, bioinfornatics a écrit :
> Hi,
> I would like to know how compute in on pass 3 tokens position in a
> sequence.
> curently i do:
> File f = File( "reader.d", "r" );
> scope(exit) f.close();
> char[1024] buffer;
> char[] content = f.rawRead(buffer);
> char[sizediff_t] token = ['(', '{', ';'];
> auto position = map!( a => content.countUntil( a ) )( [ ['('], ['{'],
> [';'] ] );
>
>
> if i use reduce instead map the build fail
>
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)
sizediiff_t[] countUntil( in char[] seq; char[] tokens ){
bool isSearching = true;
size_t index = 0;
sizediiff_t[] position = new sizediiff_t[](tokens.length);
while (isSearching){
if (index >⁼ seq.length)
isSearching = false;
else if (! find( position, -1) // al token found we can stop;
isSearching = false;
else{
sizediiff_t tmp = countUntil( tokens, [seq[index]]);
if (countUntil( tmp != -1 && position[tmp] != -1)
position[tmp] = index;
index++;
}
}
}
More information about the Digitalmars-d-learn
mailing list