[GUI Library] SerraUnitIndexing

SerraAvenger

Cuz I can
Reaction score
234
A simplistic indexing library. Please use this together with an interface, don't use these functions directly.

API
SUI_GetUnitIndex( u ) -> int, Gets a unique index for every unit. If the unit previously had none, a new one will be allocated.
SUI_GetUnitByIndex( i ) -> unit, returns a unit associated with the index such that u == SUI_GetUnitByIndex( SUI_GetUnitIndex( u ) ) always holds true. Thanks to Jesus4Lyf for this idea!
SUI_GetCustomValue() -> int, returns a new, unique custom value every time it is called.


Restrictions
Only 400 units that had custom indicies may be dead at a time
Only 2048 units that have custom indicies may be in a map at any time.
The custom value with the id "IdentificationIndex" may not be used. By default, the IdentificationIndex is 1337.
If those doesn't work for you (for any possible reason), just change the values.
This work is published under the NonCommercial Sampling Plus CreativeCommons license.

Please also see...
UnitIndexing, the interface this is meant to be used with.

FAQ
Q<HELP! Everything is only "Untitled blabla"! I can't use this!
A>That is normal. You are probably in standard GUI mode. In order to see function & variable names, press Ctrl+D (raw mode). HOWEVER that is only if you want to look at the code. If you want to use this library, go to UnitIndexing and follow the steps under "how to use this?">SerraUnitIndexing. Don't use functions from this library (or any Indexer) directly. ESPECIALLY if you're coding your own library.

ChangeLog
6/18/2010 - Added UnitGetByIndex(). Fixed a little bit of stuff such that the changeable variables are visible from standard GUI. V0.3
6/11/2010 - Fixed recycling bug. V0.2
 

Attachments

  • SerraUnitIndexing.SC2Lib
    29.6 KB · Views: 468

Steel

Software Engineer
Reaction score
109
All your variables and triggers load with "Untitled <type>". Each function, variable, etc. Not sure if this is on my end, something like disallowing an imported library to generate variable names. Couldn't find anything in the preferences.

Ok, I tried to power through this with everything named Untitled Variable 001, I don't think you have a good enough recycle function. It does what it's supposed to but it is slow. I can give you a more in depth analysis if you want it and this variable thing gets resolved.

serraunitindexing.png
 

SerraAvenger

Cuz I can
Reaction score
234
All your variables and triggers load with "Untitled <type>". Each function, variable, etc. Not sure if this is on my end, something like disallowing an imported library to generate variable names. Couldn't find anything in the preferences.

You were supposed to use this with UnitIndexing.
If you want to read this piece of code, use Ctrl+D. The Interface has GUI names, this one only has raw names = )

Ok, I tried to power through this with everything named Untitled Variable 001, I don't think you have a good enough recycle function. It does what it's supposed to but it is slow. I can give you a more in depth analysis if you want it and this variable thing gets resolved.

Indeed, a more in-depth analysis would be apreciated. Especially about the recycling and the speed : )
Again, pressing Ctrl+D makes the code readable (it's not supposed to be used directly)

>*Is the a way to convert to custom text, yet?
Depends on what you want.
Ctrl+F11 compiles the map script and shows it to you, if that's what you want = )

EDIT:
Btw, the fact that you see everything like that stems from the fact that I only code in Raw mode (Ctrl+D). Automatically generating the GUI identifiers from the script identifiers should, but does not, work.
Since, however, the UnitIndexer is the library that the end user (who is probably not galaxy-proficient) should use, I think giving everything a name is neither worth the hassle nor needed. In fact, giving it standard GUI names would only increase the likeliness of this being used directly - which does (ofc) work, but is pretty dumb.

EDIT2:
Found a critical bug where I had put 0 instead of NextFreeIndex. Recycling should not have worked hence. Fixed and uploaded fixed version.
EDIT3: Seems I didn't upload the fixed version -.-
 

Steel

Software Engineer
Reaction score
109
I'll look at it after work but here is something you should think about:
Do I want the recycle or the lookup method to be faster?

Usually a speed increase to the lookup is better to look for. Since you're processing an unsorted amount of data it becomes difficult to increase speed without a reference point because you're just traversing every element. If you have 400 elements in your array and you start at 0 each time and what you're looking for is in the last position you still have to pass over each element. So every time you reference that unit you take the longest time possible, which is n (n in this case being 400). If you can do something like sorting these items by the unit's name, you can cut down on time needed to find the necessary unit. Visual Example:


array[0] = "Admiral Raynor"
array[1] = "Hydralisk"
array[2] = "Hydralisk"
array[3] = "Hydralisk"
array[4] = "Larva"
array[5] = "Larva"
array[6] = "Marine"
array[7] = "Marine"
array[8] = "Marine"
array[9] = "Marine"
array[10] = "Observer"

Even though you are ordering them by string and creating duplicates, it's a faster method. You wouldn't store them based on their name, you would still use the unique Unit datatype to identify them, Marine is just your key. Since the data is sorted you can easily implement a number of search methods to get what you need. You should be able to find the algorithmic differences between list traversal and other various lookup methods out there.


Another thing I hinted on was that each time you reference the unit you're talking about you need to look it up. Sure, in a single function you could do something like...
lv_unitNew = SerraUnitIndexingFind(<someunit>)
Then just go about using lv_unitNew. However, if you reference it multiple times, especially in quick succession outside of the same functions, you're going to get a performance hit. You might want to think about making a cache for quick referencing units. It's less practical for a dataset of only 400 though, if you get up to something like a thousand or more, it would be worth it.

This is really complex stuff, it's fun if you want to tinker around with things but this isn't anything you should bend over backwards to worry about.
 

SerraAvenger

Cuz I can
Reaction score
234
Short reply:
I'm using UnitData for lookup and should have constant lookup time. No iterative search, unless the GUI unit user data does use iterative search itself.
Indexing and recycling is both constant time, too. More later, gtg = )

