Epetra_HashTable.h

Go to the documentation of this file.
00001 //@HEADER
00002 /*
00003 ************************************************************************
00004 
00005               Epetra: Linear Algebra Services Package 
00006                 Copyright (2001) Sandia Corporation
00007 
00008 Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 license for use of this work by or on behalf of the U.S. Government.
00010 
00011 This library is free software; you can redistribute it and/or modify
00012 it under the terms of the GNU Lesser General Public License as
00013 published by the Free Software Foundation; either version 2.1 of the
00014 License, or (at your option) any later version.
00015  
00016 This library is distributed in the hope that it will be useful, but
00017 WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 Lesser General Public License for more details.
00020  
00021 You should have received a copy of the GNU Lesser General Public
00022 License along with this library; if not, write to the Free Software
00023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 USA
00025 Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00026 
00027 ************************************************************************
00028 */
00029 //@HEADER
00030 
00031 #ifndef Epetra_HashTable_H_
00032 #define Epetra_HashTable_H_
00033 
00034 #include "Epetra_Object.h"
00035 
00036 class Epetra_HashTable : public Epetra_Object
00037 {
00038   struct Node
00039   {
00040      int Key;
00041      int Value;
00042      Node * Ptr;
00043 
00044      Node( const int key = 0, const int value = 0, Node * ptr = 0 )
00045      : Key(key), Value(value), Ptr(ptr) {}
00046 
00047     private:
00048      Node(const Node& src)
00049        : Key(src.Key), Value(src.Value), Ptr(src.Ptr) {}
00050 
00051     Node& operator=(const Node& src)
00052     { Key = src.Key; Value = src.Value; Ptr = src.Ptr; return(*this); }
00053   };
00054 
00055   Node ** Container_;
00056   int Size_;
00057   unsigned int Seed_;
00058 
00059   int Func( const int key ) { return (Seed_ ^ key)%Size_; }
00060      
00061  public:
00062 
00063   Epetra_HashTable( const int size, const unsigned int seed = (2654435761U) )
00064   : Container_(NULL),
00065     Size_(size),
00066     Seed_(seed)
00067   {
00068     if (size<=0)
00069       throw ReportError( "Bad Hash Table Size: " + toString(size), -1 );
00070 
00071     Container_ = new Node * [size];
00072     for( int i = 0; i < size; ++i ) Container_[i] = 0;
00073   }
00074 
00075   Epetra_HashTable( const Epetra_HashTable & obj )
00076   : Container_(NULL),
00077     Size_(obj.Size_),
00078     Seed_(obj.Seed_)
00079   {
00080     Container_ = new Node * [Size_];
00081     for( int i = 0; i < Size_; ++i ) Container_[i] = 0;
00082     for( int i = 0; i < Size_; ++i )
00083     {
00084       Node * ptr = obj.Container_[i];
00085       while( ptr ) { Add( ptr->Key, ptr->Value ); ptr = ptr->Ptr; }
00086     }
00087   }
00088 
00089   ~Epetra_HashTable()
00090   {
00091     Node * ptr1;
00092     Node * ptr2;
00093     for( int i = 0; i < Size_; ++i )
00094     {
00095       ptr1 = Container_[i];
00096       while( ptr1 ) { ptr2 = ptr1; ptr1 = ptr1->Ptr; delete ptr2; }
00097     }
00098 
00099     delete [] Container_;
00100   }
00101 
00102   void Add( const int key, const int value )
00103   {
00104     int v = Func(key);
00105     Node * n1 = Container_[v];
00106     Container_[v] = new Node(key,value,n1);
00107   }
00108 
00109   int Get( const int key )
00110   {
00111     Node * n = Container_[ Func(key) ];
00112     while( n && (n->Key != key) ) n = n->Ptr;
00113     if( n ) return n->Value;
00114     else    return -1;
00115   }
00116 
00117  private:
00118   Epetra_HashTable& operator=(const Epetra_HashTable& src)
00119     {
00120       (void)src;
00121       //not currently supported
00122       bool throw_error = true;
00123       if (throw_error) {
00124   throw ReportError("Epetra_HashTable::operator= not supported.",-1);
00125       }
00126       return(*this);
00127     }
00128 
00129 };
00130 
00131 #endif

Generated on Wed May 12 21:41:05 2010 for Epetra Package Browser (Single Doxygen Collection) by  doxygen 1.4.7