Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SG_DList.h « SceneGraph « gameengine « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b82e51e0d2f8b411714cbfc11768d349146cfdb6 (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
162
163
164
165
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file SG_DList.h
 *  \ingroup bgesg
 */
 
#ifndef __SG_DLIST
#define __SG_DLIST

#include <stdlib.h>

#ifdef WITH_CXX_GUARDEDALLOC
#include "MEM_guardedalloc.h"
#endif

/**
 * Double circular linked list
 */
class SG_DList
{
protected :
	SG_DList* m_flink;
	SG_DList* m_blink;

public:
	template<typename T> class iterator
	{
	private:
		SG_DList&	m_head;
		T*			m_current;
	public:
		typedef iterator<T> _myT;
		iterator(SG_DList& head) : m_head(head), m_current(NULL) {}
		~iterator() {}

		void begin()
		{
			m_current = (T*)m_head.Peek();
		}
		void back()
		{
			m_current = (T*)m_head.Back();
		}
		bool end()
		{
			return (m_current == (T*)m_head.Self());
		}
		bool add_back(T* item)
		{
			return m_current->AddBack(item);
		}
		T* operator*()
		{
			return m_current;
		}
		_myT& operator++()
		{
			// no check of NULL! make sure you don't try to increment beyond end
			m_current = (T*)m_current->Peek();
			return *this;
		}
		_myT& operator--()
		{
			// no check of NULL! make sure you don't try to increment beyond end
			m_current = (T*)m_current->Back();
			return *this;
		}
	};

	template<typename T> class const_iterator
	{
	private:
		const SG_DList&	m_head;
		const T*		m_current;
	public:
		typedef const_iterator<T> _myT;
		const_iterator(const SG_DList& head) : m_head(head), m_current(NULL) {}
		~const_iterator() {}

		void begin()
		{
			m_current = (const T*)m_head.Peek();
		}
		void back()
		{
			m_current = (const T*)m_head.Back();
		}
		bool end()
		{
			return (m_current == (const T*)m_head.Self());
		}
		const T* operator*()
		{
			return m_current;
		}
		_myT& operator++()
		{
			// no check of NULL! make sure you don't try to increment beyond end
			m_current = (const T*)m_current->Peek();
			return *this;
		}
		_myT& operator--()
		{
			// no check of NULL! make sure you don't try to increment beyond end
			m_current = (const T*)m_current->Back();
			return *this;
		}
	};

    SG_DList() 
    { 
        m_flink = m_blink = this; 
    }
	SG_DList(const SG_DList& other)
	{
        m_flink = m_blink = this; 
	}
    virtual ~SG_DList() 
    {
		Delink();
    }

    inline bool Empty()               // Check for empty queue
    {     
        return ( m_flink == this ); 
    }
    bool AddBack( SG_DList *item )  // Add to the back
    {
		if (!item->Empty())
			return false;
        item->m_blink = m_blink;
        item->m_flink = this;
        m_blink->m_flink = item;
        m_blink = item;
		return true;
    }
    bool AddFront( SG_DList *item )  // Add to the back
    {
		if (!item->Empty())
			return false;
        item->m_flink = m_flink;
        item->m_blink = this;
        m_flink->m_blink = item;
        m_flink = item;
		return true;
    }
    SG_DList *Remove()           // Remove from the front
    {
        if (Empty()) 
        {
            return NULL;
        }
        SG_DList* item = m_flink;
        m_flink = item->m_flink;
        m_flink->m_blink = this;
        item->m_flink = item->m_blink = item;
        return item;
    }
    bool Delink()             // Remove from the middle
    {
		if (Empty())
			return false;
		m_blink->m_flink = m_flink;
		m_flink->m_blink = m_blink;
		m_flink = m_blink = this;
		return true;
    }
    inline SG_DList *Peek()			// Look at front without removing
    { 
        return m_flink; 
    }  
    inline SG_DList *Back()			// Look at front without removing
    { 
        return m_blink; 
    }  
    inline SG_DList *Self() 
    { 
        return this; 
    }
    inline const SG_DList *Peek() const			// Look at front without removing
    { 
        return (const SG_DList*)m_flink; 
    }  
    inline const SG_DList *Back() const			// Look at front without removing
    { 
        return (const SG_DList*)m_blink; 
    }  
    inline const SG_DList *Self() const 
    { 
        return this; 
    }
	
	
#ifdef WITH_CXX_GUARDEDALLOC
public:
	void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:SG_DList"); }
	void operator delete( void *mem ) { MEM_freeN(mem); }
#endif
};

/**
 * SG_DListHead : Template class that implements copy constructor to duplicate list automatically
 *                The elements of the list must have themselves a copy constructor.
 */
template<typename T> class SG_DListHead : public SG_DList
{
public:
	typedef SG_DListHead<T> _myT;
	SG_DListHead() : SG_DList() {}
	SG_DListHead(const _myT& other) : SG_DList()
	{
		// copy the list, assuming objects of type T
		const_iterator<T> eit(other);
		T* elem;
		for (eit.begin(); !eit.end(); ++eit) {
			elem = (*eit)->GetReplica();
			AddBack(elem);
		}
	}
	virtual ~SG_DListHead() {}
    T* Remove()
    {
		return static_cast<T*>(SG_DList::Remove());
    }

};

#endif //__SG_DLIST