Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
set this.next.prev = this.prev
set this.prev.next = this.next
JASS:
set this.next = 0
set this.prev = thistype(0).prev
set thistype(0).prev.next = this
set thistype(0).prev = this
I know they are used for adding instances to and removing instances from linked list, kinda make the spell MUI, right? But what exactly do they do? I don't wanna copy and paste them every time without knowing the meaning of them.
I am confused by the "next.prev, prev.next, this.next" thing..... :/
"prev" and "next" are fields of the same type as the struct, like this:
JASS:
struct MyStruct
MyStruct prev
MyStruct next
....
endstruct
They stand for "previous" and "next" and instances which are connected by this mechanism form a so called Linked List. This means there is a first element (doesnt really matter what it is) and this first element knows the second element in the list. The second element knows the third and so on...
So unlike an array you cannot for example access the 25. element directly, but you can start at the first element and iterate through the list until you reached the 25. element.
This might seem very complicated but it has some advantages: when you remove an element from a list it leaves an empty slot. If you however remove an element from a list you can simply tell the element previous to the one you removed that its next element is the element after the removed one. This means if you iterate through the list the list will simply skip the removed element.
Now to "next":
The field "next" of each object stores the member of the list which comes next.
So if you have elements A, B and C you do:
JASS:
set A.next = B
set B.next = C
then A knows that the following element is B and B knows the following element is C.
In addition to "next" there is also "prev", which tells every element which other element comes prior to that.
So if we use the example of A, B and C again to form a list A-B-C you now have to do:
JASS:
set A.next = B
set B.prev = A
set B.next = C
set C.prev = B
Now the List looks like this: "A B C".
To remove the element B from the list (so it looks like this: "A C") we have to do the following:
- Tell A that its next element is C
- Tell C that its previous element is A
in code this looks like this:
JASS:
set A.next = C
set C.prev = A
So far so good.
Now assume you write a method ".remove()" for the struct which implements this list functionality. This method should remove the current object from the list. Like this:
JASS:
struct MyStruct
MyStruct prev
MyStruct next
....
method remove takes nothing returns nothing
// remove this object from the list
endmethod
endstruct
Now we cannot just call "set A.next = C" because in this general case we dont know what A and B are. So we have to replace A and B by the objects previous and next element. We can access them with "this.prev" and "this.next".
So if we just take the above code and replace "A" and "B" by "this.prev" and "this.next" we end up with this:
JASS:
set this.prev.next = this.next
set this.next.prev = this.prev
So our "remove()" method would look like this:
JASS:
struct MyStruct
MyStruct prev
MyStruct next
....
method remove takes nothing returns nothing
set this.prev.next = this.next
set this.next.prev = this.prev
endmethod
endstruct
Il leave the other code from your question as an exercise...
@Nestharus:
Yea because using complicated libraries always helps when you try to understand basics.
"prev" and "next" are fields of the same type as the struct, like this:
JASS:
struct MyStruct
MyStruct prev
MyStruct next
....
endstruct
They stand for "previous" and "next" and instances which are connected by this mechanism form a so called Linked List. This means there is a first element (doesnt really matter what it is) and this first element knows the second element in the list. The second element knows the third and so on...
So unlike an array you cannot for example access the 25. element directly, but you can start at the first element and iterate through the list until you reached the 25. element.
This might seem very complicated but it has some advantages: when you remove an element from a list it leaves an empty slot. If you however remove an element from a list you can simply tell the element previous to the one you removed that its next element is the element after the removed one. This means if you iterate through the list the list will simply skip the removed element.
Now to "next":
The field "next" of each object stores the member of the list which comes next.
So if you have elements A, B and C you do:
JASS:
set A.next = B
set B.next = C
then A knows that the following element is B and B knows the following element is C.
In addition to "next" there is also "prev", which tells every element which other element comes prior to that.
So if we use the example of A, B and C again to form a list A-B-C you now have to do:
JASS:
set A.next = B
set B.prev = A
set B.next = C
set C.prev = B
Now the List looks like this: "A B C".
To remove the element B from the list (so it looks like this: "A C") we have to do the following:
- Tell A that its next element is C
- Tell C that its previous element is A
in code this looks like this:
JASS:
set A.next = C
set C.prev = A
So far so good.
Now assume you write a method ".remove()" for the struct which implements this list functionality. This method should remove the current object from the list. Like this:
JASS:
struct MyStruct
MyStruct prev
MyStruct next
....
method remove takes nothing returns nothing
// remove this object from the list
endmethod
endstruct
Now we cannot just call "set A.next = C" because in this general case we dont know what A and B are. So we have to replace A and B by the objects previous and next element. We can access them with "this.prev" and "this.next".
So if we just take the above code and replace "A" and "B" by "this.prev" and "this.next" we end up with this:
JASS:
set this.prev.next = this.next
set this.next.prev = this.prev
So our "remove()" method would look like this:
JASS:
struct MyStruct
MyStruct prev
MyStruct next
....
method remove takes nothing returns nothing
set this.prev.next = this.next
set this.next.prev = this.prev
endmethod
endstruct
Il leave the other code from your question as an exercise...
@Nestharus:
Yea because using complicated libraries always helps when you try to understand basics.
And @Gesh, don't use linked lists for MUI. That's not what they are for. All you need is simple allocation/deallocation on a stack.
If you would like to see how to use Alloc Alternative, look up how to allocate/deallocate a struct ^_^. If you are lost on using a module, look up how to use a module .
The difference between a double linked list and a stack isnt that big.
Using a resource you dont understand makes learning jass much impossible. Im not saying that the resource is hard to use or that he wont get good results using it, but it wont help him to understand.
In my opinion you only should use resources which you completely understand (and thus would be able to replace by something you wrote yourself). This applies to everyone, not only beginners.
@nes: I recall having this discussion with you before (not only once) but i wont just give up on my opinion.
And @Gesh, don't use linked lists for MUI. That's not what they are for. All you need is simple allocation/deallocation on a stack.
If you would like to see how to use Alloc Alternative, look up how to allocate/deallocate a struct ^_^. If you are lost on using a module, look up how to use a module .
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.