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

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

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

File size: 2.0 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
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
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include "linked.h"
18
19linked_list::linked_list()
20  : m_first(NULL),
21    m_count(0)
22{
23    ;
24}
25
26//
27// Clear all the nodes in a linked list and dispose of the
28// memory for each one by calling its destructor
29//
30linked_list::~linked_list()
31{
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    }
41}
42
43//
44// Take a node out of the linked list, but do not dispose of the memory
45//
46int linked_list::unlink(linked_node *p)
47{
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;
68}
69
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);
80
81    if (m_first != p)
82        m_first->Prev()->SetNext(p);
83    m_first->SetPrev(p);
84
85    m_count++;
86}
87
88// Add a node at the front of the list: just add it at the end and set
89// the first pointer to it
90void linked_list::add_front(class linked_node *p)
91{
92    add_end(p);
93    m_first = p;
94}
95
Note: See TracBrowser for help on using the repository browser.