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?
 
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
 
and can Example.allocate take an integer to allocate that exact integer?
 
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
 
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
 
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?
 
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 The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/
  • The Helper The Helper:
    Happy Thursday!
  • Ghan Ghan:
    I'm not so sure, there's still a lot of Thursday left for it to become unhappy.
    +3
  • The Helper The Helper:
    still a few hours
  • The Helper The Helper:
    Its Friday!!!!
    +1
  • Ghan Ghan:
    Thursday 7/10 would Thursday again
    +3

      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