nrnhash.h

Go to the documentation of this file.
00001 #ifndef nrnhash_h
00002 #define nrnhash_h
00003 // hash table where buckets are binary search maps and key is castable to unsigned long
00004 
00005 #include <ivstream.h>
00006 
00007 #if defined(HAVE_SSTREAM) // the standard...
00008 #include <vector>
00009 #include <map>
00010 #else
00011 #include <vector.h>
00012 #include <map.h>
00013 #endif
00014 
00015 #define __NrnHashEntry(Table) Table##_Entry
00016 #define NrnHashEntry(Table) __NrnHashEntry(Table)
00017 #define __NrnHashLT(Table) nrnhash_lt_##Table
00018 #define NrnHashLT(Table) __NrnHashLT(Table)
00019 
00020    // note that more recent STL versions (But not gcc2.95) supply 
00021    // at(long) to get the vector element. My version contains some
00022    // confusion with regard to how to handle issues involving const
00023    // in this classes implementation of the find method.
00024    // [] creates key value association if does not exist
00025 
00026 #define declareNrnHash(Table,Key,Value) \
00027 struct NrnHashLT(Table) { \
00028    int operator() (Key i, Key j) const { \
00029       return ((unsigned long)i) < ((unsigned long)j); \
00030    } \
00031 }; \
00032 \
00033 class NrnHashEntry(Table) : public std::map <Key, Value, NrnHashLT(Table)>{}; \
00034 \
00035 class Table : public vector<NrnHashEntry(Table)> { \
00036 public: \
00037    Table(long size); \
00038    virtual ~Table(); \
00039    boolean find(Key, Value&)const; \
00040    NrnHashEntry(Table)& at(unsigned long bucket){ return *(begin() + bucket); } \
00041    Value& operator[](Key key) { return at(hash(key))[key]; } \
00042    void remove(Key); \
00043    unsigned long hash(Key key)const { return ((unsigned long)key)%size_; } \
00044    long size_; \
00045 };
00046 
00047 #define implementNrnHash(Table,Key,Value) \
00048 Table::Table(long size) { \
00049    resize(size+1); \
00050    size_ = size; \
00051 } \
00052 \
00053 Table::~Table() {} \
00054 \
00055 boolean Table::find(Key key, Value& ps)const { \
00056    NrnHashEntry(Table)::const_iterator itr; \
00057    const NrnHashEntry(Table)& gm = ((Table*)this)->at(hash(key)); \
00058    if ((itr = gm.find(key)) == gm.end()) { \
00059       return false; \
00060    } \
00061    ps = itr->second; \
00062    return true; \
00063 }\
00064 \
00065 void Table::remove(Key key) { \
00066    NrnHashEntry(Table)& gm = ((Table*)this)->at(hash(key)); \
00067    gm.erase(key); \
00068 }
00069 
00070 // for iteration, if you have 
00071 // declareNrnHash(Table,Key,Object)
00072 // Table* table;
00073 // then you can iterate with
00074 #define NrnHashIterate(Table,table,Value,value) \
00075    if (table) for (long i__ = table->size_ - 1; i__ >= 0; --i__) { \
00076       NrnHashEntry(Table)::const_iterator p__ = table->at(i__).begin(); \
00077       NrnHashEntry(Table)::const_iterator pe__ = table->at(i__).end(); \
00078       while(p__ != pe__) { \
00079          Value value = (*p__).second; \
00080          ++p__; \
00081 // need to close with two extra }}
00082 
00083 #endif // nrnhash_h
Generated on Mon Jun 13 08:10:25 2011 for NEURON by  doxygen 1.6.3