System Recycle Indexes

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Everything in the "Read Me". Please ask questions and report bugs in this thread only. Version 1.6.

Read Me:
Code:
This system can be easily used to store values on an variable. To each value an integer will be assigned, the integer will
represent the value's location (array) on the variable. By giving the array to the system you could restore the value's data.
What's special about this system is that it recycles arrays constantly, so the only way you could reach the limit is that if
you are doing something wrong, as you would rarely reach 8191 arrays if you'll recycle properly.

The system is divided to indexes. Each index has type and name that defines it. The type will determined what kind of
variables you could attach to the system. The name will change the functions' name in the index. In order to create new index
you'll need to put an action following the syntax in the bottom in the recycleIndexes library. The syntax is:

//! runtextmacro recycleIndexes("<Type>","<Name>","<Null>")

Type - The type of the values stored on the index. There is no limit to this option, all types can be used and you can assign 
       to different indexes the same type.
Name - This will affect the functions' names when used. The functions' names syntax is fucntionName<Name>. So, for example if
       you create an index with the name "Test" your functions would be: functionNameTest (functionName changes from function 
       to function obviously). You can't give two different indexes the same name, and you can't use symbols in your name.
Null - This one is simple. If the index'es type is integer or real, you put here 0, else, you put null. This is made to make
       sure WE will not syntax you, when trying to set handles to 0 or integers to null.
       
Now you need to understand how to use the system. The system is made out of 5 functions, that's it.

SetValue<Name> - This function takes the value you want to store (type depends on the index) and returns an integer. The 
                 integer returned is the location (array) of the value in the index.
GetValue<Name> - Takes an integer and a boolean and returns a value (type depend on the index). The value returned will be the
                 value stored in the array given. If you give true as the boolean, the array will be cleaned, and later it 
                 could be recycled.
GetArray<Name> - Takes a value and returns the array that points to it in the index. If the same value is stored twice on the
                 same index with different arrays the one with the smaller array will always be returned.
GetIndexMax<Name> - Returns the biggest array used by the index.
GetNextArray<Mame> - Returns the next array that will be used in the index.

How to import the system:

    A) Make sure you have NewGen, this system can't work in regular WE it, requires NewGen pack.
    B) Copy the Recycle Indexes trigger to your map.
    
Pros:
- This is a very user friendly system. It is easy to use, flexible and, all the information you called possibly need can be
  refered to easily.
- This is a safe system, it is hard to do mistakes while using it, and if you ever do some mistake, the system will warn you
  if you have debug mode on.


Cons
- The pointers to the values are integers, which are less comfortable than strings.
- Limit of 8191 arrays per index, but this limit is unrealisitc as you'll almost never reach this limit. Also, you may have
  as many indexes as you wish.
- Jass system only, GUIers can't use this system in any way.
- There are a few problems when using integers or reals, the biggest one regards recycling. The easy solution would be 
  creating more indexes instead of recycling.

That is it. There is an example trigger posted that shows how you can use each one of the functions.

This system have been tested many times, however, if you spot bugs / problems / leaks please let me know! All questions are
welcomed as well.

v1.6 - Example Trigger rewritten.

v1.5 - Function GetArray added!
     - Example trigger updated.

v1.4 - Small code fix.

v1.3 - Example trigger improved.
     - Recycling works better now.

v1.2 - Read Me updated.
     - A few code changes.
     - Big changes to the comments.

v1.1 - GetNextArray added.
     - Comments were updated.
     - Read Me was re-written.
     - A few minor code changes.
     
v1.0 - Initial release of the system.

Code:
JASS:
library recycleIndexes

//! textmacro recycleIndexes takes type, name, null

globals
    private $type$ array index$name$ // The variable that holds all the values on it.
    private integer array recycle$name$ // All arrays that can be recycled are stored here.
    private integer recycleMax$name$ = 0 // Stores the number of arrays that can be recycled.
    private integer indexMax$name$ = 0 // Biggest array on index currently.
    private boolean new$name$ = true // Will determine if a new array needs to be created, or if we can recycle.
endglobals

// Returns the currently biggest array used.
function GetIndexMax$name$ takes nothing returns integer
    return indexMax$name$ - 1
endfunction

// Returns the next array that will be used. Differently from GetIndexMax, this function takes recycling into consideration.
function GetNextArray$name$ takes nothing returns integer 
    if recycle$name$[0] == 0 then
        return indexMax$name$
    endif
    return recycle$name$[0]
