summaryrefslogtreecommitdiff
path: root/utils/Chain.h
blob: 91d13155ea3cd3957fa5ea7c90b6c6776f4f9573 (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
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1992
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	generic chains of objects - to be updated
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef Chain_H_
#define Chain_H_

#include "cplus_bugs.h"

typedef void IvlChainItem;

class IvlChainIter;

class IvlChain {
friend	class	IvlChainIter;
public:
	enum	chain_status	{ NoError, WasEmpty, TooEarly, TooFar, BadIterator };

protected:
	IvlChainItem*	LastLink;
	chain_status	StatusFlag;
inline		IvlChain () : LastLink (0), StatusFlag (NoError)	{ }
inline		IvlChain (IvlChainItem* e) : LastLink (e), StatusFlag (NoError) { }
virtual		~IvlChain ();

virtual	IvlChainItem*	GetNext (IvlChainItem*) const = 0;
virtual	void	SetNext (IvlChainItem*, IvlChainItem*) const = 0;
virtual	void	Delete (IvlChainItem*) const = 0;
	IvlChainItem*	GetPrevious (IvlChainItem*) ;

public:
inline	chain_status	GetStatus () const		{ return StatusFlag; }
inline	int	IsEmpty () const			{ return !LastLink; }
	IvlChainItem*	First ();
	IvlChainItem*	Last ();
	IvlChainItem*	Nth (int n);
	int	Length () const;
	int	Find (IvlChainItem*) const;

	void	Append (IvlChainItem*);
	void	Prepend (IvlChainItem*);
inline	IvlChain&	operator << (IvlChainItem* it)	{ Append (it); return *this; }

	IvlChainItem*	RemoveFirst ();
	IvlChainItem*	RemoveLast ();
	int	Remove (IvlChainItem*);
	int	Remove (int (*) (IvlChainItem*));
	void	Clear ();

	void	InsertAfter (const IvlChainIter&, IvlChainItem*);
	void	InsertBefore (const IvlChainIter&, IvlChainItem*);
	IvlChainItem*	RemoveAfter (const IvlChainIter&);
	void	InsertAfter (IvlChainItem*, IvlChainItem*);
	void	InsertBefore (IvlChainItem*, IvlChainItem*);
	IvlChainItem*	RemoveAfter (IvlChainItem*);
};

class IvlChainIter {
friend	class	IvlChain;
public:
	enum	chainiter_status	{ Normal, StartOfChain, EndOfChain };
private:
	const IvlChain*	TheChain;
	IvlChainItem*	CurLink;
	chainiter_status	StatusFlag;

public:
inline		IvlChainIter (const IvlChain& l) : TheChain (&l), CurLink (0), StatusFlag (StartOfChain)	{ }
inline	void	Reset ()		{ CurLink = 0; StatusFlag = StartOfChain; }
	IvlChainIter&	operator ++ ();
	IvlChainItem*	operator * () const;
		int	Find (IvlChainItem*);
inline	chainiter_status	GetStatus () const		{ return StatusFlag; }
inline	operator int () const	{ return StatusFlag == Normal; }
};

#ifndef CPLUS_BUG19

template <class ITEM> class IvlChainOf : public IvlChain {
protected:
	IvlChainItem*	GetNext (IvlChainItem* i) const	{ return ((ITEM*) i)->GetNext (); }
	void	SetNext (IvlChainItem* i, IvlChainItem* j) const	{ ((ITEM*) i)->SetNext ((ITEM*) j); }
	void	Delete (IvlChainItem* i) const	{ delete (ITEM*) i; }

public:
inline		IvlChainOf () : IvlChain ()		{ }
inline		IvlChainOf (ITEM* e) : IvlChain (e)		{ }
inline		~IvlChainOf ()	{}
};

template <class ITEM> class IvlChainIterOf : public IvlChainIter {
public:
inline		IvlChainIterOf (const IvlChainOf <ITEM>& l) : IvlChainIter (l)	{ }
inline	ITEM*	operator * () const	{ return (ITEM*) IvlChainIter::operator * (); }
};

#endif	/* CPLUS_BUG19 */

#endif	/* Chain_H_ */