Epetra Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Epetra_HashTable.h
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Epetra: Linear Algebra Services Package
6 // Copyright 2011 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef Epetra_HashTable_H_
45 #define Epetra_HashTable_H_
46 
47 #if defined(Epetra_SHOW_DEPRECATED_WARNINGS)
48 #ifdef __GNUC__
49 #warning "The Epetra package is deprecated"
50 #endif
51 #endif
52 
53 
54 
55 #include "Epetra_Object.h"
56 
57 template<typename value_type>
58 class Epetra_HashTable : public Epetra_Object
59 {
60  struct Node
61  {
62  long long Key;
63  value_type Value;
64  Node * Ptr;
65 
66  Node( const long long key = 0, const value_type value = 0, Node * ptr = 0 )
67  : Key(key), Value(value), Ptr(ptr) {}
68 
69  private:
70  Node(const Node& src)
71  : Key(src.Key), Value(src.Value), Ptr(src.Ptr) {}
72 
73  Node& operator=(const Node& src)
74  { Key = src.Key; Value = src.Value; Ptr = src.Ptr; return(*this); }
75  };
76 
77  Node ** Container_;
78  long long Size_;
79  unsigned int Seed_;
80 
81  int Func( const long long key ) {
82  int intkey = (int) ((key & 0x000000007fffffffLL) + ((key & 0x7fffffff80000000LL) >> 31));
83  return (int) ((Seed_ ^ intkey)%Size_);
84  }
85 
86  public:
87 
88  Epetra_HashTable( const int size, const unsigned int seed = (2654435761U) )
89  : Container_(NULL),
90  Size_(size),
91  Seed_(seed)
92  {
93  if (size<=0)
94  throw ReportError( "Bad Hash Table Size: " + toString(size), -1 );
95 
96  Container_ = new Node * [size];
97  for( int i = 0; i < size; ++i ) Container_[i] = 0;
98  }
99 
101  : Container_(NULL),
102  Size_(obj.Size_),
103  Seed_(obj.Seed_)
104  {
105  Container_ = new Node * [Size_];
106  for( int i = 0; i < Size_; ++i ) Container_[i] = 0;
107  for( int i = 0; i < Size_; ++i )
108  {
109  Node * ptr = obj.Container_[i];
110  while( ptr ) { Add( ptr->Key, ptr->Value ); ptr = ptr->Ptr; }
111  }
112  }
113 
115  {
116  Node * ptr1;
117  Node * ptr2;
118  for( int i = 0; i < Size_; ++i )
119  {
120  ptr1 = Container_[i];
121  while( ptr1 ) { ptr2 = ptr1; ptr1 = ptr1->Ptr; delete ptr2; }
122  }
123 
124  delete [] Container_;
125  }
126 
127  void Add( const long long key, const value_type value )
128  {
129  int v = Func(key);
130  Node * n1 = Container_[v];
131  Container_[v] = new Node(key,value,n1);
132  }
133 
134  value_type Get( const long long key )
135  {
136  Node * n = Container_[ Func(key) ];
137  while( n && (n->Key != key) ) n = n->Ptr;
138  if( n ) return n->Value;
139  else return -1;
140  }
141 
142  private:
144  {
145  (void)src;
146  //not currently supported
147  bool throw_error = true;
148  if (throw_error) {
149  throw ReportError("Epetra_HashTable::operator= not supported.",-1);
150  }
151  return(*this);
152  }
153 
154 };
155 
156 #endif
Node(const Node &src)
value_type Get(const long long key)
Node(const long long key=0, const value_type value=0, Node *ptr=0)
Epetra_HashTable(const Epetra_HashTable &obj)
std::string toString(const int &x) const
Epetra_Object: The base Epetra class.
Definition: Epetra_Object.h:65
Epetra_HashTable & operator=(const Epetra_HashTable &src)
Node & operator=(const Node &src)
unsigned int Seed_
int Func(const long long key)
virtual int ReportError(const std::string Message, int ErrorCode) const
Error reporting method.
int n
void Add(const long long key, const value_type value)