Помогите, пожалуйста, найти где тут ошибка. Вообще никак не могу понять, как перебирать данные в структуре используя переменные 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
30
7 лет назад
Отредактирован Clamp
0
Пример использования
struct SpawnLink {
    public int length;

    private Spawn attach;
    private thistype prev, next;

    public float operator x()      { return this.attach.x; }
    public float operator y()      { return this.attach.y; }
    public float operator charge() { return this.attach.charge; }

    private static thistype create(float x, float y, player owner, int level, int length, thistype prev) {
        thistype this = thistype.allocate();
        this.prev = prev;
        this.next = 0;
        this.attach = Spawn.create(x, y, owner, level);
        this.length = length;
        this.changeLength(1);
        return this;
    }

    private void destroy() {
        if (this.prev != 0) && (this.next != 0) {
            this.prev.next = this.next;
            this.next.prev = this.prev;
        } else {
            this.prev.next = 0;
            this.next.prev = 0;
        }
        this.attach.destroy();
        this.deallocate();
    }

    public thistype add(float x, float y, player owner, int level) {
        if (this == 0) {
            return thistype.create(x, y, owner, level, 0, 0);
        }
        this.next = thistype.create(x, y, owner, level, this.length, this);
        return this.next;
    }

    public thistype remove(thistype toRemove) {
        thistype temp = this;
        if (this == toRemove) {
            temp = this.prev;
        }
        this.changeLength(-1);
        toRemove.destroy();
        return temp;
    }

    public void removeFirst() {
        thistype iterated = this;
        while (iterated.prev != 0) {
            iterated = iterated.prev;
        }
        this.changeLength(-1);
        iterated.destroy();
    }

    public void removeAll() {
        thistype temp, iterated = this;
        while (iterated != 0) {
            temp = iterated.prev;
            iterated.destroy();
            iterated = temp;
        }
    }

    public thistype getNearest(float x, float y) {
        thistype nearest = this, iterated = this.prev;
        float tempDistSqr, nearestDistSqr = this.attach.distToSqr(x, y);
        while (iterated != 0) {
            tempDistSqr = iterated.attach.distToSqr(x, y);
            if (nearestDistSqr > tempDistSqr) {
                nearestDistSqr = tempDistSqr;
                nearest = iterated;
            }
            iterated = iterated.prev;
        }
        return nearest;
    }

    public unit getUnit() {
        return this.attach.getUnit();
    }

    private void changeLength(int howMuch) {
        thistype iterated = this;
        while (iterated != 0) {
            iterated.length += howMuch;
            iterated = iterated.prev;
        }
    }

    public void setLevel(int level) {
        thistype iterated = this;
        while (iterated != 0) {
            iterated.attach.setLevel(level);
            iterated = iterated.prev;
        }
    }
}
0
32
7 лет назад
0
Ну это пример как надо юзать структуры под множество обьектов, тут смеца цвета, но чаще движение молний между юнитами и так далее..
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.. готовые давно за нас сделали чем изобретать велосипеды
Чтобы оставить комментарий, пожалуйста, войдите на сайт.