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

How to transfer a lot of options in "for" operator


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
groofshark
Member
Join Date: Jul 2013
Old 05-29-2015 , 06:31   How to transfer a lot of options in "for" operator
Reply With Quote #1

Hello, I have a lot of options and I want to save space and turn them into "for" operator, but I dont know how... I tried but there isn't effect...

Simple Example Code


I want to do something like that, but now there isn't effect:
PHP Code:
for( new itemitem<sizeof(options-1); item++ )
{    
    for( new 
inuminum<sizeof(iItem-1); inum++ )
    {
        if( 
g_options[id][item] && iItem == inum 
        {
            return 
ITEM_DISABLED;
        }
    }

I would like to help me. Thanks

Last edited by groofshark; 05-29-2015 at 06:32.
groofshark is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 05-29-2015 , 09:10   Re: How to transfer a lot of options in "for" operator
Reply With Quote #2

just a heads up, just because the second code "looks better" it's actually worse in terms of optimization since you're uselessly adding two loops in there

this is micro optimization, aka it doesn't directly affect the server, but still, it's good not to get used to such practices as when they add up they start impacting the server
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.
aron9forever is offline
groofshark
Member
Join Date: Jul 2013
Old 05-29-2015 , 14:56   Re: How to transfer a lot of options in "for" operator
Reply With Quote #3

Didn't understand...
groofshark is offline
Neeeeeeeeeel.-
Some Guy Yellin'
Join Date: Jul 2010
Location: Argentina
Old 05-29-2015 , 15:39   Re: How to transfer a lot of options in "for" operator
Reply With Quote #4

If you tell us about what exactly you are doing we could help you optimizing your code. For example, what is iItem, g_option, etc.

As aron9forever said, if there are just 3 conditionals would be better to leave it as the first example.
__________________
Neeeeeeeeeel.- is offline
Send a message via Skype™ to Neeeeeeeeeel.-
groofshark
Member
Join Date: Jul 2013
Old 05-29-2015 , 17:22   Re: How to transfer a lot of options in "for" operator
Reply With Quote #5

I want exactly to change the content of menu_cb function to for operators. Something like 2nd code, but correctly written. No there are more options, just I want to see example.
groofshark is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-29-2015 , 17:57   Re: How to transfer a lot of options in "for" operator
Reply With Quote #6

Quote:
Originally Posted by groofshark View Post
I want exactly to change the content of menu_cb function to for operators. Something like 2nd code, but correctly written. No there are more options, just I want to see example.
Just because you want to do it doesn't mean you should do it. As the others have already said, you need to provide better context of what you are trying to do. I wouldn't be surprised if this is an XY Problem.
__________________
fysiks is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 05-30-2015 , 04:37   Re: How to transfer a lot of options in "for" operator
Reply With Quote #7

You already have a lookup table that is indexed by your item numbers. If your code is more complex than the example you provided, you should always look towards crafting this type of setup, where you simply retrieve from an array index - it doesn't matter if it is of constant size or dynamic. If you have a case where you feel that a for loop is actually necessary, you need to update your example and show us.

Code:
if (g_options[id][iItem]) {    return MENU_DISABLED; }
__________________
In Flames we trust!

Last edited by Nextra; 05-30-2015 at 04:43.
Nextra is offline
groofshark
Member
Join Date: Jul 2013
Old 05-30-2015 , 04:47   Re: How to transfer a lot of options in "for" operator
Reply With Quote #8

Quote:
Originally Posted by Nextra View Post
You already have a lookup table that is indexed by your item numbers. If your code is more complex than the example you provided, you should always look towards crafting this type of setup, where you simply retrieve from an array index - it doesn't matter if it is of constant size or dynamic. If you have a case where you feel that a for loop is actually necessary, you need to update your example and show us.

Code:
if (g_options[id][iItem]) {    return MENU_DISABLED; }
That's for all menu. I want instead adding 20 if( g_options[id][iItem] ) just to make a loop and that loop to give on every "IF" - option and his number(iItem)

EDIT(sorry, double posting):
That's the example


I can't explain it better.

Last edited by groofshark; 05-30-2015 at 04:52.
groofshark is offline
Old 05-30-2015, 04:50
groofshark
This message has been deleted by groofshark.
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-30-2015 , 10:31   Re: How to transfer a lot of options in "for" operator
Reply With Quote #9

What Nextra provided should do the trick as long as your sample code is accurate to what you're trying to do.
__________________

Last edited by Bugsy; 05-30-2015 at 10:54.
Bugsy is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 05-30-2015 , 11:23   Re: How to transfer a lot of options in "for" operator
Reply With Quote #10

Quote:
Originally Posted by groofshark View Post
That's for all menu. I want instead adding 20 if( g_options[id][iItem] ) just to make a loop and that loop to give on every "IF" - option and his number(iItem)
You don't need to loop unless you have a more complex situation.

Code:
/**  * Given this enum the following things are true:  *    opt1 == 0 && opt2 == 1 && opt3 == 2  */ enum _:options {     opt1,     opt2,     opt3, } /**  * That means that this if can easily be reduced.  * Observe that when |iItem == 0| then |opt1 == iItem| is also true.  */ if(g_options[id][opt1] && iItem == 0 ) {     return ITEM_DISABLED; } /**  * So instead we can write it as following, note that |opt1| has  * been replaced, and instead |iItem| is retrieved from the array.  * This does not change code behavior.  */ if(g_options[id][iItem] && iItem == 0 ) {     return ITEM_DISABLED; } /**  * Since the code inside of your if statements does not  * differ (it always returns the same value), the |iItem == 0|  * check is actually redundant. This means that we can remove it  * and get rid of all the other if statements at the same time:  */ if(g_options[id][iItem]) {     return ITEM_DISABLED; }

A loop would unnecessarily complicate things. You need to do something based on a single value, and you have the means to directly derive the necessary information from that single value. You don't need to search for anything, you don't need to iterate. A lookup-table is perfect for this, and you already have it in place. You just need to use it properly.
__________________
In Flames we trust!
Nextra 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 05:31.


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