summaryrefslogtreecommitdiff
path: root/utils/IdTable.h
blob: aec342449c604a58cc1db0da0808d7ec57542964 (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
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1990, 1991, 1992, 1993, 1994
 *	Laboratoire de Recherche en Informatique (LRI)
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	tables for managing an ID scheme, originally by Michel Beaudouin-Lafon
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef IdTable_H_
#define IdTable_H_

#include "cplus_bugs.h"
#include "word.h"
#include "bool.h"

typedef void IvlIdItem;
typedef lword IvlID;
typedef sword IvlIdType;
typedef sword IvlIdIndex;

class IvlIdTable {
friend	class	IvlIdIter;
friend	class	IvlIdCell;

protected:
	IvlIdCell*	Entries;	// the table itself
	IvlIdCell*	LastCell;		// its end
	IvlIdIndex	FirstFree;		// index of first free entry
	IvlIdIndex	LastFree;	// index of end of free list
	short	NumFree;	// number of free Entries

	bool	Grow (int);

public:
		IvlIdTable (int);	// initial size should be power of 2
	
	IvlID	Store (IvlIdItem*, IvlIdType = 0);
	IvlIdItem*	Get (IvlID, IvlIdType* = 0);
	IvlIdItem*	Remove (IvlID, bool* = 0, IvlIdType* = 0);
	void	Change (IvlID, IvlIdItem*, IvlIdType = 0);
	
};

class IvlIdIter {
public:
	enum	iditer_status	{ Normal, StartOfTable, EndOfTable };

protected:
	const IvlIdTable*	TheTable;
	iditer_status	StatusFlag;
	IvlIdCell*		Entries;
	IvlIdCell*		CurCell;
	
public:
inline		IvlIdIter (const IvlIdTable& t) : TheTable (&t), StatusFlag (StartOfTable)	{ CurCell = Entries = t.Entries; }
inline	void	Reset ()				{ CurCell = Entries = TheTable->Entries; StatusFlag = StartOfTable; }
inline	IvlIdIter&	operator = (const IvlIdTable& t)	{ TheTable = &t; StatusFlag = StartOfTable; CurCell = Entries = t.Entries; return *this; }
inline	IvlIdIter&	operator = (const IvlIdIter& it)	{ TheTable = it.TheTable; StatusFlag = it.StatusFlag; CurCell = it.CurCell; Entries = it.Entries; return *this; }

	IvlIdIter&	operator ++ ();
	IvlIdItem*	Current () const;
inline	IvlIdItem*	operator * () const		{ return Current (); }
inline	operator int () const	{ return StatusFlag == Normal; }
	IvlIdType		CurType () const;
	IvlID		CurId () const;
	
};
template <class ITEM> class IvlIdTableOf : public IvlIdTable {
public:
inline		IvlIdTableOf (int sz) : IvlIdTable (sz)	{}
	
inline	IvlID	Store (ITEM* it, IvlIdType typ = 0)	{ return IvlIdTable::Store (it, typ); }
inline	ITEM*	Get (IvlID id, IvlIdType* typ = 0)	{ return (ITEM*) IvlIdTable::Get (id, typ); }
inline	ITEM*	Remove (IvlID id, bool* f = 0, IvlIdType* t = 0)	{ return (ITEM*) IvlIdTable::Remove (id, f, t); }
inline	void	Change (IvlID id, ITEM* i, IvlIdType t = 0)	{ IvlIdTable::Change (id, i, t); }
};

template <class ITEM> class IvlIdIterOf : public IvlIdIter {
public:
inline			IvlIdIterOf (IvlIdTableOf <ITEM> & t) : IvlIdIter (t)	{ }
inline	IvlIdIterOf<ITEM>&	operator =  (const IvlIdTableOf <ITEM>& t)	{ return (IvlIdIterOf<ITEM>&) IvlIdIter::operator = (t); }
inline	IvlIdIterOf<ITEM>&	operator =  (const IvlIdIterOf<ITEM>& li)	{ return (IvlIdIterOf<ITEM>&) IvlIdIter::operator = (li); }
inline	IvlIdIterOf<ITEM>&	operator ++ ()	{ return (IvlIdIterOf<ITEM>&) IvlIdIter::operator ++ (); }
inline	ITEM*	Current () const	{ return (ITEM*) IvlIdIter::Current (); }
inline	ITEM*	operator * () const		{ return Current (); }
};

#endif	/* IdTable_H_ */