/* * 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 CcuIdItem; typedef lword CcuID; typedef sword CcuIdType; typedef sword CcuIdIndex; class CcuIdTable { friend class CcuIdIter; friend class CcuIdCell; protected: CcuIdCell* Entries; // the table itself CcuIdCell* LastCell; // its end CcuIdIndex FirstFree; // index of first free entry CcuIdIndex LastFree; // index of end of free list short NumFree; // number of free Entries bool Grow (int); public: CcuIdTable (int); // initial size should be power of 2 CcuID Store (CcuIdItem*, CcuIdType = 0); CcuIdItem* Get (CcuID, CcuIdType* = 0); CcuIdItem* Remove (CcuID, bool* = 0, CcuIdType* = 0); void Change (CcuID, CcuIdItem*, CcuIdType = 0); }; class CcuIdIter { public: enum iditer_status { Normal, StartOfTable, EndOfTable }; protected: const CcuIdTable* TheTable; iditer_status StatusFlag; CcuIdCell* Entries; CcuIdCell* CurCell; public: inline CcuIdIter (const CcuIdTable& t) : TheTable (&t), StatusFlag (StartOfTable) { CurCell = Entries = t.Entries; } inline void Reset () { CurCell = Entries = TheTable->Entries; StatusFlag = StartOfTable; } inline CcuIdIter& operator = (const CcuIdTable& t) { TheTable = &t; StatusFlag = StartOfTable; CurCell = Entries = t.Entries; return *this; } inline CcuIdIter& operator = (const CcuIdIter& it) { TheTable = it.TheTable; StatusFlag = it.StatusFlag; CurCell = it.CurCell; Entries = it.Entries; return *this; } CcuIdIter& operator ++ (); CcuIdItem* Current () const; inline CcuIdItem* operator * () const { return Current (); } inline operator int () const { return StatusFlag == Normal; } CcuIdType CurType () const; CcuID CurId () const; }; template class CcuIdTableOf : public CcuIdTable { public: inline CcuIdTableOf (int sz) : CcuIdTable (sz) {} inline CcuID Store (ITEM* it, CcuIdType typ = 0) { return CcuIdTable::Store (it, typ); } inline ITEM* Get (CcuID id, CcuIdType* typ = 0) { return (ITEM*) CcuIdTable::Get (id, typ); } inline ITEM* Remove (CcuID id, bool* f = 0, CcuIdType* t = 0) { return (ITEM*) CcuIdTable::Remove (id, f, t); } inline void Change (CcuID id, ITEM* i, CcuIdType t = 0) { CcuIdTable::Change (id, i, t); } }; template class CcuIdIterOf : public CcuIdIter { public: inline CcuIdIterOf (CcuIdTableOf & t) : CcuIdIter (t) { } inline CcuIdIterOf& operator = (const CcuIdTableOf & t) { return (CcuIdIterOf&) CcuIdIter::operator = (t); } inline CcuIdIterOf& operator = (const CcuIdIterOf& li) { return (CcuIdIterOf&) CcuIdIter::operator = (li); } inline CcuIdIterOf& operator ++ () { return (CcuIdIterOf&) CcuIdIter::operator ++ (); } inline ITEM* Current () const { return (ITEM*) CcuIdIter::Current (); } inline ITEM* operator * () const { return Current (); } }; #endif /* IdTable_H_ */