[mathic] 26/62: Added lookup method
Doug Torrance
dtorrance-guest at moszumanska.debian.org
Wed Apr 1 11:36:20 UTC 2015
This is an automated email from the git hooks/post-receive script.
dtorrance-guest pushed a commit to branch master
in repository mathic.
commit 4a7049db0e541872019eeb0e2f39adf56a606a0d
Author: Mike Stillman <mikestillman1 at gmail.com>
Date: Thu Aug 2 09:33:52 2012 -0400
Added lookup method
---
src/mathic/HashTable.h | 25 ++++++++++++++++++++++---
src/test/HashTable.cpp | 15 +++++++++++++--
2 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/src/mathic/HashTable.h b/src/mathic/HashTable.h
index 08525a9..3505e43 100644
--- a/src/mathic/HashTable.h
+++ b/src/mathic/HashTable.h
@@ -8,6 +8,9 @@
// 3. resizing
// 4. put ASSERT's in, to check for all manner of contracts
+// Notes to self:
+// see http://attractivechaos.wordpress.com/2008/08/28/comparison-of-hash-table-libraries/
+
#ifndef MATHIC_HASHTABLE_GUARD
#define MATHIC_HASHTABLE_GUARD
@@ -66,6 +69,8 @@ namespace mathic {
// else return std::pair(true, node in the hash table).
std::pair<bool, Node *> insert(const Key &k, const Value &v);
+ Node * lookup(const Key &k);
+
// remove 'p' from the hash table. 'p' itself is not removed???!
void remove(Node *p);
@@ -87,6 +92,7 @@ namespace mathic {
private:
Node * makeNode(const Key &k, const Value &v);
+
void grow(unsigned int nbits);
// Used for consistency checking. Returns the number of nodes in the table.
@@ -153,8 +159,7 @@ namespace mathic {
template<class C>
std::pair<bool, typename HashTable<C>::Node *> HashTable<C>::insert(const Key &k, const Value &v)
{
- size_t fullHashVal = mConf.hashValue(k);
- size_t hashval = fullHashVal & mHashMask;
+ size_t hashval = mConf.hash(k) & mHashMask;
MATHIC_ASSERT(hashval < mHashTable.size());
Node *tmpNode = mHashTable[hashval];
@@ -199,6 +204,20 @@ namespace mathic {
MATHIC_ASSERT(computeNodeCount() == mNodeCount);
return std::pair<bool, Node *>(true,result);
}
+
+ template<class C>
+ typename HashTable<C>::Node * HashTable<C>::lookup(const Key &k)
+ {
+ size_t hashval = mConf.hash(k) & mHashMask;
+
+ MATHIC_ASSERT(hashval < mHashTable.size());
+ for (Node *p = mHashTable[hashval]; p != 0; p = p->next)
+ {
+ if (mConf.keysEqual(p->key, k))
+ return p;
+ }
+ return NULL;
+ }
template<class C>
void HashTable<C>::remove(Node *p)
@@ -246,7 +265,7 @@ namespace mathic {
p = p->next;
q->next = 0;
// Reinsert node. We know that it is unique
- size_t hashval = mConf.hashvalue(q->key) & mHashMask;
+ size_t hashval = mConf.hash(q->key) & mHashMask;
Node *r = mHashTable[hashval];
if (r == 0) mBinCount++;
if (r == 0 || !mAlwaysInsertAtEnd)
diff --git a/src/test/HashTable.cpp b/src/test/HashTable.cpp
index f3a9202..17bb4c1 100755
--- a/src/test/HashTable.cpp
+++ b/src/test/HashTable.cpp
@@ -12,11 +12,22 @@ namespace {
bool keysEqual(Key k1, Key k2) {return k1==k2;}
void combine(Value &a, const Value &b){a+=b;}
};
+
+ typedef mathic::HashTable<HashTableConf> HashTab;
}
TEST(HashTable, NoOp) {
HashTableConf C;
- mathic::HashTable<HashTableConf> H(C);
- ASSERT_TRUE(true);
+ HashTab H(C);
+
+ H.insert(1,3);
+ H.insert(14,7);
+ H.insert(17,7);
+ H.insert(14,4);
+
+ HashTab::Node *p = H.lookup(14);
+ ASSERT_FALSE(p == NULL);
+ ASSERT_EQ(p->key,14);
+ ASSERT_EQ(p->value,11);
};
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mathic.git
More information about the debian-science-commits
mailing list