Can vJass structs have integer value 0?

krainert

Member
Reaction score
10
Question is in the title. Also, would anybody happen to know how exactly integer identifiers are assigned to structs?
 

luorax

Invasion in Duskwood
Reaction score
67
Also, would anybody happen to know how exactly integer identifiers are assigned to structs?

http://www.hiveworkshop.com/forums/...ls-280/coding-efficient-vjass-structs-187477/

JASS:
struct a
endstruct


JASS:
constant integer si__a=1
integer si__a_F=0
integer si__a_I=0
integer array si__a_V

//Generated allocator of a
function s__a__allocate takes nothing returns integer
 local integer this=si__a_F //first node on recycle stack
    if (this!=0) then
        set si__a_F=si__a_V[this] //set stack to next node (like stack.next)
    else
        set si__a_I=si__a_I+1
        set this=si__a_I
    endif
    if (this>8190) then //protection against too many structs
        return 0
    endif

    set si__a_V[this]=-1 //set stack to -1 (stack.next = -1)
 return this
endfunction

//Generated destructor of a
function s__a_deallocate takes integer this returns nothing
    if this==null then //don't deallocate null instance
        return
    elseif (si__a_V[this]!=-1) then //double free protection
        return
    endif
    set si__a_V[this]=si__a_F //set this.next = stack
    set si__a_F=this //set stack = this
endfunction

Can vJass structs have integer value 0?

Normal structs will never get the index of 0. However, if you use an array struct, then allocation/deallocation is up to you, so yes, you can use 0 too.
 

Dirac

22710180
Reaction score
147
It's quite simple actually, remember that structs are nothing but array systems so it looks the same to say
JASS:
struct Test
    integer x
endstruct
To
JASS:
globals
    private integer array Test_x
endglobals

Non array structs have an implemented default "allocation method" but it's kinda terrible. To learn more about struct allocation check out >Alloc< which is the faster, optimum way to allocate struct instances. You can always have your struct "extend array" to create your own personal allocation method if you need to use 0 as an initial variable. Here's an allocation method that uses 0 as initial value
JASS:
struct Tester 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
 

luorax

Invasion in Duskwood
Reaction score
67
It's not going to compile, since you can't use arrays inside array structs.
 

Dirac

22710180
Reaction score
147
It's not going to compile, since you can't use arrays inside array structs.
Yes you can, the only thing an array struct is different from a non array struct are the default allocation methods and the fact that you can't extend a struct to an array struct (which is really stupid)
 

luorax

Invasion in Duskwood
Reaction score
67
Nevermind, it's static. I missed that thing. Non-static array members are not allowed.
 

Nestharus

o-o
Reaction score
84
Always use 0 as null for structs. Never ever make it so a struct can possibly be 0 : P. There are reasons for this ; ).

If 0 isn't null, how do you check if a struct variable is null or not? You can't ; P.

In a linked list, how do you check when node.next or node.prev are null? You can't.

Always make 0 null and start your struct instances at 1.
 

Dirac

22710180
Reaction score
147
And they shouldn't anyways, the use of arrays inside structs would lead to 2d arrays, which are fail. Use hashtables instead.
 

WaterKnight

Member
Reaction score
7
If 0 isn't null, how do you check if a struct variable is null or not?.

By comparing it to -1 as suggested here?

In a linked list, how do you check when node.next or node.prev are null?

You can if you preallocate this -1 to where it belongs to. I guess you are referring to the default values of Wc3, game caches, hash tables and integer arrays returning 0 at unassigned keys.

Always make 0 null and start your struct instances at 1.

Suitable index allocation method depends on the situation. For example, if you want to bijectively connect instances of different structs, you do not even need a new index as you can take the one from the inputted struct.
 

Romek

Super Moderator
Reaction score
964
Integers are 0 by default, not -1. Using 0 as null means structs are null as default, and not whichever random instance happens to be the first one. Also this:
I guess you are referring to the default values of Wc3, game caches, hash tables and integer arrays returning 0 at unassigned keys.
That extra 1 instance isn't important. Just stick to using 0 as null!
 

krainert

Member
Reaction score
10
Integers are 0 by default, not -1. Using 0 as null means structs are null as default, and not whichever random instance happens to be the first one. Also this:
That extra 1 instance isn't important. Just stick to using 0 as null!

Semantically it then makes sense for me to use -1 as I need to represent the absence of a reference altogether and hence differentiate between my null value and the null value denoting a null struct. To elaborate, I'm building an interable hash map that's basically a dynamically sized map whose values are themselves arrays of size 1-3 such that i->(j,p,n) where j is the value mapped to i, p is the key of the preceding element, and n is key of the succeeding element for a key i. Hence, if an entry is the last in the chain, for example, it's n value will be -1, representing an absent struct reference.
 

Romek

Super Moderator
Reaction score
964
In that case, it's fine. I assumed this was for something more 'regular' like a spell. :p
 
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

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top