Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

SymbolTable.h

00001 
00002 /*
00003      
00004        NEVER INCLUDE THIS FILE unless your name is Symbol.h !
00005 
00006        We need to to have class Symbol fully defined first, and
00007        Symbol.h needs us fully define before its inline functions, so
00008        it includes us between the class defn and the inlines.
00009 
00010  */
00011 
00012 // See the file LICENSE in the some directory as this file for legal
00013 // notices about this software.
00014 #ifndef DEV_W3_ORG__2001_BLINDFOLD_SRC_SYMBOLTABLE_H
00015 #define DEV_W3_ORG__2001_BLINDFOLD_SRC_SYMBOLTABLE_H
00016 // $Id: SymbolTable_8h-source.html,v 1.5 2001/10/10 20:40:58 sandro Exp $
00017 #include "config.h"
00018 #include "EqStr.h"
00019 
00029 class SymbolTable {
00030       
00031 public:
00032 
00033     // STANDARD MEMBER FUNCTIONS
00034     SymbolTable();
00035     SymbolTable(const SymbolTable& other);
00036     const SymbolTable& operator=(const SymbolTable& other);
00037     bool operator==(const SymbolTable& other) const;
00038     bool operator<(const SymbolTable& other) const;
00039     size_t hash() const;
00040     friend std::ostream& operator<<(std::ostream& s, const SymbolTable& me);
00041     std::ostream& print_to(std::ostream& stream) const; 
00042     ~SymbolTable();       
00043 
00044     // CLASS-SPECIFIC PUBLIC INTERFACE
00045 
00050     size_t getTableNumber() const { return tableNumber; }
00051 
00054     typedef unsigned int Type;
00057     typedef unsigned int Index;
00058 
00062     const Type type(Index index) const
00063     { return records[index].type; }
00067     const char* text(Index index) const
00068     { return records[index].text; }
00073     const void keep(Index index)
00074     { ++records[index].refCount; }
00079     const bool release(Index index)
00080     { 
00081     bool reclaimed = (0 == --records[index].refCount);
00082     if (reclaimed) reclaim(index); 
00083     return reclaimed;
00084     }
00085 
00090     Index lookupOrAdd(Type type, const char* text, bool* added_ptr=0);
00091 
00095     size_t size() const { return count; }
00096 
00097 private:
00098 
00099     friend class Symbol;   // so it can manipulate the bindings
00100 
00101     void reclaim(Index index);  // called when refCount reaches zero
00102 
00103     Type numTypes;
00104 
00105     struct Record {
00106     Type type;
00107     char* text;
00108     size_t refCount;
00109     size_t nextHole;   // could be union'd with something (size vs clarity)
00111     Symbol boundTo;    // See note at top of file if you get and error here
00112          
00113     };
00114 
00115     std::vector<Record> records;
00116     size_t first_hole;
00117     size_t count;
00118 
00119     typedef std::hash_map<const char*, size_t, std::hash<const char*>, EqStr> 
00120     Map;
00121     typedef Map::iterator Iter;
00122 
00123     std::vector<Map> map;   // one map for each type
00124 
00125     const Record& record(Index i) { return records[i]; }
00126 
00127     size_t tableNumber;
00128     static size_t nextTableNumber;
00129 
00130 };     
00131 
00132 STANDARD_EXTERNAL(SymbolTable)
00133 
00134 #endif  /* DEV_W3_ORG__2001_BLINDFOLD_SRC_SYMBOLTABLE_H */   // -*-C++-*-

Home to blindfold. This page generated via doxygen 1.2.11.1 Wed Oct 10 16:40:33 2001.