A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PDBMap.cc
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #ifndef MAP_CC
20 #define MAP_CC
21 
22 #include "InterfaceFunctions.h"
23 #include "PDBMap.h"
24 
25 namespace pdb {
26 
27 // does a bunch of bit twiddling to compute a hash of an int
28 inline unsigned int newHash(unsigned int x) {
29  x = ((x >> 16) ^ x) * 0x45d9f3b;
30  x = ((x >> 16) ^ x) * 0x45d9f3b;
31  x = (x >> 16) ^ x;
32  return x;
33 }
34 
35 // computes a hash over an input character array
36 inline size_t hashMe(char* me, size_t len) {
37  size_t code = 0;
38 #ifndef HASH_FOR_TPCH
39  for (int i = 0; i < len; i += 8) {
40 
41  int low = 0;
42  int high = 0;
43 
44  for (int j = 0; j < 4; j++) {
45  if (i + j < len) {
46  low += (me[i + j] & 0xFF) << (8 * j);
47  } else {
48  low += 123 << (8 * j);
49  }
50  }
51 
52  if (len <= 4)
53  high = low;
54  else {
55  for (int j = 4; j < 8; j++) {
56  if (i + j < len) {
57  high += (me[i + j] & 0xFF) << (8 * (j - 3));
58  } else {
59  high += 97 << (8 * (j - 3));
60  }
61  }
62  }
63 
64  size_t returnVal = ((size_t)newHash(high)) << 32;
65  returnVal += newHash(low) & 0xFFFFFFFF;
66  code = code ^ returnVal;
67  }
68 #else
69  for (int i = 0; i < len; i++) {
70  code = 31 * code + me[i];
71  }
72 
73 #endif
74  return code;
75 }
76 
77 template <class KeyType, class ValueType>
78 Map<KeyType, ValueType>::Map(uint32_t initSize) {
79 
80  if (initSize < 2) {
81  std::cout << "Fatal Error: Map initialization:" << initSize
82  << " too small; must be at least one.\n";
83 
84  initSize = 2;
85  }
86 
87  // this way, we'll allocate extra bytes on the end of the array
89  size_t size = temp.getObjSize();
90  myArray = makeObjectWithExtraStorage<PairArray<KeyType, ValueType>>(size * initSize, initSize);
91 }
92 
93 template <class KeyType, class ValueType>
95 
97  size_t size = temp.getObjSize();
98  myArray = makeObjectWithExtraStorage<PairArray<KeyType, ValueType>>(size * 2, 2);
99 }
100 
101 template <class KeyType, class ValueType>
103 
104 
105 template <class KeyType, class ValueType>
106 void Map<KeyType, ValueType>::setUnused(const KeyType& clearMe) {
107  myArray->setUnused(clearMe);
108 }
109 
110 
111 template <class KeyType, class ValueType>
112 ValueType& Map<KeyType, ValueType>::operator[](const KeyType& which) {
113 
114  // JiaNote: each time we increase size only when key doesn't exist.
115  // so that we can make sure usedSlot < maxSlots each time before we invoke[] for insertion
116  // and for read-only data, we will not invoke doubleArray(), if we always invoke count() before
117  // invoke []
118  if (myArray->count(which) == 0) {
119  if (myArray->isOverFull()) {
120  Handle<PairArray<KeyType, ValueType>> temp = myArray->doubleArray();
121  myArray = temp;
122  }
123  }
124  ValueType& res = (*myArray)[which];
125  return res;
126 }
127 
128 template <class KeyType, class ValueType>
129 int Map<KeyType, ValueType>::count(const KeyType& which) {
130  return myArray->count(which);
131 }
132 
133 template <class KeyType, class ValueType>
135  return myArray->numUsedSlots();
136 }
137 
138 template <class KeyType, class ValueType>
140  PDBMapIterator<KeyType, ValueType> returnVal(myArray, true);
141  return returnVal;
142 }
143 
144 template <class KeyType, class ValueType>
146  PDBMapIterator<KeyType, ValueType> returnVal(myArray);
147  return returnVal;
148 }
149 
150 
151 template <class KeyType, class ValueType>
153  return myArray;
154 }
155 }
156 
157 #endif
size_t getObjSize()
Definition: PairArray.cc:44
~Map()
Definition: PDBMap.cc:102
ValueType & operator[](const KeyType &which)
Definition: PDBMap.cc:112
Handle< PairArray< KeyType, ValueType > > & getArray()
Definition: PDBMap.cc:152
int count(const KeyType &which)
Definition: PDBMap.cc:129
size_t hashMe(char *me, size_t len)
Definition: PDBMap.cc:36
unsigned int newHash(unsigned int x)
Definition: PDBMap.cc:28
size_t size() const
Definition: PDBMap.cc:134
void setUnused(const KeyType &clearMe)
Definition: PDBMap.cc:106
PDBMapIterator< KeyType, ValueType > begin()
Definition: PDBMap.cc:139
Map()
Definition: PDBMap.cc:94
PDBMapIterator< KeyType, ValueType > end()
Definition: PDBMap.cc:145