Epetra Package Browser (Single Doxygen Collection) Development
Epetra_HashTable.h
Go to the documentation of this file.
00001 /*
00002 //@HEADER
00003 // ************************************************************************
00004 // 
00005 //               Epetra: Linear Algebra Services Package 
00006 //                 Copyright 2011 Sandia Corporation
00007 // 
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00039 // 
00040 // ************************************************************************
00041 //@HEADER
00042 */
00043 
00044 #ifndef Epetra_HashTable_H_
00045 #define Epetra_HashTable_H_
00046 
00047 #include "Epetra_Object.h"
00048 
00049 class Epetra_HashTable : public Epetra_Object
00050 {
00051   struct Node
00052   {
00053      int Key;
00054      int Value;
00055      Node * Ptr;
00056 
00057      Node( const int key = 0, const int value = 0, Node * ptr = 0 )
00058      : Key(key), Value(value), Ptr(ptr) {}
00059 
00060     private:
00061      Node(const Node& src)
00062        : Key(src.Key), Value(src.Value), Ptr(src.Ptr) {}
00063 
00064     Node& operator=(const Node& src)
00065     { Key = src.Key; Value = src.Value; Ptr = src.Ptr; return(*this); }
00066   };
00067 
00068   Node ** Container_;
00069   int Size_;
00070   unsigned int Seed_;
00071 
00072   int Func( const int key ) { return (Seed_ ^ key)%Size_; }
00073      
00074  public:
00075 
00076   Epetra_HashTable( const int size, const unsigned int seed = (2654435761U) )
00077   : Container_(NULL),
00078     Size_(size),
00079     Seed_(seed)
00080   {
00081     if (size<=0)
00082       throw ReportError( "Bad Hash Table Size: " + toString(size), -1 );
00083 
00084     Container_ = new Node * [size];
00085     for( int i = 0; i < size; ++i ) Container_[i] = 0;
00086   }
00087 
00088   Epetra_HashTable( const Epetra_HashTable & obj )
00089   : Container_(NULL),
00090     Size_(obj.Size_),
00091     Seed_(obj.Seed_)
00092   {
00093     Container_ = new Node * [Size_];
00094     for( int i = 0; i < Size_; ++i ) Container_[i] = 0;
00095     for( int i = 0; i < Size_; ++i )
00096     {
00097       Node * ptr = obj.Container_[i];
00098       while( ptr ) { Add( ptr->Key, ptr->Value ); ptr = ptr->Ptr; }
00099     }
00100   }
00101 
00102   ~Epetra_HashTable()
00103   {
00104     Node * ptr1;
00105     Node * ptr2;
00106     for( int i = 0; i < Size_; ++i )
00107     {
00108       ptr1 = Container_[i];
00109       while( ptr1 ) { ptr2 = ptr1; ptr1 = ptr1->Ptr; delete ptr2; }
00110     }
00111 
00112     delete [] Container_;
00113   }
00114 
00115   void Add( const int key, const int value )
00116   {
00117     int v = Func(key);
00118     Node * n1 = Container_[v];
00119     Container_[v] = new Node(key,value,n1);
00120   }
00121 
00122   int Get( const int key )
00123   {
00124     Node * n = Container_[ Func(key) ];
00125     while( n && (n->Key != key) ) n = n->Ptr;
00126     if( n ) return n->Value;
00127     else    return -1;
00128   }
00129 
00130  private:
00131   Epetra_HashTable& operator=(const Epetra_HashTable& src)
00132     {
00133       (void)src;
00134       //not currently supported
00135       bool throw_error = true;
00136       if (throw_error) {
00137   throw ReportError("Epetra_HashTable::operator= not supported.",-1);
00138       }
00139       return(*this);
00140     }
00141 
00142 };
00143 
00144 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines