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
963
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
963
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.

      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