/* * 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_ */