summaryrefslogtreecommitdiff
path: root/utils/IdTable.h
blob: 0b64219c98ed3de993925e6f24f156fd8e3086f7 (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
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1990, 1991, 1992
 *	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 "word.h"
#include "bool.h"

/*
 *	TID_MASK = 2^TID_SHIFT = max table size
 *	TID_MASK = TID_SHIFT bits set to 1.
 */
#define	TID_MAX	(2 << 16)
#define	TID_SHIFT	16
#define	TID_MASK	0xffff

struct CcuIdCell {
	sword		chk;	// the check number
	sword		typ;	// for the appli, for free list when not allocated
	const void*	obj;	// the object
};

class CcuIdTable {
friend	class	CcuIdIter;

protected:
	CcuIdCell*	entries;	// the table itself
	CcuIdCell*	last;		// its end
	sword	free;		// index of first free entry
	sword	last_free;	// index of end of free list
	sword	num_free;	// number of free entries

	bool	Grow (int);

public:
		CcuIdTable (int = 4);	// initial size should be power of 2	*** 4 for testing
	
	lword	Store (const void*, sword = 0);
	bool	Remove (lword);
	const void*	Get (lword, sword* = 0);
	void	Change (lword, const void*, sword = 0);
	
};

class CcuIdIter {
protected:
	CcuIdTable*	idt;
	CcuIdCell*		entries;
	CcuIdCell*		cur;
	
	void		Step ();

public:
			CcuIdIter (CcuIdTable& t)	{ idt = &t; cur = entries = t.entries; Step (); }
	
	const void*	operator () ()		{ Step (); return cur ? cur->obj : 0; }
	const void*	operator ++ ()		{ Step (); return cur ? cur++->obj : 0; }
	const void*	Current ()		{ return cur ? cur->obj : 0; }
	sword		CurType ()			{ return cur ? cur->typ : 0; }
	lword		CurId ()				{ return cur ? (cur -> chk << TID_SHIFT) | (cur - entries) : 0; }
	
	void	Reset ()				{ cur = entries = idt->entries; }
};

#endif	/* IdTable_H_ */