wc3campaigns
WC3C Homepage - www.wc3c.netUser Control Panel (Requires Log-In)Engage in discussions with other users and join contests in the WC3C forums!Read one of our many tutorials, ranging in difficulty from beginner to advanced!Show off your artistic talents in the WC3C Gallery!Download quality models, textures, spells (vJASS/JASS), systems, and scripts!Download maps that have passed through our rigorous approval process!

Go Back   Wc3C.net > Resources > Code Resources > Scripts
User Name
Password
Register Rules Get Hosted! Chat Pastebin FAQ and Rules Members List Calendar



Reply
 
Thread Tools Search this Thread
Old 12-08-2009, 11:35 PM   #16
PurplePoot
User


Official Map Reviewer
 
Join Date: Jan 2006
Posts: 363

PurplePoot will become famous soon enough (60)PurplePoot will become famous soon enough (60)PurplePoot will become famous soon enough (60)

Default

Quote:
Originally Posted by weaaddar
First, your variable names are terrible. o,r,c are not very meaningful.
Sorry about that, it was largely my fault. However, what's the big deal? It's not like they do anything very interesting or hard to see from glancing at their respective functions.

Also, those benchmarks are not very meaningful (getRandomInt takes a random amount of time to execute), but does speed really matter? I can't see this as being a high-speed operation.
PurplePoot is offline   Reply With Quote
Sponsored Links - Login to hide this ad!
Old 12-09-2009, 12:29 AM   #17
Rising_Dusk
Obscurity, the Art


Projects Director
Project Leader: OD
 
Join Date: Feb 2006
Posts: 9,729

Submissions (27)

Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)

Hero Contest #3 - 1st PlaceApproved Map: Desert of ExileApproved Map: Advent of the ZenithHero Contest #2 - 1st PlaceHero Contest - Third place>

Send a message via AIM to Rising_Dusk Send a message via MSN to Rising_Dusk
Default

Quote:
Originally Posted by PurplePoot
Also, those benchmarks are not very meaningful (getRandomInt takes a random amount of time to execute), but does speed really matter? I can't see this as being a high-speed operation.
Yeah, but they average out to roughly the same over many iterations, so they are meaningful.

The variable names are plenty meaningful, they're just intentionally short so as to not bloat code. I've used similar names for locals in countless of my submissions. o is a substitute for i, index. c is for count, and r is for real. Pretty straightforward.
__________________
Rising_Dusk is offline   Reply With Quote
Old 12-09-2009, 01:50 AM   #18
weaaddar
User


Respected User
 
Join Date: Apr 2002
Posts: 2,372

Submissions (3)

weaaddar has a spectacular aura about (131)

Default

As for speed, people can always use the map optimizer, you shouldn't write intentionally obfuscated code. o a substitute for i? What does that even mean. It makes sense to you as you've worked with the code, but as for people merely glancing its hard to figure that out. If you really care about performance you should only be setting by need.

Another thing I've noticed is that you really can save a lot of performance if you can just consider 0 to be an unused value. If you could just do fetch, as opposed to test and fetch you'd gain some speed. Could you store Cnt+1 in the hashtable, that way you never have to worry about if(HaveSavedInteger(...)) as you can just set o = LoadInteger(...) and then test if(o != 0)

Heres a 5 minute job::
Collapse JASS:
library Pool
globals
    //These constants can be changed
    private constant integer MAX_INSTANCES         = 8191
    private constant integer MAX_ENTRIES           = 256
            constant integer INTPOOL_NO_ENTRIES    = 0x672819
            
    //Don't change the following global declaration
    private hashtable ht = InitHashtable()
endglobals

