Show differencesHistory of post edits
#ActualRobot Ninja
Posted 21 December 2012 - 02:40 PM
*EDIT* The website upgrade must have made this reply's format go crazy. So I reposted below, and it looks about right.
#2Robot Ninja
Posted 21 December 2012 - 02:32 PM
<p>
*EDIT* OMG that looks horrible. I'm assuming this is a side-effect of the website upgrade. I'll try to rework this so that it looks better.
Unless there's something I'm not seeing, I couldn't figure out how to do that without dynamic allocation of the object. Is that the only way? I don't know if I really want my sorted list to hold the addresses rather than the actual objects themselves. :/ Just for reference, here are the data members I have in my hash table class: You can do the packaging in the insertion method, on their behalf. So that makes the user responsible for packaging their key and data before calling the table's Insert function. Is this how it is usually done?
std::size_t m_tableSize;std::size_t m_loadThreshold; // Tracks number of items in table.float m_loadFactor; // Holds threshold value for auto table resize.SortedList<hashcontainer<key, data="">> *m_pTable; // Table of sorted buckets/chains. Here is my constructor definition:
template <typename hashfunc="" typename="">ChainedHashTable<key, hashfunc="">::ChainedHashTable( std::size_t size /*= 37 */, float loadFactor /*= 1 */, const HashFunc &hashFunc = HashFunc() ) : m_tableSize(size), m_loadFactor(loadFactor), hash(hashFunc), m_pTable(new SortedList<hashcontainer<key, data="">>[size]){ }Here is my hash container:template <typename data="" typename="">struct HashContainer{public: /** Constructor. * @param key Key object used by hashing function. * @param data Data object stored in the hash table. */ HashContainer( const Key &key, const Data &data ) : m_key(key), m_data(data) {} /** Destructor. */ ~HashContainer(); /** Getter func. * Returns a const reference to the key. */ const Key& GetKey() { return m_key; } /** Getter func. * Returns a reference (const/non-const) to the data object stored inside. */ const Data& GetData() const { return m_data; } Data& GetData() { return m_data; } /** Operator == * Returns true if this key is equal to the other key. */ bool operator==( const HashContainer &hc ) { return this->GetKey() == hc.GetKey(); } bool operator==( const Key &key ) { return this->GetKey() == key; } /** Operator != * Returns true if this key is not equal to the other key. */ bool operator!=( const HashContainer &hc ) { return this->GetKey() != hc.GetKey(); } bool operator!=( const Key &key ) { return this->GetKey() != key; } /** Operator < * Returns true if this key is less than the other key. */ bool operator<( const HashContainer &hc ) { return this->GetKey() < hc.GetKey(); } bool operator<( const Key &key ) { return this->GetKey() > key; } /** Operator > * Returns true if this key is greater than the other key. */ bool operator>( const HashContainer &hc ) { return this->GetKey() > hc.GetKey(); } bool operator>( const Key &key ) { return this->GetKey() > key; }private: Key m_key; Data m_data;}; </typename></hashcontainer<key,></key,></typename></hashcontainer<key,></p>*EDIT* OMG that looks horrible. I'm assuming this is a side-effect of the website upgrade. I'll try to rework this so that it looks better.
#1Robot Ninja
Posted 21 December 2012 - 02:30 PM
<p>
Unless there's something I'm not seeing, I couldn't figure out how to do that without dynamic allocation of the object. Is that the only way? I don't know if I really want my sorted list to hold the addresses rather than the actual objects themselves. :/ Just for reference, here are the data members I have in my hash table class: You can do the packaging in the insertion method, on their behalf. So that makes the user responsible for packaging their key and data before calling the table's Insert function. Is this how it is usually done?
std::size_t m_tableSize;std::size_t m_loadThreshold; // Tracks number of items in table.float m_loadFactor; // Holds threshold value for auto table resize.SortedList<hashcontainer<key, data="">> *m_pTable; // Table of sorted buckets/chains. Here is my constructor definition:
template <typename hashfunc="" typename="">ChainedHashTable<key, hashfunc="">::ChainedHashTable( std::size_t size /*= 37 */, float loadFactor /*= 1 */, const HashFunc &hashFunc = HashFunc() ) : m_tableSize(size), m_loadFactor(loadFactor), hash(hashFunc), m_pTable(new SortedList<hashcontainer<key, data="">>[size]){ } Here is my hash container:template <typename data="" typename="">struct HashContainer{public: /** Constructor. * @param key Key object used by hashing function. * @param data Data object stored in the hash table. */ HashContainer( const Key &key, const Data &data ) : m_key(key), m_data(data) {} /** Destructor. */ ~HashContainer(); /** Getter func. * Returns a const reference to the key. */ const Key& GetKey() { return m_key; } /** Getter func. * Returns a reference (const/non-const) to the data object stored inside. */ const Data& GetData() const { return m_data; } Data& GetData() { return m_data; } /** Operator == * Returns true if this key is equal to the other key. */ bool operator==( const HashContainer &hc ) { return this->GetKey() == hc.GetKey(); } bool operator==( const Key &key ) { return this->GetKey() == key; } /** Operator != * Returns true if this key is not equal to the other key. */ bool operator!=( const HashContainer &hc ) { return this->GetKey() != hc.GetKey(); } bool operator!=( const Key &key ) { return this->GetKey() != key; } /** Operator < * Returns true if this key is less than the other key. */ bool operator<( const HashContainer &hc ) { return this->GetKey() < hc.GetKey(); } bool operator<( const Key &key ) { return this->GetKey() > key; } /** Operator > * Returns true if this key is greater than the other key. */ bool operator>( const HashContainer &hc ) { return this->GetKey() > hc.GetKey(); } bool operator>( const Key &key ) { return this->GetKey() > key; }private: Key m_key; Data m_data;}; </typename></hashcontainer<key,></key,></typename></hashcontainer<key,></p>