Ignore:
Timestamp:
Apr 22, 2011, 7:32:17 PM (11 years ago)
Author:
Sam Hocevar
Message:

imlib: remove a lot of dead code, especially from the linked list and
the WAV reader.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/src/imlib/linked.cpp

    r494 r534  
    1717#include "linked.h"
    1818
    19 //
    20 // take a node out of the linked list, but do not dispose of the memory
    21 //
    22 int linked_list::unlink(linked_node *p)
     19linked_list::linked_list()
     20  : m_first(NULL),
     21    m_count(0)
    2322{
    24   linked_node *q;
    25   if (first())        // if there are any nodes..
    26   {
    27     if (first()==p)  // if they want to unlinkt the first node
    28     {
    29       fn->last()->set_next(fn->next());   // set the first's last's next to the first's next
    30       fn->next()->set_last(fn->last());    // set the first next's last to the first last
    31       if (fn->next()==fn)                   // if there is ony one node
    32       { fn=NULL; cn=NULL; }                   // clear the list
    33       else fn=p->next();                  // else point the first pointer to the next node
    34       nn--;                              // decrement the number of nodes in the list
    35       return 1;
    36     }
    37     else
    38     {
    39       q=first()->next();
    40       while (q!=p && q!=first())    // find the node in the list
    41     q=q->next();
    42       if (q!=first())                  // is it in the list at all?
    43       {  q->last()->set_next(q->next());   // yes unlink the pointers
    44      q->next()->set_last(q->last());
    45      nn--;
    46       return 1;
    47       }                                    // decrement the number of nodes
    48     }
    49   }
    50   return 0;
     23    ;
    5124}
    5225
    53 
    5426//
    55 // clear all the nodes in a linked list and dispose of the
     27// Clear all the nodes in a linked list and dispose of the
    5628// memory for each one by calling its destructor
    5729//
    5830linked_list::~linked_list()
    5931{
    60   if (fn)
    61     fn->last()->set_next(NULL);  // set the last nodes next to NULL
    62                                  // so we can go until we hit NULL
    63   while (fn != NULL)          // repeat until we get to that node
    64   {
    65     cn=fn->next();
    66     delete fn;                // delete the old node
    67     fn=cn;
    68   }
    69   cn=NULL;                   // clear the list
    70   nn=0;                     // set the number of nodes to 0
     32    if (m_first)
     33        m_first->Prev()->SetNext(NULL); // set the prev nodes next to NULL
     34                                        // so we can go until we hit NULL
     35    while (m_first != NULL)
     36    {
     37        linked_node *tmp = m_first->Next();
     38        delete m_first;
     39        m_first = tmp;
     40    }
    7141}
    7242
    7343//
    74 // return the index of a given node in a linked list, by starting at the
    75 // node and parsing backwards
     44// Take a node out of the linked list, but do not dispose of the memory
    7645//
    77 int linked_list::node_number(linked_node *p)
     46int linked_list::unlink(linked_node *p)
    7847{
    79   int x;
    80   x=1;
    81   while (p!=first())
    82   { x++; p=p->last(); }
    83   return x;
     48    if (!m_count)
     49        return 0;
     50
     51    linked_node *q = m_first;
     52
     53    while (q != p) // find the node in the list
     54    {
     55        q = q->Next();
     56        if (q == m_first)
     57            return 0; // not in the list!
     58    }
     59
     60    q->Prev()->SetNext(q->Next());
     61    q->Next()->SetPrev(q->Prev());
     62
     63    if (p == m_first) // if they want to unlink the first node
     64        m_first = (m_first->Next() == m_first) ? NULL : p->Next();
     65
     66    m_count--;
     67    return 1;
    8468}
    8569
     70//
     71// Add a node to the end of a linked_list
     72//
     73void linked_list::add_end(class linked_node *p)
     74{
     75    if (m_first)
     76        p->SetPrev(m_first->Prev());
     77    else
     78        m_first = p;
     79    p->SetNext(m_first);
    8680
    87 // this function returns a pointer to the xth node
    88 class linked_node *linked_list::get_node(int x)
    89 {
    90   class linked_node *p;
    91   p=fn;             // start tat the first node
    92   while (x--!=1) p=p->next();  // go forward X-1 nodes
    93   return p;
     81    if (m_first != p)
     82        m_first->Prev()->SetNext(p);
     83    m_first->SetPrev(p);
     84
     85    m_count++;
    9486}
    9587
    96 
    97 // this function adds a node to the end of a linked_list
    98 void linked_list::add_end(class linked_node *p)
    99 {
    100   nn++;
    101   if (fn==NULL)        // if there are no nodes, then this one becomes the first
    102   { fn=p;
    103     fn->set_next(fn);  // and it poits to itself for first and last
    104     fn->set_last(fn);
    105   }
    106   else
    107   {
    108     p->set_last(fn->last());  // otherwise add it in at the end
    109     p->set_next(fn);
    110     fn->last()->set_next(p);
    111     fn->set_last(p);          // the first's last pointer points to the node we just added
    112   }
    113 }
    114 
    115 
    116 // to add a node at the fron of the list, just add it at the end and set
     88// Add a node at the front of the list: just add it at the end and set
    11789// the first pointer to it
    11890void linked_list::add_front(class linked_node *p)
    11991{
    120   add_end(p);
    121   fn=p;
     92    add_end(p);
     93    m_first = p;
    12294}
    12395
    124 
    125 // insert adds a node in the list according to is sort value
    126 void linked_list::insert(class linked_node *p)
    127 {
    128   class linked_node *q;
    129 
    130   // if there are nodes, or it belongs at the beginin call add front
    131   if ((fn==NULL) || (p->compare(fn,sortby)>0))
    132     add_front(p);
    133   // else if it goes at the ned call add_end
    134   else if (p->compare(fn->last(),sortby)<=0)
    135     add_end(p);
    136   else      // otherwise we have to find the right spot for it.
    137   {
    138     nn++;
    139     q=fn;
    140     while (q!=fn->last())  // q starts at the front
    141     { q=q->next();
    142       if (p->compare(q,sortby)>0)  // repeat until we find a value greater than the one we are inserting
    143       { p->set_next(q);
    144     p->set_last(q->last());     // insert it with pointers here
    145     q->last()->set_next(p);
    146     q->set_last(p);
    147     q=fn->last();         // set q to the last node so we finish the loop
    148       }
    149     }
    150   }
    151 }
    152 
    153 linked_list::linked_list(linked_node *first)
    154 { linked_node *prev;
    155   fn=first; cn=first; sortby=1; nn=0;
    156   if (first)
    157   { cn=first;
    158     do
    159     {
    160       nn++;
    161       prev=cn;
    162       cn=cn->next();
    163     } while (cn && cn!=first);
    164     if (cn==NULL)
    165     {
    166       fn->set_last(prev);
    167       prev->set_next(fn);
    168     }
    169     cn=first;
    170 
    171   }
    172 }
Note: See TracChangeset for help on using the changeset viewer.