endfunction

// Takes an array and returns the value it points to. If the function will take true it will clean the array so it could be
// recycled later.
function GetValue$name$ takes integer loc, boolean remove returns $type$
    local integer c = 0
    local $type$ temp = $null$

    if index$name$[loc] == $null$ then // The array isn&#039;t used.
        debug call BJDebugMsg(&quot;Index isn&#039;t used!&quot;)
        return $null$
    endif
    
    if remove then // Should the array be cleared?
        if loc &lt; indexMax$name$ then // Do we need to do it in the hard way?
            set temp = index$name$[loc]
            set index$name$[loc] = $null$
            set new$name$ = false
            set recycle$name$[recycleMax$name$] = loc
            set recycleMax$name$ = recycleMax$name$ + 1
            return temp
        elseif loc == indexMax$name$ then // We can do it the easy way!
            set indexMax$name$ = indexMax$name$ - 1
            return index$name$[loc]
        else // Index doesn&#039;t exist!
            debug call BJDebugMsg(&quot;Index isn&#039;t used!&quot;)
        endif
    endif

    return index$name$[loc]
endfunction

// Takes a value and returns the array pointing to it.
function SetValue$name$ takes $type$ value returns integer
    if new$name$ then // Use a new index, or recycle an old one?
        set index$name$[indexMax$name$] = value
        set indexMax$name$ = indexMax$name$ + 1
        
        if indexMax$name$ == 8192 then // Limit reached!
            debug call BJDebugMsg(&quot;8191 indexes used, limit reached&quot;)
        elseif indexMax$name$ == 8193 then // Limit passed!
            debug call BJDebugMsg(&quot;Too many values attached! Recycle!&quot;)
        endif
        
        return indexMax$name$ - 1
    else // We can recycle an old array.
        set recycleMax$name$ = recycleMax$name$ - 1
        set index$name$[recycle$name$[recycleMax$name$]] = value
        
        if recycleMax$name$ == 0 then // We recycled everything we can.
            set new$name$ = true
        endif
        
        return recycle$name$[recycleMax$name$]
    endif
    
    return 0
endfunction

// Takes a value and returns the array it is stored in.
function GetArray$name$ takes $type$ value returns integer
    local integer c = 0
    local integer j = GetIndexMax$name$()
    
    loop
        exitwhen c &gt; j
        if index$name$[c] == value then
            return c
        endif
        set c = c + 1
    endloop
    
    debug call BJDebugMsg(&quot;Value isn&#039;t stored on this index!&quot;)
    return 0
endfunction
//! endtextmacro

//! runtextmacro recycleIndexes(&quot;unit&quot;,&quot;Pet&quot;,&quot;null&quot;)
//! runtextmacro recycleIndexes(&quot;unit&quot;,&quot;Caster&quot;,&quot;null&quot;)
endlibrary
 

Attachments

  • Recycle Indexes v1.6.w3x
    31 KB · Views: 232
Reaction score
456
Works as designed for me, as I already told you. Couldn't find any bugs, leaks or anything like that.

Good that you rewrote the read me :p..
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Thanks for the feedback Überplayer.

> Good that you rewrote the read me

Yeah, in version 1.0 it wasn't clear enough. I hope it is clearer now.
 

Cohadar

master of fugue
Reaction score
209
Well not bad.
At least algorithm idea is new unlike other zillion similar system attempts.

Tip: reals and integers are not handles, attachment systems are not supposed to work with them, so just say: "do not attach to reals or integers"

So on a scale 1..10 you get 6.
(witch is quite impressive because most new systems of this type get -10 from me)
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Thanks for the feedback Cohadar.

> Tip: reals and integers are not handles, attachment systems are not supposed to work with them, so just say: "do not attach to reals or integers"

Hmm... Well, that would be the easy solution. :p I'm not sure how to fix the current problem I'm having, and I doubt it can be fixed, so, using this system with integers and real is tricky. But, as I said, you can avoid trouble by creating more indexes and never recycling them.
 

Cohadar

master of fugue
Reaction score
209
And sometimes the easy solution is the best one...
There is no attachment system that can attach to integers and reals,
nor there ever be one.

See the problem is that they are not handles,
but what that actually means is that they have no identity.

You cannot attach to stuff that have no identity.

So what does this identity actually mean?

Difference 1:
Objects that have identity can be created/destroyed
objects that don't have identity simply exist.

You can create/destroy units, locations, triggers....
You cannot create number 5 can you? it simply exists.
You cannot destroy 3.14 can you.

Difference 2:
Objects that have identity are never equal.

Two units of same type, created at exact same location with exact same stats are still not equal. If you kill one the other will still be alive.

On the other hand two numbers 3 are always the same.
ALL numbers 3 in the world are equal.

Difference 3:
null values are not valid identities.
null unit is not a unit - it is simply null - invalid
But number zero is a valid number

You cannot attach to stuff that have no identity.
QED
 
Reaction score
456
>null unit is not a unit - it is simply null - invalid
Better to say null is nothing, it doesn't even exist?

>QED
?
 

Cohadar

master of fugue
Reaction score
209
null is not nothing, null exists.

QED = quod erat demonstrandum
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
I'm thinking about adding strings to the system, meaning you could store a value and instead of getting random integer setting a string as the pointer to the value. However, the current problem with this is that if give two values the same name, the first one given will always be returned, so I'm not sure I'm willing to put it in the system. I'm also hoping to improve a bit example trigger.

*Waiting for approval / disapproval.*
 

Doom-Angel

Jass User (Just started using NewGen)
Reaction score
167
i tried to read the read me but im not quite sure what is this system used for?
what do you mean by recycling?
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
This system is similiar to handle attachment systems (actually, it is one). Basically it let's you create as many indexes as you will, each index can hold up to 8192 values, each one with a unique array (pointer). When using that array you can restore the value attached. One of the main features of that system is that it recycles the index. This means that if you attached a value, and now you are done with it, the system will attach a new value in the old value's array, thus, it will recycle it's arrays making the 8192 limit almost impossible to reach unless you are doing something wrong.
 

Doom-Angel

Jass User (Just started using NewGen)
Reaction score
167
are the arrays actually a global one?
anyway im a bit confused (as usual) but i guess this could prove useful if i knew how to use it and when but i guess im just to lazy to start learning vJass :p
anyway +Rep
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
> are the arrays actually a global one?

Yes, they are global, otherwise you couldn't have restored their data.

> anyway im a bit confused

Feel free to ask anything! :)

> guess im just to lazy to start learning vJass

Now days Jass isn't worth much without vJass, if you learned Jass vJass would be easy to learn. I suggest making an effort and trying to learn it.
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
Well, I guess this is the same thing:
JASS:
//! textmacro recycleIndexes takes type, name
struct $name$
     private $type$ value 

     public static method create takes $type$ value returns $name$
          local $name$ n = .allocate()
          set n.value = value
          return n
     endmethod

     public method getValue takes nothing returns $type$
           return .value
     endmethod
endstruct
//! endtextmacro



Anyway, found one error:
JASS:
if recycle$name$[0] == $null$ then

If I read right then recycle$name$ is an integer array but $null$ can be of any type (well, any anything actually...).

Also, you don't really need recycle$name$ to be a first-in-first-out so you don't need the looping. Just pop the end of that array and use that as the "recycled" index.
 

Cohadar

master of fugue
Reaction score
209
and the whole story I gave you about identifiers has a little side note:
null is always null if you use it only for handles.

So you don't need that third null argument in your textmacro.
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
> If I read right then recycle$name$ is an integer array but $null$ can be of any type (well, any anything actually...).

That is why I suggest not recycling integers / reals, and as Cohadar said, anyway most handle attachment system don't support them.

> Also, you don't really need recycle$name$ to be a first-in-first-out so you don't need the looping. Just pop the end of that array and use that as the "recycled" index.

You got a point there. I guess I'll add it in v1.3. Thanks!
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
> If I read right then recycle$name$ is an integer array but $null$ can be of any type (well, any anything actually...).

That is why I suggest not recycling integers / reals, and as Cohadar said, anyway most handle attachment system don't support them.

That's not what I said. recycle$name$ is an integer array and there is no reason at all to compare it with $null$.
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Sorry I misunderstood you. I guess this means v1.4 will be coming up, thanks for spotting the problem.
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Actually Phyrex kinda found an easier way to do what I did. As for practicle example, check the example trigger in the map, it is basically handle attachment system, only instead of string you use integers, and instead of gamecache you use index that is created by using a textmacro.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top