![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsHashTableBySTLVector.cpp 00003 00004 Class TAPsHashTableBySTLVector is a hash table created from STL vector class. 00005 00006 SUKITTI PUNAK (04/08/2005) 00007 UPDATE (04/15/2005) 00008 ******************************************************************************/ 00009 #include "TAPsHashTableBySTLVector.hpp" 00010 // Using Inclusion Model (i.e. definitions are included in declarations) 00011 // (this name.cpp is included in name.hpp) 00012 // Each friend is defined directly inside its declaration. 00013 00014 BEGIN_NAMESPACE_TAPs__DS 00015 //============================================================================= 00016 // Constructor and Destructor 00017 //----------------------------------------------------------------------------- 00018 template <typename T> 00019 HashTableBySTLVector<T>::HashTableBySTLVector ( int numberOfBuckets ) 00020 : m_iNoOfBuckets( numberOfBuckets ) 00021 { 00022 m_pvBucketNo = new std::vector<T>[m_iNoOfBuckets]; 00023 } 00024 //----------------------------------------------------------------------------- 00025 template <typename T> 00026 HashTableBySTLVector<T>::~HashTableBySTLVector () 00027 { 00028 if ( m_pvBucketNo != NULL ) { 00029 delete [] m_pvBucketNo; 00030 } 00031 m_pvBucketNo = NULL; 00032 } 00033 //----------------------------------------------------------------------------- 00034 //============================================================================= 00035 // Operation(s) Unsorted Elements 00036 //----------------------------------------------------------------------------- 00037 // Insert in into bucket# 00038 // All values, assume numbers, in bucket will not be sorted. 00039 // Return the inserted index 00040 template <typename T> 00041 int HashTableBySTLVector<T>::Insert ( T in, int bucketNo ) 00042 { 00043 m_pvBucketNo[ bucketNo ].push_back( in ); 00044 return static_cast<int>( m_pvBucketNo[ bucketNo ].size() - 1 ); 00045 } 00046 //----------------------------------------------------------------------------- 00047 // Search key in bucket# with unsorted values 00048 // Return the first index that contains the key, else return -1 00049 template <typename T> 00050 int HashTableBySTLVector<T>::Search ( T key, int bucketNo ) 00051 { 00052 if ( !m_pvBucketNo[ bucketNo ].empty() ) { 00053 for ( int i = 0; i < m_pvBucketNo[ bucketNo ].size(); ++i ) { 00054 if ( key == m_pvBucketNo[ bucketNo ][ i ] ) { 00055 return i; 00056 } 00057 } 00058 } 00059 return -1; // empty bucket or not found 00060 } 00061 //============================================================================= 00062 // Operation(s) Sorted Elements 00063 //----------------------------------------------------------------------------- 00064 // Insert in into bucket# 00065 // All values, assume numbers, in bucket will not be sorted. 00066 // Return the inserted index 00067 template <typename T> 00068 int HashTableBySTLVector<T>::InsertSort ( T in, int bucketNo ) 00069 { 00070 if ( !m_pvBucketNo[ bucketNo ].empty() ) { 00071 std::vector<T>::iterator pos = m_pvBucketNo[ bucketNo ].begin(); 00072 for ( int i = 0; i < static_cast<int>( m_pvBucketNo[ bucketNo ].size() ); ++i, ++pos ) { 00073 if ( in < m_pvBucketNo[ bucketNo ][ i ] ) { 00074 m_pvBucketNo[ bucketNo ].insert( pos, in ); 00075 return i; 00076 } 00077 } 00078 } 00079 m_pvBucketNo[ bucketNo ].push_back( in ); 00080 return static_cast<int>( m_pvBucketNo[ bucketNo ].size() - 1 ); 00081 } 00082 //----------------------------------------------------------------------------- 00083 // Search key in bucket# with sorted values 00084 // Return the first index that contains the key, else return -1 00085 template <typename T> 00086 int HashTableBySTLVector<T>::SearchSort ( T key, int bucketNo ) 00087 { 00088 if ( !m_pvBucketNo[ bucketNo ].empty() ) { 00089 for ( int i = 0; i < m_pvBucketNo[ bucketNo ].size(); ++i ) { 00090 if ( key >= m_pvBucketNo[ bucketNo ][ i ] ) { 00091 if ( key == m_pvBucketNo[ bucketNo ][ i ] ) return i; 00092 else return -1; 00093 } 00094 } 00095 } 00096 return -1; // empty bucket or not found 00097 } 00098 //----------------------------------------------------------------------------- 00099 //============================================================================= 00100 END_NAMESPACE_TAPs__DS 00101 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00102 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8