EDIT:



>Another thing I hinted on was that each time you reference the unit you're talking about you need to look it up. Sure, in a single function you could do something like...
lv_unitNew = SerraUnitIndexingFind(<someunit>)
Then just go about using lv_unitNew. However, if you reference it multiple times, especially in quick succession outside of the same functions, you're going to get a performance hit. You might want to think about making a cache for quick referencing units. It's less practical for a dataset of only 400 though, if you get up to something like a thousand or more, it would be worth it.

The thing is just: If I could cache it easily, this wouldn't be needed = )
Please also note that the data set could get any size; Just the amount of indicies that are about to be recycled should never get bigger than 400.

>Usually a speed increase to the lookup is better to look for.
That's absolutely true.
Right now the lookup reads as (if the unit has an index)
Code:
func UnitGetIndex( u unit) int {
 return (int) UnitCustomValueGet( u, 1337 ) // UnitCustomValueGet returns a float :(
}

As you can see, the speed of this function only depends on the UnitCustomValueGet (? not sure if that's the right term) function from Ntve.
AMOF, I'm only having another int comparision (which will fail for an indexed unit).

Allocating a new/reusing an old index is constant time: a couple of variable lookups, an array lookup in the worst case and two variable sets.

Recycling an old index (that is, freeing the index such that it can be used again) has about the same cost as allocating/reusing an index.

So the only thing that really determines speed in my code is the UnitCustomValueGet function.

I could try tinkering with the value index (1337) and see if that makes the code go faster or something. I don't believe it though.
 

SerraAvenger

Cuz I can
Reaction score
234
Is this the same?
Haven't seen it yet, but not exactly. I've just looked at yours and there's couple of (small) differences.
Same:
You're using the same algorithms for allocation/deallocation of indicies.
You're auto-deallocating
Different:
Code behaviour/Possibilities

You're auto-allocating - which makes yours a tiny tad faster in a lot of situations, but probably also generates lots of garbage. The main problem would be for maps with lots of units, where auto-allocation can easily overflow standard array sizes.
You're just using 0 as the custom index, which can be dangerous
You have the possibility to retrieve the unit by Index. I think I should also implement this for my library, though.
You don't have a AllocateCustomValue function (not that it's needed or anything interesting, I thought I'd just point this out)
Documentation/Usability:
You haven't linked to my interface. Especially as there's two systems now, using an interface is crucial.
You should post a small description of what it does, too.
Since you're not linking to my interface, I think I also have to mention that you're a) not using labels, which makes your functions basically unusable in GUI, b) you're using JASS naming conventions, which sucks (UnitGetIndex?), c) you're not usable for a base system that other systems require because you don't show a library id.
EDIT: d), for the same reasoin as c), your library can't even be used in my interface!

As you can probably see, the main difference is that my library is much better documented and easier to use and links to the interface; While your library can easily cause unnecessary array overflows and is a tad faster.
I'll add your IndexGetUnit function tho', since I like it a lot = ) Good idea there! It aids GUIlers a lot ; ) (pun intended)
EDIT: PS: just using "UnitIndexer" as a name for a whole Library is a little tad megalomaniac, isn't it? It kinda suggests that this is the one and only Unit Indexer = ) I know that my Interface is named UnitIndexing, but it is only a shallow interface everyone should be using anyway.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/
  • The Helper The Helper:
    Here is another comfort food favorite - Million Dollar Casserole - https://www.thehelper.net/threads/recipe-million-dollar-casserole.193614/

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top