/** * @file lluuidhashmap.h * @brief A uuid based hash map. * * Copyright (c) 2003-$CurrentYear$, Linden Research, Inc. * $License$ */ #ifndef LL_LLUUIDHASHMAP_H #define LL_LLUUIDHASHMAP_H #include "stdtypes.h" #include "llerror.h" #include "lluuid.h" // UUID hash map /* LLUUIDHashMap foo(test_equals); LLUUIDHashMapIter bar(&foo); LLDynamicArray source_ids; const S32 COUNT = 100000; S32 q; for (q = 0; q < COUNT; q++) { llinfos << "Creating" << llendl; LLUUID id; id.generate(); //llinfos << q << ":" << id << llendl; uuid_pair pair; pair.mUUID = id; pair.mValue = q; foo.set(id, pair); source_ids.put(id); //ms_sleep(1); } uuid_pair cur; llinfos << "Iterating" << llendl; for (cur = bar.first(); !bar.done(); cur = bar.next()) { if (source_ids[cur.mValue] != cur.mUUID) { llerrs << "Incorrect value iterated!" << llendl; } //llinfos << cur.mValue << ":" << cur.mUUID << llendl; //ms_sleep(1); } llinfos << "Finding" << llendl; for (q = 0; q < COUNT; q++) { cur = foo.get(source_ids[q]); if (source_ids[cur.mValue] != cur.mUUID) { llerrs << "Incorrect value found!" << llendl; } //llinfos << res.mValue << ":" << res.mUUID << llendl; //ms_sleep(1); } llinfos << "Removing" << llendl; for (q = 0; q < COUNT/2; q++) { if (!foo.remove(source_ids[q])) { llerrs << "Remove failed!" << llendl; } //ms_sleep(1); } llinfos << "Iterating" << llendl; for (cur = bar.first(); !bar.done(); cur = bar.next()) { if (source_ids[cur.mValue] != cur.mUUID) { llerrs << "Incorrect value found!" << llendl; } //llinfos << cur.mValue << ":" << cur.mUUID << llendl; //ms_sleep(1); } llinfos << "Done with UUID map test" << llendl; return 0; */ // // LLUUIDHashNode // template class LLUUIDHashNode { public: LLUUIDHashNode(); public: S32 mCount; U8 mKey[SIZE]; DATA mData[SIZE]; LLUUIDHashNode *mNextNodep; }; // // LLUUIDHashNode implementation // template LLUUIDHashNode::LLUUIDHashNode() { mCount = 0; mNextNodep = NULL; } template class LLUUIDHashMap { public: // basic constructor including sorter LLUUIDHashMap(BOOL (*equals)(const LLUUID &uuid, const DATA_TYPE &data), const DATA_TYPE &null_data); ~LLUUIDHashMap(); inline DATA_TYPE &get(const LLUUID &uuid); inline BOOL check(const LLUUID &uuid) const; inline DATA_TYPE &set(const LLUUID &uuid, const DATA_TYPE &type); inline BOOL remove(const LLUUID &uuid); void removeAll(); inline S32 getLength() const; // Warning, NOT O(1!) public: BOOL (*mEquals)(const LLUUID &uuid, const DATA_TYPE &data); LLUUIDHashNode mNodes[256]; S32 mIterCount; protected: DATA_TYPE mNull; }; // // LLUUIDHashMap implementation // template LLUUIDHashMap::LLUUIDHashMap(BOOL (*equals)(const LLUUID &uuid, const DATA_TYPE &data), const DATA_TYPE &null_data) : mEquals(equals), mIterCount(0), mNull(null_data) { } template LLUUIDHashMap::~LLUUIDHashMap() { removeAll(); } template void LLUUIDHashMap::removeAll() { S32 bin; for (bin = 0; bin < 256; bin++) { LLUUIDHashNode* nodep = &mNodes[bin]; BOOL first = TRUE; while (nodep) { S32 i; const S32 count = nodep->mCount; // Iterate through all members of this node for (i = 0; i < count; i++) { nodep->mData[i] = mNull; } nodep->mCount = 0; // Done with all objects in this node, go to the next. LLUUIDHashNode* curp = nodep; nodep = nodep->mNextNodep; // Delete the node if it's not the first node if (first) { first = FALSE; curp->mNextNodep = NULL; } else { delete curp; } } } } template inline S32 LLUUIDHashMap::getLength() const { S32 count = 0; S32 bin; for (bin = 0; bin < 256; bin++) { LLUUIDHashNode* nodep = &mNodes[bin]; while (nodep) { count += nodep->mCount; nodep = nodep->mNextNodep; } } return count; } template inline DATA_TYPE &LLUUIDHashMap::get(const LLUUID &uuid) { LLUUIDHashNode* nodep = &mNodes[uuid.mData[0]]; // Grab the second byte of the UUID, which is the key for the node data const S32 second_byte = uuid.mData[1]; while (nodep) { S32 i; const S32 count = nodep->mCount; // Iterate through all members of this node for (i = 0; i < count; i++) { if ((nodep->mKey[i] == second_byte) && mEquals(uuid, nodep->mData[i])) { // The second byte matched, and our equality test passed. // We found it. return nodep->mData[i]; } } // Done with all objects in this node, go to the next. nodep = nodep->mNextNodep; } return mNull; } template inline BOOL LLUUIDHashMap::check(const LLUUID &uuid) const { const LLUUIDHashNode* nodep = &mNodes[uuid.mData[0]]; // Grab the second byte of the UUID, which is the key for the node data const S32 second_byte = uuid.mData[1]; while (nodep) { S32 i; const S32 count = nodep->mCount; // Iterate through all members of this node for (i = 0; i < count; i++) { if ((nodep->mKey[i] == second_byte) && mEquals(uuid, nodep->mData[i])) { // The second byte matched, and our equality test passed. // We found it. return TRUE; } } // Done with all objects in this node, go to the next. nodep = nodep->mNextNodep; } // Didn't find anything return FALSE; } template inline DATA_TYPE &LLUUIDHashMap::set(const LLUUID &uuid, const DATA_TYPE &data) { // Set is just like a normal find, except that if we find a match // we replace it with the input value. // If we don't find a match, we append to the end of the list. LLUUIDHashNode* nodep = &mNodes[uuid.mData[0]]; const S32 second_byte = uuid.mData[1]; while (1) { const S32 count = nodep->mCount; S32 i; for (i = 0; i < count; i++) { if ((nodep->mKey[i] == second_byte) && mEquals(uuid, nodep->mData[i])) { // We found a match for this key, replace the data with // the incoming data. nodep->mData[i] = data; return nodep->mData[i]; } } if (!nodep->mNextNodep) { // We've iterated through all of the keys without finding a match if (i < SIZE) { // There's still some space on this node, append // the key and data to it. nodep->mKey[i] = second_byte; nodep->mData[i] = data; nodep->mCount++; return nodep->mData[i]; } else { // This node is full, append a new node to the end. nodep->mNextNodep = new LLUUIDHashNode; nodep->mNextNodep->mKey[0] = second_byte; nodep->mNextNodep->mData[0] = data; nodep->mNextNodep->mCount = 1; return nodep->mNextNodep->mData[0]; } } // No match on this node, go to the next nodep = nodep->mNextNodep; } } template inline BOOL LLUUIDHashMap::remove(const LLUUID &uuid) { if (mIterCount) { // We don't allow remove when we're iterating, it's bad karma! llerrs << "Attempted remove while an outstanding iterator in LLUUIDHashMap!" << llendl; } // Remove is the trickiest operation. // What we want to do is swap the last element of the last // node if we find the one that we want to remove, but we have // to deal with deleting the node from the tail if it's empty, but // NOT if it's the only node left. LLUUIDHashNode *nodep = &mNodes[uuid.mData[0]]; // Not empty, we need to search through the nodes const S32 second_byte = uuid.mData[1]; // A modification of the standard search algorithm. while (nodep) { const S32 count = nodep->mCount; S32 i; for (i = 0; i < count; i++) { if ((nodep->mKey[i] == second_byte) && mEquals(uuid, nodep->mData[i])) { // We found the node that we want to remove. // Find the last (and next-to-last) node, and the index of the last // element. We could conceviably start from the node we're on, // but that makes it more complicated, this is easier. LLUUIDHashNode *prevp = &mNodes[uuid.mData[0]]; LLUUIDHashNode *lastp = prevp; // Find the last and next-to-last while (lastp->mNextNodep) { prevp = lastp; lastp = lastp->mNextNodep; } // First, swap in the last to the current location. nodep->mKey[i] = lastp->mKey[lastp->mCount - 1]; nodep->mData[i] = lastp->mData[lastp->mCount - 1]; // Now, we delete the entry lastp->mCount--; lastp->mData[lastp->mCount] = mNull; if (!lastp->mCount) { // We deleted the last element! if (lastp != &mNodes[uuid.mData[0]]) { // Only blitz the node if it's not the head // Set the previous node to point to NULL, then // blitz the empty last node prevp->mNextNodep = NULL; delete lastp; } } return TRUE; } } // Iterate to the next node, we've scanned all the entries in this one. nodep = nodep->mNextNodep; } return FALSE; } // // LLUUIDHashMapIter // template class LLUUIDHashMapIter { public: LLUUIDHashMapIter(LLUUIDHashMap *hash_mapp); ~LLUUIDHashMapIter(); inline void first(); inline void next(); inline BOOL done() const; DATA_TYPE& operator*() const { return mCurHashNodep->mData[mCurHashNodeKey]; } DATA_TYPE* operator->() const { return &(operator*()); } protected: LLUUIDHashMap *mHashMapp; LLUUIDHashNode *mCurHashNodep; S32 mCurHashMapNodeNum; S32 mCurHashNodeKey; DATA_TYPE mNull; }; // // LLUUIDHashMapIter Implementation // template LLUUIDHashMapIter::LLUUIDHashMapIter(LLUUIDHashMap *hash_mapp) { mHashMapp = hash_mapp; mCurHashNodep = NULL; mCurHashMapNodeNum = 0; mCurHashNodeKey = 0; } template LLUUIDHashMapIter::~LLUUIDHashMapIter() { if (mCurHashNodep) { // We're partway through an iteration, we can clean up now mHashMapp->mIterCount--; } } template inline void LLUUIDHashMapIter::first() { // Iterate through until we find the first non-empty node; S32 i; for (i = 0; i < 256; i++) { if (mHashMapp->mNodes[i].mCount) { if (!mCurHashNodep) { // Increment, since it's no longer safe for us to do a remove mHashMapp->mIterCount++; } mCurHashNodep = &mHashMapp->mNodes[i]; mCurHashMapNodeNum = i; mCurHashNodeKey = 0; //return mCurHashNodep->mData[0]; return; } } // Completely empty! mCurHashNodep = NULL; //return mNull; return; } template inline BOOL LLUUIDHashMapIter::done() const { return mCurHashNodep ? FALSE : TRUE; } template inline void LLUUIDHashMapIter::next() { // No current entry, this iterator is done if (!mCurHashNodep) { //return mNull; return; } // Go to the next element mCurHashNodeKey++; if (mCurHashNodeKey < mCurHashNodep->mCount) { // We're not done with this node, return the current element //return mCurHashNodep->mData[mCurHashNodeKey]; return; } // Done with this node, move to the next mCurHashNodep = mCurHashNodep->mNextNodep; if (mCurHashNodep) { // Return the first element mCurHashNodeKey = 0; //return mCurHashNodep->mData[0]; return; } // Find the next non-empty node (keyed on the first byte) mCurHashMapNodeNum++; S32 i; for (i = mCurHashMapNodeNum; i < 256; i++) { if (mHashMapp->mNodes[i].mCount) { // We found one that wasn't empty mCurHashNodep = &mHashMapp->mNodes[i]; mCurHashMapNodeNum = i; mCurHashNodeKey = 0; //return mCurHashNodep->mData[0]; return; } } // OK, we're done, nothing else to iterate mCurHashNodep = NULL; mHashMapp->mIterCount--; // Decrement since we're safe to do removes now //return mNull; } #endif // LL_LLUUIDHASHMAP_H