Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_HashUtils.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TEUCHOS_HASHUTILS_H
00030 #define TEUCHOS_HASHUTILS_H
00031 
00036 #include "Teuchos_ConfigDefs.hpp"
00037 
00038 namespace Teuchos
00039 {
00040   using std::string;
00041 
00047   class HashUtils
00048     {
00049     public:
00050       /* Get the next prime in a sequence of hashtable sizes */
00051       static int nextPrime(int newCapacity);
00052 
00053     private:
00054 
00055       // sequence of primes generated via mathematica:
00056       // Table[Prime[Round[1.5^x]], {x, 8, 36}]
00057       static const int primeCount_;
00058       static const int primes_[];
00059       /*={101, 163, 271, 443, 733, 1187, 1907, 3061,
00060         4919, 7759, 12379, 19543, 30841, 48487, 75989,
00061         119089, 185971, 290347, 452027, 703657, 1093237,
00062         1695781, 2627993, 4067599, 6290467, 9718019,
00063         15000607, 23133937, 35650091};*/
00064     };
00065 
00069   template <class T> int hashCode(const T& x);
00070 
00074   template <> inline int hashCode(const int& x) 
00075     {
00076       return x;
00077     }
00078 
00082   template <> inline int hashCode(const double& x)
00083     {
00084       return (int) x;
00085     }
00086 
00090   template <> inline int hashCode(const bool& x)
00091     {
00092       return (int) x;
00093     }
00094 
00095 
00099   template <> inline int hashCode(const std::string& x)
00100     {
00101       const char* str = x.c_str();
00102       int len = x.length();
00103       int step = len/4 + 1;
00104       int base = 1;
00105       int rtn = 0;
00106 
00107       for (int i=0; i<len/2; i+=step)
00108         {
00109           rtn += base*(int) str[i];
00110           base *= 128;
00111           rtn += base*(int) str[len-i-1];
00112           base *= 128;
00113         }
00114 
00115       return rtn;
00116     }
00117 
00118 
00119 
00120 }
00121 #endif // TEUCHOS_HASHUTILS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines