Created
April 26, 2022 18:50
-
-
Save crazymonkyyy/6de535001d70a9bc45c0ea20913a38c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto reindex(string indexstring,string lengthstring="l",R)(R range){ | |
static struct reindex_{ | |
R range; | |
int j=0; | |
auto opIndex(int i){ | |
i+=j; | |
import basic; | |
mixin("return range["~indexstring~"];"); | |
} | |
auto length(){ | |
auto l=range.length; | |
return mixin(lengthstring)-j; | |
} | |
auto front(){ | |
return this[0]; | |
} | |
void popFront(){ | |
j++; | |
} | |
bool empty(){ | |
return length==0; | |
} | |
} | |
return reindex_(range); | |
} | |
unittest{ | |
import basic; | |
auto foo=iota(0,5); | |
auto bar=iota(0,10); | |
foo.reindex!("$-i-1").writeln; | |
bar.reindex!("$-i-1").writeln; | |
foo.reindex!("min($-1,max(0,i-1))","l+2").writeln; | |
bar.reindex!("min($-1,max(0,i-1))","l+2").writeln; | |
foo.reindex!("min(i,($-1)*2-i+1)","l*2").writeln; | |
bar.reindex!("min(i,($-1)*2-i+1)","l*2").writeln; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment