source: abuse/trunk/src/imlib/linked.cpp @ 534

Last change on this file since 534 was 534, checked in by Sam Hocevar, 12 years ago

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

File size: 2.0 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
[2]13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
[481]17#include "linked.h"
[2]18
[534]19linked_list::linked_list()
20  : m_first(NULL),
21    m_count(0)
[2]22{
[534]23    ;
[2]24}
25
[112]26//
[534]27// Clear all the nodes in a linked list and dispose of the
[112]28// memory for each one by calling its destructor
29//
[2]30linked_list::~linked_list()
31{
[534]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    }
[2]41}
42
[112]43//
[534]44// Take a node out of the linked list, but do not dispose of the memory
[112]45//
[534]46int linked_list::unlink(linked_node *p)
[2]47{
[534]48    if (!m_count)
49        return 0;
[2]50
[534]51    linked_node *q = m_first;
[2]52
[534]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;
[2]68}
69
[534]70//
71// Add a node to the end of a linked_list
72//
[2]73void linked_list::add_end(class linked_node *p)
74{
[534]75    if (m_first)
76        p->SetPrev(m_first->Prev());
77    else
78        m_first = p;
79    p->SetNext(m_first);
80
81    if (m_first != p)
82        m_first->Prev()->SetNext(p);
83    m_first->SetPrev(p);
84
85    m_count++;
[2]86}
87
[534]88// Add a node at the front of the list: just add it at the end and set
[2]89// the first pointer to it
90void linked_list::add_front(class linked_node *p)
91{
92    add_end(p);
[534]93    m_first = p;
[2]94}
95
Note: See TracBrowser for help on using the repository browser.