Page 1 of 1

new array method - cycle([shift])

Posted: Sun Jul 19, 2015 4:07 am
by andenixa
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.

Re: new array method - cycle([shift])

Posted: Sun Jul 19, 2015 10:34 am
by Turley
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. :)

Re: new array method - cycle([shift])

Posted: Sun Jul 19, 2015 10:46 am
by Turley
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.

Re: new array method - cycle([shift])

Posted: Tue Jul 21, 2015 8:19 pm
by andenixa
Thats a terrific suggestion, thanks Turley.

Re: new array method - cycle([shift])

Posted: Tue Jul 21, 2015 9:43 pm
by andenixa
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.

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;