methods/struct extending array allocation, +rep for help

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
ok, so i know that in order to be able to allocate structs yourself they need to extend an array, and as i understand it this can be done with a method like such:
JASS:
    struct Example extends array
        static integer array r
        static integer m = 0

        static method allocate takes nothing returns thistype
            local thistype this
            if r[0]==0 then
                set this=m
                set m=m+1
            else
                set this=r[0]
                set r[0]=r[r[0]]
            endif
            return this
        endmethod

        method deallocate takes nothing returns nothing
            set r[this]=r[0]
            set r[0]=this
        endmethod
    endstruct


so how would i allocate each one? would it be with: [ljass]set integer = Example.allocate[/ljass]; or something like that?
 

dudeim

New Member
Reaction score
22
It would be something like this:
JASS:
function somerandomfunctiontouseyourstruct takes nothing returns nothing
local Example e = Example.create() //create automaticaly calls .allocate (i thought this was no different if you created allocate yourself but not 100% sure)
//now you can do stuff with your struct 
endfunction
//if the create method doesn't auto call .allocate if you create it yourself write a static create method in your struct that does the following
static method create takes nothing returns Example //this now returns an integer that is not yet assigned each time you call Example.create()
local Example e = Example.allocate()
return e
endmethod
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
and can Example.allocate take an integer to allocate that exact integer?
 

Ayanami

칼리
Reaction score
288
JASS:
    struct Example extends array
        static integer array r
        static integer m = 0

        static method allocate takes nothing returns thistype
            local thistype this
            if r[0]==0 then
                set this=m // this part is wrong, your set m = m + 1 line should be above this, you're setting this = 0 the first time, which shouldn't happen
                set m=m+1
            else
                set this=r[0]
                set r[0]=r[r[0]]
            endif
            return this
        endmethod

        method deallocate takes nothing returns nothing
            set r[this]=r[0]
            set r[0]=this
        endmethod
    endstruct

For structs that extend arrays, there is no default constructor from what I know. Thus, you'd need to call the [ljass].allocate[/ljass] directly. Or you can declare your custom constructor:

JASS:

struct Main extends array
    private static method allocate takes nothing returns thistype
        // code
    endmethod

    private method deallocate takes nothing returns thistype
        // code
    endmethod

    public static method create takes nothing returns thistype
        return thistype.allocate()
    endmethod
endstruct


And thus, you can use your custom constructor [ljass].create()[/ljass]. However, you could just use [ljass].allocate()[/ljass] directly.

By the way just asking, do you know what you're doing inside the allocation/deallocation method? Or did you just copy + paste it from somewhere else? o.o
 

Dirac

22710180
Reaction score
147
and can Example.allocate take an integer to allocate that exact integer?
You're confusing what allocation is.
Imagine that you have to store X amount of data inside an array named A, but you don't know how much X is, so you start storing data in A starting from 0 adding 1 everytime new data comes in
A[1]=data1
A[2]=data2
A[3]=data3
Imagine that A[2] no longers need to be read by any means and it's space can and should be reused for the next incoming data (data4) so it must be added to a recycle list.
A[2]=data4
What allocate returns is the index of the array A[X] and when it's deallocated it's added to a recycle list.

If you wish to use custom indexes for arrays instead of adding +1 i suggest you to not use an allocation or deallocation method whatsoever, just proceed to type [ljass]local thistype this = 36[/ljass] where 36 will be the index of the array
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
By the way just asking, do you know what you're doing inside the allocation/deallocation method? Or did you just copy + paste it from somewhere else? o.o

well if i am correct allocating is simply determining what integer to use next, and deallocating recycles that integer for later use, i used a similar method to recycle units in my SC2 Unit indexer

If you wish to use custom indexes for arrays instead of adding +1 i suggest you to not use an allocation or deallocation method whatsoever, just proceed to type [ljass]local thistype this = 36[/ljass] where 36 will be the index of the array

thank you, i was more unsure as to whether or not you could do this
as far as methods can you just call a method like normal or are they called upon creation/allocation of the array?

so could i allocate a specific number like this for example:

JASS:
struct Example2 extends array
    real SomeStat

    static method Allocate takes unit u returns thistype
        local thistype this = GetUnitUserData(u)
        return this
    endmethod
endstruct


to hook into a unit indexer that is

EDIT: oh wait i believe i would do that like this:
[ljass]local Example t = Example.Allocate(unit)[/ljass]
correct?
 

Dirac

22710180
Reaction score
147

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
ya because its not really allocating correct? just tethering an instance to a unit

what exactly is a method operator? do methods encapsule them?
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top