summaryrefslogtreecommitdiff
path: root/utils/List.h
blob: def2919cb125687f5400805c5e00489ad134488e (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
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1992-1995
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	plain and generic lists (original ideas by Yves Berteaud)
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef List_H_
#define List_H_

#include "cplus_bugs.h"
#include <sys/types.h>

class IvlAllocator;

typedef void IvlListItem;
typedef void* IvlListIndex;

#ifdef CPLUS_BUG20
class IvlListLink;
#endif

class IvlList {
friend	class	IvlListIter;

#ifndef CPLUS_BUG20
	class IvlListLink {
	static	IvlAllocator*	ListLinkMem;
	public:
		IvlListItem*	Entry;
		IvlListLink*	Next;
	inline		IvlListLink (IvlListItem* e, IvlListLink* n = 0)	{ Entry = e; Next = n ? n : this; }
		IvlListLink*	Previous ();
		void*	operator new (size_t);
		void	operator delete (void*);
	};
#endif

public:
	enum	list_status	{ NoError, WasEmpty, TooEarly, TooFar, BadIterator };
	enum	list_remove	{ All = -1 };

private:

	IvlListLink*	LastLink;
	list_status	StatusFlag;

	IvlListIndex	InsertAfterLink (IvlListLink*, IvlListItem*);
	IvlListIndex	InsertBeforeLink (IvlListLink*, IvlListItem*);
	IvlListItem*	RemoveAfterLink (IvlListLink*);

public:
inline		IvlList () : LastLink (0), StatusFlag (NoError)	{ }
		IvlList (IvlListItem*);
		IvlList (const IvlList&);
		~IvlList ();
		IvlList&	operator = (const IvlList&);
inline	list_status	GetStatus () const		{ return StatusFlag; }

inline	int	IsEmpty () const			{ return !LastLink; }
	IvlListItem*	First ();
	IvlListItem*	Last ();
	IvlListItem*	Nth (int n);
	int	Length () const;
	int	Find (IvlListItem*, int* = 0) const;

	IvlListIndex	Append (IvlListItem*);
	IvlListIndex	Prepend (IvlListItem*);
inline	IvlList&	operator << (IvlListItem* it)	{ Append (it); return *this; }

	IvlListItem*	RemoveFirst ();
	IvlListItem*	RemoveLast ();
	int	Remove (IvlListItem*, int = 1);
	int	Remove (int (*) (IvlListItem*), int = 1);
	void	Clear ();

	IvlListIndex	InsertAfter (const IvlListIter&, IvlListItem*);
	IvlListIndex	InsertBefore (const IvlListIter&, IvlListItem*);
	IvlListItem*	RemoveAfter (const IvlListIter&);
};

class IvlListIter {
friend	class	IvlList;

public:
	enum	listiter_status	{ Normal, StartOfList, EndOfList };

private:
	const IvlList*	TheList;
#ifdef CPLUS_BUG20
	IvlListLink*	CurLink;
#else
	IvlList::IvlListLink*	CurLink;
#endif
	listiter_status	StatusFlag;

public:
inline		IvlListIter (const IvlList& l) : TheList (&l), CurLink (0), StatusFlag (StartOfList)	{ }
		IvlListIter (const IvlList&, IvlListIndex);
inline	void	Reset ()		{ CurLink = 0; StatusFlag = StartOfList; }
inline	IvlListIter&	operator = (const IvlList& l)	{ TheList = &l; CurLink = 0; StatusFlag = StartOfList; return *this; }
inline	IvlListIter&	operator = (const IvlListIter& li)	{ TheList = li.TheList; CurLink = li.CurLink; StatusFlag = li.StatusFlag; return *this; }
	IvlListIter&	operator ++ ();
#ifndef CPLUS_BUG16
	IvlListIter&	operator ++ (int);
#endif
	IvlListItem*	operator * () const;
		int	Find (IvlListItem*);
inline	listiter_status	GetStatus () const		{ return StatusFlag; }
inline	operator int () const	{ return StatusFlag == Normal; }
};

#ifndef CPLUS_BUG19
template <class ITEM> class IvlListIterOf;

template <class ITEM> class IvlListOf : public IvlList {
public:
inline		IvlListOf () : IvlList ()	{}
inline		IvlListOf (ITEM* it) : IvlList (it)	{}
inline	ITEM*	First ()	{ return (ITEM*) (IvlList::First ()); }
inline	ITEM*	Last ()	{ return (ITEM*) (IvlList::Last ()); }
inline	ITEM*	Nth (int n)	{ return (ITEM*) (IvlList::Nth (n)); }
inline	int	Find (ITEM* it, int* r = 0) const	{ return IvlList::Find (it, r); }

inline	IvlListIndex	Append (ITEM* it)	{ return IvlList::Append (it); }
inline	IvlListIndex	Prepend (ITEM* it)	{ return IvlList::Prepend (it); }
inline	IvlListOf<ITEM>&	operator << (ITEM* it)	{ IvlList::Append (it); return *this; }

inline	ITEM*	RemoveFirst ()	{ return (ITEM*) (IvlList::RemoveFirst ()); }
inline	ITEM*	RemoveLast ()	{ return (ITEM*) (IvlList::RemoveLast ()); }
inline	int	Remove (ITEM* it, int nb = 1)	{ return IvlList::Remove (it, nb); }
inline	int	Remove (int (*p) (ITEM*), int nb = 1)	{ return IvlList::Remove ((int (*) (void*)) p, nb); }

inline	IvlListIndex	InsertAfter (const IvlListIterOf <ITEM>& li, ITEM*it)	{ return IvlList::InsertAfter (li, it); }
inline	IvlListIndex	InsertBefore (const IvlListIterOf <ITEM>& li, ITEM* it)	{ return IvlList::InsertBefore (li, it); }
inline	ITEM*	RemoveAfter (const IvlListIterOf <ITEM>& li)		{ return (ITEM*) IvlList::RemoveAfter (li); }
};

template <class ITEM> class IvlListIterOf : public IvlListIter {
public:
inline		IvlListIterOf (const IvlListOf <ITEM>& l) : IvlListIter (l)	{ }
inline	IvlListIterOf<ITEM>&	operator =  (const IvlListOf <ITEM>& l)	{ return (IvlListIterOf <ITEM>&) IvlListIter::operator = (l); }
inline	IvlListIterOf<ITEM>&	operator =  (const IvlListIterOf <ITEM>& li)	{ return (IvlListIterOf <ITEM>&) IvlListIter::operator = (li); }
inline	IvlListIterOf<ITEM>&	operator ++ ()	{ return (IvlListIterOf <ITEM>&) IvlListIter::operator ++ (); }
inline	ITEM*	operator * () const	{ return (ITEM*) IvlListIter::operator * (); }
inline	int	Find (ITEM* it)		{ return IvlListIter::Find (it); }
};
#endif	/* CPLUS_BUG19 */

typedef IvlListItem IvlStackItem;

/* CPLUS_BUG10 */
class IvlStack : _private IvlList {
public:
		IvlList :: Clear;
		IvlList :: IsEmpty;
		IvlList :: GetStatus;
inline void	Push (IvlStackItem* p)	{ Prepend (p); }
inline IvlStackItem*	Pop ()		{ return RemoveFirst (); }
inline IvlStackItem*	Top ()		{ return First (); }
};

#ifndef CPLUS_BUG19
template <class ITEM> class IvlStackOf : public IvlStack {
public:
inline void	Push (ITEM* p)	{ IvlStack::Push (p); }
inline ITEM*	Pop ()		{ return (ITEM*) IvlStack::Pop (); }
inline ITEM*	Top ()		{ return (ITEM*) IvlStack::Top (); }
inline		operator const IvlListOf <ITEM>& ()	{ return *(IvlListOf <ITEM>*) this; }
};
#endif	/* CPLUS_BUG19 */

typedef IvlListItem IvlQueueItem;

#if 0
/* CPLUS_BUG10 */
class IvlQueue : _private IvlList {
public:
		IvlList :: Clear;
		IvlList :: IsEmpty;
		IvlList :: GetStatus;
inline void	Put (IvlQueueItem* p)	{ Append (p); }
inline IvlQueueItem*	Get ()		{ return RemoveFirst (); }
inline		operator const IvlList& ()	{ return *this; }
};

#ifndef CPLUS_BUG19
template <class ITEM> class IvlQueueOf : public IvlQueue {
public:
inline void	Put (ITEM* p)	{ IvlQueue::Put (p); }
inline ITEM*	Get ()		{ return (ITEM*) IvlQueue::Get (); }
inline		operator const IvlListOf <ITEM>& ()	{ return *(IvlListOf <ITEM>*) this; }
};
#endif	/* CPLUS_BUG19 */

#endif	/* 0 */

#endif	/* List_H_ */