PropertyHashTable.hpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #ifndef sim_stat_PropertyHashTable_hpp
00023 #define sim_stat_PropertyHashTable_hpp
00024 
00025 #include "util/template/SinglyLinkedList.hpp"
00026 #include "comp/schema/CompSchema.hpp"
00027 #include "util/error/Log.hpp"
00028 #include "Property.hpp"
00029 
00030 namespace se_core {
00031 
00032     class PropertyHashTable {
00033     public:
00034         typedef CompSchema::VoidList::iterator_type iterator_type;
00035     protected:
00036         enum {
00037             DEFAULT_HASH_TABLE_SIZE = 16
00038         };
00039 
00040         iterator_type* hashTable_;
00041         int tableSize_;
00042         int count_;
00043 
00044 
00045     public:
00046         PropertyHashTable()
00047                 : count_(0) {
00048             initialize();
00049         }
00050 
00051         PropertyHashTable(int size)
00052                 : count_(0) {
00053             initialize(size);
00054         }
00055 
00056         virtual ~PropertyHashTable() {
00057             if(hashTable_) {
00058                 // For all hash lists
00059                 for(int i = 0; i < tableSize(); ++i) {
00060                     // delete HashNode's
00061                     iterator_type it;
00062                     it = hashTable_[i];
00063                     while(it != CompSchema::VoidList::end()) {
00064                         Property* p = static_cast<Property*>(CompSchema::voidList.next(it));
00065                         delete p;
00066                     }
00067                     it = hashTable_[i];
00068                     // remove link chain from nodeList
00069                     CompSchema::voidList.removeChain(it);
00070                 }
00071                 delete [] hashTable_;
00072             }
00073         }
00074 
00075         inline int tableIndex(int key) const {
00076             return key & (tableSize_ - 1);
00077         }
00078 
00079         void initialize(int size = DEFAULT_HASH_TABLE_SIZE) {
00080             hashTable_ = new iterator_type[size];
00081             tableSize_ = size;
00082             for(int i = 0; i < tableSize_; ++i) {
00083                 hashTable_[i] = CompSchema::VoidList::end();
00084             }
00085         }
00086 
00087 
00088         // insert key into hash table.
00089         // returns pointer to old data with the key, if any, or
00090         // NULL if the key wasn't in the table previously.
00091         Property* add(Property& newNode) {
00092             if(!hashTable_)
00093                 return 0;
00094 
00095             AssertFatal(lookup(newNode.key()) == 0, "Property with that hash already exists: " << newNode.name() << " == " << lookup(newNode.key())->name() );
00096 
00097             int key = newNode.key();
00098             unsigned int index = tableIndex(key);
00099             Property* old = lookup(key);
00100             if(old) {
00101                 remove(old->key());
00102             }
00103 
00104             CompSchema::voidList.add(&newNode, hashTable_[ index ]);
00105             ++count_;
00106             return old;
00107         }
00108 
00109 
00110         Property* lookup(unsigned int key) const {
00111             if(!hashTable_)
00112                 return 0;
00113 
00114             Property* node = lookupNode(key);
00115             return node;
00116         }
00117 
00118 
00119         // returns the list that contains this hash key...
00120         // (for instance, if you have multiple matching keys)
00121         iterator_type lookupList(unsigned int key) {
00122             if(!hashTable_)
00123                 return -1;
00124             unsigned int index = tableIndex(key);
00125             return hashTable_[index];
00126         }
00127 
00128 
00129         Property* remove(short key) {
00130             if(!hashTable_)
00131                 return 0;
00132 
00133             unsigned int index = tableIndex(key);
00134 
00135             iterator_type walker = hashTable_[index];
00136             while( walker != CompSchema::VoidList::end() ) {
00137                 Property* v = static_cast<Property*>(CompSchema::voidList.next(walker));
00138                 if(v->key() == key) {
00139                     CompSchema::voidList.remove(v, hashTable_[ index ]);
00140                     --count_;
00141                     return v;
00142                 }
00143             }
00144 
00145             return 0;
00146         }
00147 
00148 
00149         iterator_type hashTable(int index) {
00150             return hashTable_[index];
00151         }
00152 
00153 
00154 
00155         int tableSize() {
00156             return tableSize_;
00157         }
00158 
00159 
00160         int count() {
00161             return count_;
00162         }
00163 
00164 
00165     protected:
00166         Property* lookupNode(unsigned int key) const {
00167             unsigned int index = tableIndex(key);
00168             Property* ret = 0;
00169 
00170             iterator_type walker;
00171             walker = hashTable_[index];
00172 
00173             while(walker != CompSchema::VoidList::end()) {
00174                 Property* v = static_cast<Property*>(CompSchema::voidList.next(walker));
00175                 if(v->key() == key) {
00176                     ret = v;
00177                     break;
00178                 }
00179             }
00180 
00181             return ret;
00182         }
00183 
00184 
00185     private:
00186         // declared to prevent unintentional use...
00187         // (Don't forget to move to public access if you declare them!)
00188         PropertyHashTable& Copy(const PropertyHashTable& source_object) { return *this; }
00189         PropertyHashTable& operator= (const PropertyHashTable& source_object) { return *this; }
00190     };
00191 }
00192 
00193 #endif

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:12 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo