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.
  • 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 The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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