dlist.cpp 4.34 KB
Newer Older
1
/*
2
 * This program source code file is part of KiCad, a free EDA CAD application.
3 4
 *
 * Copyright (C) 2008 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5
 * Copyright (C) 1992-2008 KiCad Developers, see change_log.txt for contributors.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */


26 27 28
#include <fctsys.h>
#include <dlist.h>
#include <base_struct.h>
29 30 31 32 33 34 35 36


/* Implement the class DHEAD from dlist.h */


DHEAD::~DHEAD()
{
    if( meOwner )
dickelbeck's avatar
dickelbeck committed
37
        DeleteAll();
38 39 40
}


dickelbeck's avatar
dickelbeck committed
41
void DHEAD::DeleteAll()
42
{
43 44
    EDA_ITEM* next;
    EDA_ITEM* item = first;
45 46 47 48 49 50 51 52 53 54 55 56 57 58

    while( item )
    {
        next = item->Next();
        delete item;            // virtual destructor, class specific
        item = next;
    }

    first = 0;
    last  = 0;
    count = 0;
}


59
void DHEAD::append( EDA_ITEM* aNewElement )
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
    wxASSERT( aNewElement != NULL );

    if( first )        // list is not empty, first is not touched
    {
        wxASSERT( last != NULL );

        aNewElement->SetNext( 0 );
        aNewElement->SetBack( last );

        last->SetNext( aNewElement );
        last = aNewElement;
    }
    else        // list is empty, first and last are changed
    {
        aNewElement->SetNext( 0 );
        aNewElement->SetBack( 0 );

        first = aNewElement;
        last  = aNewElement;
    }

    aNewElement->SetList( this );

    ++count;
}


88
void DHEAD::insert( EDA_ITEM* aNewElement, EDA_ITEM* aAfterMe )
89 90 91 92
{
    wxASSERT( aNewElement != NULL );

    if( !aAfterMe )
dickelbeck's avatar
dickelbeck committed
93
        append( aNewElement );
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    else
    {
        wxASSERT( aAfterMe->GetList() == this );

        // the list cannot be empty if aAfterMe is supposedly on the list
        wxASSERT( first && last );

        if( first == aAfterMe )
        {
            aAfterMe->SetBack( aNewElement );

            aNewElement->SetBack( 0 );  // first in list does not point back
            aNewElement->SetNext( aAfterMe );

            first = aNewElement;
        }
        else
        {
112
            EDA_ITEM* oldBack = aAfterMe->Back();
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

            aAfterMe->SetBack( aNewElement );

            aNewElement->SetBack( oldBack );
            aNewElement->SetNext( aAfterMe );

            oldBack->SetNext( aNewElement );
        }

        aNewElement->SetList( this );

        ++count;
    }
}


129
void DHEAD::remove( EDA_ITEM* aElement )
130
{
131
    wxASSERT( aElement );
132 133 134 135 136 137
    wxASSERT( aElement->GetList() == this );

    if( aElement->Next() )
    {
        aElement->Next()->SetBack( aElement->Back() );
    }
dickelbeck's avatar
dickelbeck committed
138
    else  // element being removed is last
139 140 141 142 143 144 145
    {
        wxASSERT( last == aElement );
        last = aElement->Back();
    }

    if( aElement->Back() )
    {
dickelbeck's avatar
dickelbeck committed
146
        aElement->Back()->SetNext( aElement->Next() );
147
    }
dickelbeck's avatar
dickelbeck committed
148
    else    // element being removed is first
149 150 151 152 153 154 155 156 157 158 159 160
    {
        wxASSERT( first == aElement );
        first = aElement->Next();
    }

    aElement->SetBack( 0 );
    aElement->SetNext( 0 );
    aElement->SetList( 0 );

    --count;
}

dickelbeck's avatar
dickelbeck committed
161 162 163 164
#if defined(DEBUG)

void DHEAD::VerifyListIntegrity()
{
165
    EDA_ITEM* item;
dickelbeck's avatar
dickelbeck committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    unsigned i = 0;

    for( item = first;  item && i<count;  ++i, item = item->Next() )
    {
        if( i < count-1 )
        {
            wxASSERT( item->Next() );
        }

        wxASSERT( item->GetList() == this );
    }

    wxASSERT( item == NULL );
    wxASSERT( i == count );

    i = 0;
    for( item = last;  item && i<count;  ++i, item = item->Back() )
    {
        if( i < count-1 )
        {
            wxASSERT( item->Back() );
        }
    }

    wxASSERT( item == NULL );
    wxASSERT( i == count );
192 193

    // printf("list %p has %d items.\n", this, count );
dickelbeck's avatar
dickelbeck committed
194 195 196 197
}

#endif