Помогите, пожалуйста, найти где тут ошибка. Вообще никак не могу понять, как перебирать данные в структуре используя переменные thistype.next, thistype.prev.
code
struct data
        static timer    period = CreateTimer( )
               thistype prev
               thistype next

               unit     u
               integer  a

        method destroy takes nothing returns nothing
            if ( this.next != 0 ) then
                set this.next.prev = this.prev
            endif
                
            set this.prev.next = this.next
            set this.prev = thistype(0).prev
            set thistype(0).prev = this
            
            if ( thistype(0).next == 0 ) then
                call PauseTimer( period )
            endif

            call thistype.deallocate( this )
        endmethod

        static method iterate takes nothing returns nothing
            local thistype this = thistype( 0 )
            
            loop
                set this = this.next
                exitwhen ( this == 0 )
                set this.a = this.a + 1
                call SetUnitVertexColor( this.u, 255, 255, 255, this.a )
                if ( this.a >= 255 ) then
                    call this.destroy( )
                endif
            endloop
        endmethod

        static method createUnit takes nothing returns thistype
            local thistype this = thistype.allocate( )

            set this.next = thistype(0).next
            set thistype(0).next = this
            set this.prev = thistype(0)

            set this.u = CreateUnit( Player(0), 'hfoo', 0.0, 0.0, 0.0 )
            set this.a = 0
            call SetUnitVertexColor( this.u, 255, 255, 255, 0 )

            if thistype(0).next == 0 then
                call TimerStart( period, 0.03125, true, function thistype.iterate )
            else
                set thistype(0).next.prev = this
            endif

            return this
        endmethod

    endstruct

Принятый ответ

quq_CCCP, благодарю, но всё же хотелось бы именно с thistype.next, thistype.prev.
кажется, я сделаль:
struct data
        static timer    period = CreateTimer( )
               thistype prev
               thistype next

               unit     u
               integer  a

        method destroy takes nothing returns nothing
            set this.prev.next = this.next
            set this.next.prev = this.prev

            if ( thistype(0).next == 0 ) then
                call PauseTimer( period )
            endif

            call thistype.deallocate( this )
        endmethod

        static method iterate takes nothing returns nothing
            local thistype this = thistype( 0 ).next

            loop
                exitwhen ( this == 0 )

				set this.a = this.a + 1
				call SetUnitVortexColor( this.u, 255, 255, 255, this.a )

                if ( this.a > 255 ) then
                    call this.destroy( )
                endif

                set this = this.next
            endloop
        endmethod

        static method createUnit takes nothing returns thistype
            local thistype this = thistype.allocate( )

            set this.next = 0
            set this.prev = thistype(0).prev
            set this.next.prev = this
            set this.prev.next = this

			set this.u = CreateUnit(...)
			set this.a = 0
			call SetUnitVortexColor( this.u, 255, 255, 255, this.a )
            
            if ( this.prev == 0 ) then
                call TimerStart( period, 0.03125, true, function thistype.iterate )
            endif

            return this
        endmethod

    endstruct

Показан только небольшой набор комментариев вокруг указанного. Перейти к актуальным.
0
21
7 лет назад
0
quq_CCCP, да, это я сам написал, но она не работает, потому что я не знаю как написать правильно используя next, prev
0
30
7 лет назад
0
Паттерн называется "двусвязный список", если что.
0
21
7 лет назад
0
Clamp, Ваш код, к сожалению, я совсем не понял. Мне бы простой код, такой, какой я сам выложил наверху.
0
32
7 лет назад
0
ScopteRectuS, вот тут есть пример перебора структуры с пояснениями ссылка
И вот тут ссылка.
Но особо увлекаться этим не вижу смысла, ну только если у вас не 300+ объектов для перебора..
0
21
7 лет назад
0
quq_CCCP, благодарю, но всё же хотелось бы именно с thistype.next, thistype.prev.
кажется, я сделаль:
struct data
        static timer    period = CreateTimer( )
               thistype prev
               thistype next

               unit     u
               integer  a

        method destroy takes nothing returns nothing
            set this.prev.next = this.next
            set this.next.prev = this.prev

            if ( thistype(0).next == 0 ) then
                call PauseTimer( period )
            endif

            call thistype.deallocate( this )
        endmethod

        static method iterate takes nothing returns nothing
            local thistype this = thistype( 0 ).next

            loop
                exitwhen ( this == 0 )

				set this.a = this.a + 1
				call SetUnitVortexColor( this.u, 255, 255, 255, this.a )

                if ( this.a > 255 ) then
                    call this.destroy( )
                endif

                set this = this.next
            endloop
        endmethod

        static method createUnit takes nothing returns thistype
            local thistype this = thistype.allocate( )

            set this.next = 0
            set this.prev = thistype(0).prev
            set this.next.prev = this
            set this.prev.next = this

			set this.u = CreateUnit(...)
			set this.a = 0
			call SetUnitVortexColor( this.u, 255, 255, 255, this.a )
            
            if ( this.prev == 0 ) then
                call TimerStart( period, 0.03125, true, function thistype.iterate )
            endif

            return this
        endmethod

    endstruct
Принятый ответ
0
30
7 лет назад
0
если у вас не 300+ объектов для перебора
Использовать список практически нецелесообразно.
0
23
7 лет назад
0
а чем не угодил готовый библиотека table.. готовые давно за нас сделали чем изобретать велосипеды
Показан только небольшой набор комментариев вокруг указанного. Перейти к актуальным.
Чтобы оставить комментарий, пожалуйста, войдите на сайт.