new array method - cycle([shift])

Made a small change or new addition to the POL Core that makes a difference? You can post the changes here in .patch or .diff file format, for our Dev team to screen and apply to the SVN!

Moderator: POL Developer

Locked
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

new array method - cycle([shift])

Post 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.
Attachments
array_mth_cycle.patch
(2.56 KiB) Downloaded 643 times
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

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

Post 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. :)
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

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

Post 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.
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

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

Post by andenixa »

Thats a terrific suggestion, thanks Turley.
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

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

Post 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;

Locked