struct intpool[MAX_INSTANCES]
    private integer Cnt = 0
    private real    WeightTotal = 0.
    private integer array Entries[MAX_ENTRIES]
    private real    array Weights[MAX_ENTRIES]
    
    method getWeight takes integer entry returns real
        return Weights[LoadInteger(ht, integer(this), entry)]
    endmethod
    
    method addInt takes integer entry, real weight returns nothing
        local integer o;
        if.Cnt == MAX_ENTRIES then
            //Can't hold any more entries
            debug call BJDebugMsg(SCOPE_PREFIX+"Error: .addEntry has reached MAX_ENTRIES")
            return
        elseif weight <= 0. then
            //Zero or negative weights make no sense
            debug call BJDebugMsg(SCOPE_PREFIX+"Error: .addEntry can't take zero or negative weights")
            return
        endif
        o = LoadInteger(ht,this,entry);
        if o > 0 then
            //Update old entry
            set .WeightTotal = .WeightTotal - .Weights[o] + weight
            set .Weights[o]  = weight
        else
            //Make a new entry
            set .Cnt           = .Cnt + 1
            call SaveInteger(ht, integer(this), entry, .Cnt)
            set .Entries[.Cnt] = entry
            set .Weights[.Cnt] = weight
            set .WeightTotal   = .WeightTotal + weight
        endif
    endmethod
    method removeInt takes integer entry returns nothing
        local integer o = LoadInteger(ht, integer(this), entry)
        if o > 0 then
            call RemoveSavedInteger(ht, integer(this), entry)
            //Remove its entry in the arrays
            set .WeightTotal = .WeightTotal - .Weights[o]
            set .Entries[o]  = .Entries[.Cnt]
            set .Weights[o]  = .Weights[.Cnt]
            set .Cnt         = .Cnt - 1
        debug else
            debug call BJDebugMsg(SCOPE_PREFIX+"Error: .removeEntry entry doesn't exist")
        endif
    endmethod
    method getRandomInt takes nothing returns integer
        local real    r = GetRandomReal(0, .WeightTotal)
        local integer c = 1
        if .WeightTotal <= 0. then
            debug call BJDebugMsg(SCOPE_PREFIX+"Error: intpool has no entries")
            return INTPOOL_NO_ENTRIES
        endif
        loop
            set r = r - .Weights[c]
            exitwhen r <= 0
            set c = c + 1
        endloop
        return .Entries[c]
    endmethod
endstruct
endlibrary

I think this is a bit cleaner. untested though.

Last edited by weaaddar : 12-09-2009 at 02:07 AM.
weaaddar is offline   Reply With Quote
Old 12-09-2009, 02:03 AM   #19
Rising_Dusk
Obscurity, the Art


Projects Director
Project Leader: OD
 
Join Date: Feb 2006
Posts: 9,729

Submissions (27)

Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)

Hero Contest #3 - 1st PlaceApproved Map: Desert of ExileApproved Map: Advent of the ZenithHero Contest #2 - 1st PlaceHero Contest - Third place>

Send a message via AIM to Rising_Dusk Send a message via MSN to Rising_Dusk
Default

Quote:
Originally Posted by weaaddar
As for speed, people can always use the map optimizer, you shouldn't write intentionally obfuscated code. o a substitute for i? What does that even mean. It makes sense to you as you've worked with the code, but as for people merely glancing its hard to figure that out. If you really care about performance you should only be setting by need.
I am not writing code for research or for use with numerous collaborators, it doesn't matter what I use. I am the only person who will be working on it, so I'm the one who needs to know what things do. The variables aren't even being used in confusing ways, it's pretty clear to the reviewers what is going on.
Quote:
Originally Posted by weaaddar
Another thing I've noticed is that you really can save a lot of performance if you can just consider 0 to be an unused value. If you could just do fetch, as opposed to test and fetch you'd gain some speed. Could you store Cnt+1 in the hashtable, that way you never have to worry about if(HaveSavedInteger(...)) as you can just set o = LoadInteger(...) and then test if(o != 0)
I don't like the assumption that 0 is unused. I use these integer pools for array indexing of other datatypes, so the 0 index is important.
__________________
Rising_Dusk is offline   Reply With Quote
Old 12-09-2009, 02:09 AM   #20
weaaddar
User


Respected User
 
Join Date: Apr 2002
Posts: 2,372

Submissions (3)

weaaddar has a spectacular aura about (131)

Default

