Skip to content

Commit

Permalink
Added IntLinearProbingHashTable, which is a specialization of LinearP…
Browse files Browse the repository at this point in the history
…robingHashTable with keys of type size_t.

It extended LinearProbingHashTable, and its test code, IntLinearProbingHashTableTest, extended from LinearProbingHashTableTestBase.

Signed-off-by: saad0105050 <saad0105050@gmail.com>
  • Loading branch information
saad0105050 committed Sep 16, 2014
1 parent 8d58f38 commit e3480cf
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 289 deletions.
1 change: 1 addition & 0 deletions YASI_12/YASI_12.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<ClInclude Include="ds.dictionary.h" />
<ClInclude Include="ds.doublylinkedlist.h" />
<ClInclude Include="ds.hashtablebase.h" />
<ClInclude Include="ds.IntLinearProbingHashTable.h" />
<ClInclude Include="ds.iterator.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
</ClInclude>
Expand Down
3 changes: 3 additions & 0 deletions YASI_12/YASI_12.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,8 @@
<ClInclude Include="ds.SeparateChainingHashTable.h">
<Filter>Header Files\Dictionary</Filter>
</ClInclude>
<ClInclude Include="ds.IntLinearProbingHashTable.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
89 changes: 89 additions & 0 deletions YASI_12/ds.IntLinearProbingHashTable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#pragma once
#include "common.h"
#include "ds.LinearProbingHashTable.h"
using namespace std;

namespace yasi{
namespace ds{

// a hash table with integer key
template<
class Value = size_t,
class HashFunction = IntHashFunction<int> >
class IntLinearProbingHashTable
: public LinearProbingHashTable <
size_t, Value, HashFunction, KVPair<size_t, Value> > {

///////////////// enable testing ///////////////
friend class IntLinearProbingHashTableTest;
template<typename A, typename B, typename C>
friend class LinearProbingHashTableTestBase;

public:
typedef size_t Key;
typedef KVPair<Key, Value> EntryType; // must have a public member `key'
typedef EntryType BucketType; // each bucket holds an EntryType object
typedef EntryType Pair; // which is actually a key-value pair
typedef EntryType* BucketEntryPtr; // ptr to an entry object
typedef Key KeyType;
typedef Value ValueType;

protected:
typedef LinearProbingHashTable < size_t, Value, KVPairSimple<int, Value>, HashFunction >
base;


/////////// simplify some table accessor/modifier interface ///////////
virtual inline bool isNull(const int bucket) const{
return table[bucket].key == 0;
}
virtual inline void makeNull(const int bucket) const{
table[bucket].key = 0;
}
virtual inline bool isKeyZero(const Key* pKey) const{
return *pKey == 0;
}
virtual inline bool isKeyZero(const Key& key) const{
return key == 0;
}
virtual inline void setKeyZero(Key* pKey) const{
*pKey = 0;
}

public:

virtual ~IntLinearProbingHashTable(){}
IntLinearProbingHashTable(unsigned int logSize = INIT_LOGSIZE) :LinearProbingHashTable(logSize){}

};

////////// test IntLinearProbingHashTable
// inherit tests from base class, just redefine the types

class IntLinearProbingHashTableTest : public LinearProbingHashTableTestBase<
IntLinearProbingHashTable<int, Mod8Function<int> >,
IntLinearProbingHashTable<int, Mod16Function<int> >,
IntLinearProbingHashTable<int, Mod32Function<int> >
>{
//
protected:
typedef IntLinearProbingHashTable<int, Mod8Function<int> > IntHashTable8;
typedef IntLinearProbingHashTable<int, Mod16Function<int> > IntHashTable16;
typedef IntHashTable16 IntHashTable; // default
typedef IntLinearProbingHashTable<int, Mod32Function<int> > IntHashTable32;
typedef IntHashTable::BucketType BucketType;
typedef IntHashTable::BucketEntryPtr BucketEntryPtr;
typedef IntHashTable::Pair Pair;
public:
// inherit all public tests

};
ADD_TEST_F(IntLinearProbingHashTableTest, insert);
ADD_TEST_F(IntLinearProbingHashTableTest, copyTable);
ADD_TEST_F(IntLinearProbingHashTableTest, growCondition);
ADD_TEST_F(IntLinearProbingHashTableTest, shrinkCondition);
ADD_TEST_F(IntLinearProbingHashTableTest, isKeyZero);
ADD_TEST_F(IntLinearProbingHashTableTest, remove);

}
}
Loading

0 comments on commit e3480cf

Please sign in to comment.