Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

sglHashTable.hpp

00001 /*****************************************************************************
00002  * SGL: A Scene Graph Library
00003  *
00004  * Copyright (C) 1997-2001  Scott McMillan   All Rights Reserved.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the Free
00018  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *****************************************************************************
00020  *     File: sglHashTable.hpp
00021  *  Created: 16 July 1997
00022  *  Summary: create a hash table
00023  *****************************************************************************/
00024 
00025 #ifndef __SGL_HASH_LIST_HPP
00026 #define __SGL_HASH_LIST_HPP
00027 
00028 #include <sgldb.h>
00029 #include <vector>
00030 
00031 //----------------------------------------------------------------------------
00033 template <class T>
00034 class SGLDB_DLL_API sglHashTable
00035 {
00036    typedef typename vector<T*>::iterator hash_iterator;
00037 public:
00038    sglHashTable(unsigned int hashsize) :
00039       m_hashsize(hashsize)
00040       {
00041          m_hash_list = new vector<T*>[hashsize];
00042       }
00043 
00045    virtual ~sglHashTable() { delete[] m_hash_list; }
00046 
00048    T* search(T* t)
00049       {
00050          unsigned int pos = t->hash(m_hashsize);
00051          for (hash_iterator i = m_hash_list[pos].begin();
00052               i != m_hash_list[pos].end(); ++i)
00053          {
00054             if (t->isEqual(*i))
00055                return *i;
00056          }
00057 
00058          return NULL;
00059       }
00060 
00063    T* searchagain(T* t, T* u)
00064       {
00065          unsigned int pos = t->hash(m_hashsize);
00066          bool foundu = false;
00067          for (hash_iterator i = m_hash_list[pos].begin();
00068               i != m_hash_list[pos].end(); ++i)
00069          {
00070             if (u == *i)
00071                foundu = true;
00072             else if (foundu && t->isEqual(*i))
00073                return *i;
00074          }
00075 
00076          return NULL;
00077       }
00078 
00080    void add(T* t)
00081       {
00082          unsigned int pos = t->hash(m_hashsize);
00083          m_hash_list[pos].push_back(t);
00084       }
00085 
00086 private:
00088    sglHashTable();
00089 
00091    sglHashTable(const sglHashTable &);
00092 
00094    sglHashTable &operator=(const sglHashTable &);
00095 
00096 private:
00098    unsigned int m_hashsize;
00099 
00103    vector<T*> *m_hash_list;
00104 };
00105 
00106 #endif

Generated at Mon Jul 1 18:00:04 2002 for SGL by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001