summaryrefslogtreecommitdiff
path: root/thirdparty/vhacd/inc/vhacdCircularList.inl
blob: 2be5180524e2a15a36db05d9d8047c69e7fc0d86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#pragma once
#ifndef HACD_CIRCULAR_LIST_INL
#define HACD_CIRCULAR_LIST_INL
namespace VHACD
{
    template < typename T > 
    inline bool CircularList<T>::Delete(CircularListElement<T> * element)
    {
        if (!element)
        {
            return false;
        }
        if (m_size > 1)
        {
            CircularListElement<T> * next = element->GetNext();
            CircularListElement<T> * prev = element->GetPrev();
            delete element;
            m_size--;
            if (element == m_head)
            {
                m_head = next;
            }
            next->GetPrev() = prev;
            prev->GetNext() = next;
            return true;
        }
        else if (m_size == 1)
        {
            delete m_head;
            m_size--;
            m_head = 0;
            return true;
        }
        else
        {
            return false;
        }
    }
    
    template < typename T > 
    inline bool CircularList<T>::Delete()
    {
        if (m_size > 1)
        {
            CircularListElement<T> * next = m_head->GetNext();
            CircularListElement<T> * prev = m_head->GetPrev();
            delete m_head;
            m_size--;
            m_head = next;
            next->GetPrev() = prev;
            prev->GetNext() = next;
            return true;
        }
        else if (m_size == 1)
        {
            delete m_head;
            m_size--;
            m_head = 0;
            return true;
        }
        else
        {
            return false;
        }
    }
    template < typename T > 
    inline CircularListElement<T> * CircularList<T>::Add(const T * data)
    {
        if (m_size == 0)
        {
            if (data)
            {
                m_head = new CircularListElement<T>(*data);
            }
            else
            {
                m_head = new CircularListElement<T>();
            }
            m_head->GetNext() = m_head->GetPrev() = m_head;
        }
        else
        {
            CircularListElement<T> * next = m_head->GetNext();
            CircularListElement<T> * element = m_head;
            if (data)
            {
                m_head = new CircularListElement<T>(*data);
            }
            else
            {
                m_head = new CircularListElement<T>();
            }
            m_head->GetNext() = next;
            m_head->GetPrev() = element;
            element->GetNext() = m_head;
            next->GetPrev() = m_head;
        }
        m_size++;
        return m_head;
    }
    template < typename T > 
    inline CircularListElement<T> * CircularList<T>::Add(const T & data)
    {
        const T * pData = &data;
        return Add(pData);
    }
    template < typename T > 
    inline bool CircularList<T>::Next()
    {
        if (m_size == 0)
        {
            return false;
        }
        m_head = m_head->GetNext();
        return true;
    }
    template < typename T > 
    inline bool CircularList<T>::Prev()
    {
        if (m_size == 0)
        {
            return false;
        }
        m_head = m_head->GetPrev();
        return true;
    }
    template < typename T > 
    inline CircularList<T>::CircularList(const CircularList& rhs)
    {
        if (rhs.m_size > 0)
        {
            CircularListElement<T> * current = rhs.m_head;
            do
            {
                current = current->GetNext();
                Add(current->GetData());
            }
            while ( current != rhs.m_head );
        }
    }
    template < typename T > 
    inline const CircularList<T>& CircularList<T>::operator=(const CircularList& rhs)
    {
        if (&rhs != this)
        {
            Clear();
            if (rhs.m_size > 0)
            {
                CircularListElement<T> * current = rhs.m_head;
                do
                {
                    current = current->GetNext();
                    Add(current->GetData());
                }
                while ( current != rhs.m_head );
            }
        }
        return (*this);
    }
}
#endif