No the problem is you store CNT 0. If you internally store CNT starting from 1. You get a nice boost. Your wieghts or ints stored can be anything else. Again, you can store anything you like just store your internal data (CNT from 1).

And Rising_dusk whats the point of releasing a resource to the public if its for internal use only :?
weaaddar is offline   Reply With Quote
Old 12-09-2009, 02:15 AM   #21
Rising_Dusk
Obscurity, the Art


Projects Director
Project Leader: OD
 
Join Date: Feb 2006
Posts: 9,729

Submissions (27)

Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)

Hero Contest #3 - 1st PlaceApproved Map: Desert of ExileApproved Map: Advent of the ZenithHero Contest #2 - 1st PlaceHero Contest - Third place>

Send a message via AIM to Rising_Dusk Send a message via MSN to Rising_Dusk
Default

Quote:
Originally Posted by weaaddar
And Rising_dusk whats the point of releasing a resource to the public if its for internal use only :?
Dude, come on, these scripts are in general black boxes that people use. They don't need to know why they work, only that they do work for what the user needs. Knowing is a step beyond using. Three local variables being named with single characters do not make the script cryptic and arcane or impossible to understand.
Quote:
Originally Posted by weaaddar
No the problem is you store CNT 0. If you internally store CNT starting from 1. You get a nice boost. Your wieghts or ints stored can be anything else. Again, you can store anything you like just store your internal data (CNT from 1).
Yes, I see what you're saying. Anyways, the speed boost won't be huge, but I will do it because I can.

EDIT: I tested your suggested fix and even @ 2000 iterations could not benchmark a noticeable difference in speeds. I'm pretty sure the HaveSavedInteger call must be extremely fast, and it's also more readable with it instead of the o>0 stuff. (Personally)
__________________

Last edited by Rising_Dusk : 12-09-2009 at 06:28 AM.
Rising_Dusk is offline   Reply With Quote
Old 12-09-2009, 01:46 PM   #22
Mr.Malte
User
 
Mr.Malte's Avatar
 
Join Date: Apr 2008
Posts: 286

Submissions (2)

Mr.Malte is on a distinguished road (11)

Default

Could you really please add a way to loop over the instances of a pool somehow or could you find another solution to this problem?

I am using this library at the moment for a script.
I have got a struct with a pool and two arrays that contain information about certain requirements.
I want to add a method to the struct that returns an instance of the pool - but only if it fulfills the conditions.
There is none elegant way for that at the moment.
Mr.Malte is offline   Reply With Quote
Old 12-09-2009, 01:50 PM   #23
Rising_Dusk
Obscurity, the Art


Projects Director
Project Leader: OD
 
Join Date: Feb 2006
Posts: 9,729

Submissions (27)

Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)

Hero Contest #3 - 1st PlaceApproved Map: Desert of ExileApproved Map: Advent of the ZenithHero Contest #2 - 1st PlaceHero Contest - Third place>

Send a message via AIM to Rising_Dusk Send a message via MSN to Rising_Dusk
Default

Quote:
Originally Posted by Mr.Malte
Could you really please add a way to loop over the instances of a pool somehow or could you find another solution to this problem?
Here's your solution: Use an intpool with a LinkedList on your data arrays. You need to get this through your mind that the intpool is a not a data storage structure, it is a data handling structure. It handles your data by assigning weights to them, it doesn't store them for you to get back later. You are not allowed to access or loop through all of the entries.
__________________

Last edited by Rising_Dusk : 12-09-2009 at 01:52 PM.
Rising_Dusk is offline   Reply With Quote
Old 12-09-2009, 02:02 PM   #24
Mr.Malte
User
 
Mr.Malte's Avatar
 
Join Date: Apr 2008
Posts: 286

Submissions (2)

Mr.Malte is on a distinguished road (11)

Default

Ok, thank you.
Will try that.
But a countWeight function would still be nice.

edit: I can also use my own int ant count it up if I add a new value.

Last edited by Mr.Malte : 12-09-2009 at 02:05 PM.
Mr.Malte is offline   Reply With Quote
Old 12-09-2009, 02:08 PM   #25
Rising_Dusk
Obscurity, the Art


