Raised This Month: $51 Target: $400
 12% 

Rotating an Array?


Post New Thread Reply   
 
Thread Tools Display Modes
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-14-2016 , 03:10   Re: Rotating an Array?
Reply With Quote #11

Quote:
Originally Posted by BAILOPAN View Post
FYI: Macro-functions are slated for removal.
ShiftArray and CircleIndex would be better off as legit functions. Makes them more readable, too.
__________________
ddhoward is offline
fakuivan
Senior Member
Join Date: Nov 2015
Old 09-17-2016 , 18:06   Re: Rotating an Array?
Reply With Quote #12

PHP Code:
#include <sourcemod>

#pragma semicolon 1
#pragma newdecls required
#define INVERSE(%1,%2)    %1[(sizeof(%1) - 1) - %2]    /** INVERSE(array, element) **/

public Plugin myinfo = {
    
name        "",
    
author      "",
    
description "",
    
version     "0.0.0",
    
url         ""
};

public 
void OnPluginStart()
{
    
int i_array[] = {1234};
    
InverseAccess(i_arraysizeof(i_array), 3); //should return 1
    
INVERSE(i_array3); //should return 1
}

any InverseAccess(any[] v_arrayint i_sizeint i_element)    //if you like runtime overhead
{
    return 
v_array[i_size -i_element];

It compiles on spider

Last edited by fakuivan; 09-17-2016 at 18:09.
fakuivan is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-18-2016 , 03:33   Re: Rotating an Array?
Reply With Quote #13

It's not so much "liking runtime overhead" as much as it is "liking my code being compilable even in future versions of Sourcemod"
__________________

Last edited by ddhoward; 09-18-2016 at 03:34.
ddhoward is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 09-18-2016 , 12:43   Re: Rotating an Array?
Reply With Quote #14

I'd take runtime overhead as opposed to shitty macros (macro beyond simple value insertion).

Also more readable than macros.
Potato Uno is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 09-18-2016 , 13:18   Re: Rotating an Array?
Reply With Quote #15

Quote:
Originally Posted by Potato Uno View Post
I'd take runtime overhead as opposed to shitty macros (macro beyond simple value insertion).

Also more readable than macros.
You should use constants even for definitions ("simple value insertions"), they are type safe. There's almost no reason for using macros.
klippy is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 09-18-2016 , 17:56   Re: Rotating an Array?
Reply With Quote #16

That's true, and I think it might even be faster if done that way.
Potato Uno is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 09-22-2016 , 14:11   Re: Rotating an Array?
Reply With Quote #17

In what situation would you rotate an array as opposed to just looping it backwards?
headline is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-22-2016 , 21:24   Re: Rotating an Array?
Reply With Quote #18

Quote:
Originally Posted by Headline View Post
In what situation would you rotate an array as opposed to just looping it backwards?
They produce different results.

Original array contents:
0 1 2 3 4 5 6 7 8 9

Effective contents when looped backwards:
9 8 7 6 5 4 3 2 1 0

Contents after being rotated forward by 1 :
9 0 1 2 3 4 5 6 7 8

Contents after being rotated forward by 2 :
8 9 0 1 2 3 4 5 6 7

Contents after being rotated backwards by 1:
1 2 3 4 5 6 7 8 9 0

Contents after being rotated backwards by 2:
2 3 4 5 6 7 8 9 0 1

Etc.
__________________

Last edited by ddhoward; 09-22-2016 at 21:27.
ddhoward is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 09-22-2016 , 22:23   Re: Rotating an Array?
Reply With Quote #19

Quote:
Originally Posted by ddhoward View Post
They produce different results.

Original array contents:
0 1 2 3 4 5 6 7 8 9

Effective contents when looped backwards:
9 8 7 6 5 4 3 2 1 0

Contents after being rotated forward by 1 :
9 0 1 2 3 4 5 6 7 8

Contents after being rotated forward by 2 :
8 9 0 1 2 3 4 5 6 7

Contents after being rotated backwards by 1:
1 2 3 4 5 6 7 8 9 0

Contents after being rotated backwards by 2:
2 3 4 5 6 7 8 9 0 1

Etc.
Oh, gotcha. Thanks for the clarification
headline is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:46.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode