PageRenderTime 65ms CodeModel.GetById 39ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llsimplehash.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 155 lines | 119 code | 9 blank | 27 comment | 10 complexity | 303a298c59745b98a766257b720adc83 MD5 | raw file
  1/** 
  2 * @file llsimplehash.h
  3 *
  4 * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  5 * Second Life Viewer Source Code
  6 * Copyright (C) 2010, Linden Research, Inc.
  7 * 
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation;
 11 * version 2.1 of the License only.
 12 * 
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 * Lesser General Public License for more details.
 17 * 
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 21 * 
 22 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 23 * $/LicenseInfo$
 24 */
 25
 26#ifndef LL_LLSIMPLEHASH_H
 27#define LL_LLSIMPLEHASH_H
 28
 29#include "llstl.h"
 30
 31template <typename HASH_KEY_TYPE>
 32class LLSimpleHashEntry
 33{
 34protected:
 35	HASH_KEY_TYPE mHashKey;
 36	LLSimpleHashEntry<HASH_KEY_TYPE>* mNextEntry;
 37public:
 38	LLSimpleHashEntry(HASH_KEY_TYPE key) :
 39		mHashKey(key),
 40		mNextEntry(0)
 41	{
 42	}
 43	virtual ~LLSimpleHashEntry()
 44	{
 45	}
 46	HASH_KEY_TYPE getHashKey() const
 47	{
 48		return mHashKey;
 49	}
 50	LLSimpleHashEntry<HASH_KEY_TYPE>* getNextEntry() const
 51	{
 52		return mNextEntry;
 53	}
 54	void setNextEntry(LLSimpleHashEntry<HASH_KEY_TYPE>* next)
 55	{
 56		mNextEntry = next;
 57	}
 58};
 59
 60template <typename HASH_KEY_TYPE, int TABLE_SIZE>
 61class LL_COMMON_API LLSimpleHash
 62{
 63public:
 64	LLSimpleHash()
 65	{
 66		llassert(TABLE_SIZE);
 67		llassert((TABLE_SIZE ^ (TABLE_SIZE-1)) == (TABLE_SIZE | (TABLE_SIZE-1))); // power of 2
 68		memset(mEntryTable, 0, sizeof(mEntryTable));
 69	}
 70	virtual ~LLSimpleHash()
 71	{
 72	}
 73
 74	virtual int getIndex(HASH_KEY_TYPE key)
 75	{
 76		return key & (TABLE_SIZE-1);
 77	}
 78	
 79	bool insert(LLSimpleHashEntry<HASH_KEY_TYPE>* entry)
 80	{
 81		llassert(entry->getNextEntry() == 0);
 82		int index = getIndex(entry->getHashKey());
 83		entry->setNextEntry(mEntryTable[index]);
 84		mEntryTable[index] = entry;
 85		return true;
 86	}
 87	LLSimpleHashEntry<HASH_KEY_TYPE>* find(HASH_KEY_TYPE key)
 88	{
 89		int index = getIndex(key);
 90		LLSimpleHashEntry<HASH_KEY_TYPE>* res = mEntryTable[index];
 91		while(res && (res->getHashKey() != key))
 92		{
 93			res = res->getNextEntry();
 94		}
 95		return res;
 96	}
 97	bool erase(LLSimpleHashEntry<HASH_KEY_TYPE>* entry)
 98	{
 99		return erase(entry->getHashKey());
100	}
101	bool erase(HASH_KEY_TYPE key)
102	{
103		int index = getIndex(key);
104		LLSimpleHashEntry<HASH_KEY_TYPE>* prev = 0;
105		LLSimpleHashEntry<HASH_KEY_TYPE>* res = mEntryTable[index];
106		while(res && (res->getHashKey() != key))
107		{
108			prev = res;
109			res = res->getNextEntry();
110		}
111		if (res)
112		{
113			LLSimpleHashEntry<HASH_KEY_TYPE>* next = res->getNextEntry();
114			if (prev)
115			{
116				prev->setNextEntry(next);
117			}
118			else
119			{
120				mEntryTable[index] = next;
121			}
122			return true;
123		}
124		else
125		{
126			return false;
127		}
128	}
129	// Removes and returns an arbitrary ("first") element from the table
130	// Used for deleting the entire table.
131	LLSimpleHashEntry<HASH_KEY_TYPE>* pop_element()
132	{
133		for (int i=0; i<TABLE_SIZE; i++)
134		{
135			LLSimpleHashEntry<HASH_KEY_TYPE>* entry = mEntryTable[i];
136			if (entry)
137			{
138				mEntryTable[i] = entry->getNextEntry();
139				return entry;
140			}
141		}
142		return 0;
143	}
144	// debugging
145	LLSimpleHashEntry<HASH_KEY_TYPE>* get_element_at_index(S32 index) const
146	{
147		return mEntryTable[index];
148	}
149	
150	
151private:
152	LLSimpleHashEntry<HASH_KEY_TYPE>* mEntryTable[TABLE_SIZE];
153};
154
155#endif // LL_LLSIMPLEHASH_H