array.cycle() will move array backwards or forwards in-place as it were a conveyor belt.
{1, 2, 3}.cycle()
Will make it:
{3, 1, 2}
Accepts a negative value as a shift to move different direction.
* Currently only shift by 1 element is supported.
The performance probably is horrible like O( 2*N ), but still a way faster than trying it in eScript.
Very useful if you are into advanced scripting and for debugging purposes.
PS: Turley please start adding these useful methods. I have more from where that came from with thorough explanation of what they do.
PSS: Are you annoyed by my banner? Should I remove it? Becasue I am.
new array method - cycle([shift])
Moderator: POL Developer
new array method - cycle([shift])
- Attachments
-
- array_mth_cycle.patch
- (2.56 KiB) Downloaded 646 times
Re: new array method - cycle([shift])
Please keep publishing your additions, they are always welcome. My time is currently very limited and in the spare time I often do something else currently then programing (enough of it in my job)
But I promise to use my time more often for polserver.
But I promise to use my time more often for polserver.
Re: new array method - cycle([shift])
Just looked into the patch. I think std::rotate should do the job.
Something like
std::rotate(ref_array.begin(),ref_array.end()-shift_by, ref_array.end())
As long as shift_by is less then size of ref_array.
Something like
std::rotate(ref_array.begin(),ref_array.end()-shift_by, ref_array.end())
As long as shift_by is less then size of ref_array.
Re: new array method - cycle([shift])
Thats a terrific suggestion, thanks Turley.
Re: new array method - cycle([shift])
Yes, std::rotate seems to be doing the trick with a bit of tinkering.
I don't suppose I'll do another patch though. The mechanics got exceedingly complicated
since you moved to github, and you seem to know your magic well without me.
I don't suppose I'll do another patch though. The mechanics got exceedingly complicated
since you moved to github, and you seem to know your magic well without me.
Code: Select all
case MTH_CYCLE:
if (name_arr.empty())
{
int shift_by;
if (ex.numParams() > 0) {
if (ex.getParam( 0, (long&)shift_by )==false) {
return new BError( "Invalid parameter type" );
}
if (shift_by==0) {
return new BLong(0);
}
} else {
shift_by = 1;
}
if( ref_arr.size() == 0 )
return new BLong(0);
if (std::abs(shift_by) > ref_arr.size()) {
// seems to be useless, probably could just return 0 as well
shift_by = boost::math::copysign( ref_arr.size(), shift_by );
}
if(shift_by>0)
std::rotate(ref_arr.begin(), ref_arr.end()-shift_by, ref_arr.end());
else
std::rotate(ref_arr.begin(), ref_arr.begin()-shift_by, ref_arr.end());
return new BLong(1);
}
break;