Projects Director
Project Leader: OD
 
Join Date: Feb 2006
Posts: 9,729

Submissions (27)

Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)

Hero Contest #3 - 1st PlaceApproved Map: Desert of ExileApproved Map: Advent of the ZenithHero Contest #2 - 1st PlaceHero Contest - Third place>

Send a message via AIM to Rising_Dusk Send a message via MSN to Rising_Dusk
Default

Quote:
Originally Posted by Mr.Malte
But a countWeight function would still be nice.
You mean to get the total cumulative weight of the given intpool? That could be useful for calculating exact % chances to obtain certain entries, but it would see very rare usage. I could make it available easily, though.
Quote:
Originally Posted by Mr.Malte
edit: I can also use my own int ant count it up if I add a new value.
Yep.
__________________

Last edited by Rising_Dusk : 12-09-2009 at 02:08 PM.
Rising_Dusk is offline   Reply With Quote
Old 12-09-2009, 11:34 PM   #26
weaaddar
User


Respected User
 
Join Date: Apr 2002
Posts: 2,372

Submissions (3)

weaaddar has a spectacular aura about (131)

Default

Weird, my testing showed that the havesavedhandle performed about the same as LoadUnitHandle. I'll set up a benchmarker of all hashtable natives again.
weaaddar is offline   Reply With Quote
Old 12-09-2009, 11:44 PM   #27
Rising_Dusk
Obscurity, the Art


Projects Director
Project Leader: OD
 
Join Date: Feb 2006
Posts: 9,729

Submissions (27)

Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)

Hero Contest #3 - 1st PlaceApproved Map: Desert of ExileApproved Map: Advent of the ZenithHero Contest #2 - 1st PlaceHero Contest - Third place>

Send a message via AIM to Rising_Dusk Send a message via MSN to Rising_Dusk
Default

It's possible that it just varies so much that in my tests I didn't notice. I ran them a couple times and couldn't tell the difference between the two versions of the libraries on average. My computer also isn't amazing, so God knows what that does to benchmarks.
__________________
Rising_Dusk is offline   Reply With Quote
Old 12-10-2009, 12:58 AM   #28
weaaddar
User


Respected User
 
Join Date: Apr 2002
Posts: 2,372

Submissions (3)

weaaddar has a spectacular aura about (131)

Default

Here is my results thread for you:: http://www.wc3c.net/showthread.php?p...79#post1114879
weaaddar is offline   Reply With Quote
Old 12-10-2009, 01:04 AM   #29
Rising_Dusk
Obscurity, the Art


Projects Director
Project Leader: OD
 
Join Date: Feb 2006
Posts: 9,729

Submissions (27)

Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)Rising_Dusk has a reputation beyond repute (1192)

Hero Contest #3 - 1st PlaceApproved Map: Desert of ExileApproved Map: Advent of the ZenithHero Contest #2 - 1st PlaceHero Contest - Third place>

Send a message via AIM to Rising_Dusk Send a message via MSN to Rising_Dusk
Default

Thanks, I guess.
__________________
Rising_Dusk is offline   Reply With Quote
Old 12-10-2009, 03:51 AM   #30
grim001
requires vJass
 
grim001's Avatar


Code Moderator
 
Join Date: Nov 2006
Posts: 1,540

Submissions (10)

grim001 is just really nice (277)grim001 is just really nice (277)

Send a message via AIM to grim001
Default

Code:
<grim001>	Dusk, if you plan to update Pool some more, a method to get the chance of returning a specific value would be cool
<Rising_Dusk>	?
<Rising_Dusk>	What?
<Rising_Dusk>	Isn't that what Pool already does?
<Bobo_The_Kodo>	Pool.GetChanceToReturnThisInt(5)
<Rising_Dusk>	Oh, I see.
<Rising_Dusk>	Post that or I will forget it.
grim001 is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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


All times are GMT. The time now is 02:55 PM.


Affiliates
The Hubb The JASS Vault Clan WEnW Campaign Creations Clan CBS GamesModding Flixreel Videos

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services