Jump to content

  • Log In with Google      Sign In   
  • Create Account


#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. smile.png

#2Robot Ninja

Posted 21 December 2012 - 02:32 PM

<p>&nbsp;
&nbsp;

&nbsp; 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?

&nbsp; You can do the packaging in the insertion method, on their behalf. &nbsp;


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. :/ &nbsp; Just for reference, here are the data members I have in my hash table class:
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="">&gt; *m_pTable; // Table of sorted buckets/chains.
&nbsp; Here is my constructor definition:
template <typename hashfunc="" typename="">ChainedHashTable<key, hashfunc="">::ChainedHashTable( std::size_t size /*= 37 */, float loadFactor /*= 1 */, const HashFunc &amp;hashFunc = HashFunc() ) : m_tableSize(size), m_loadFactor(loadFactor), hash(hashFunc), m_pTable(new SortedList<hashcontainer<key, data="">&gt;[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 &amp;key, const Data &amp;data ) : m_key(key), m_data(data) {} /** Destructor. */ ~HashContainer(); /** Getter func. * Returns a const reference to the key. */ const Key&amp; GetKey() { return m_key; } /** Getter func. * Returns a reference (const/non-const) to the data object stored inside. */ const Data&amp; GetData() const { return m_data; } Data&amp; GetData() { return m_data; } /** Operator == * Returns true if this key is equal to the other key. */ bool operator==( const HashContainer &amp;hc ) { return this-&gt;GetKey() == hc.GetKey(); } bool operator==( const Key &amp;key ) { return this-&gt;GetKey() == key; } /** Operator != * Returns true if this key is not equal to the other key. */ bool operator!=( const HashContainer &amp;hc ) { return this-&gt;GetKey() != hc.GetKey(); } bool operator!=( const Key &amp;key ) { return this-&gt;GetKey() != key; } /** Operator &lt; * Returns true if this key is less than the other key. */ bool operator&lt;( const HashContainer &amp;hc ) { return this-&gt;GetKey() &lt; hc.GetKey(); } bool operator&lt;( const Key &amp;key ) { return this-&gt;GetKey() &gt; key; } /** Operator &gt; * Returns true if this key is greater than the other key. */ bool operator&gt;( const HashContainer &amp;hc ) { return this-&gt;GetKey() &gt; hc.GetKey(); } bool operator&gt;( const Key &amp;key ) { return this-&gt;GetKey() &gt; key; }private: Key m_key; Data m_data;};
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</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>&nbsp;
&nbsp;

&nbsp; 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?

&nbsp; You can do the packaging in the insertion method, on their behalf. &nbsp;
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. :/ &nbsp; Just for reference, here are the data members I have in my hash table class:
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="">&gt; *m_pTable; // Table of sorted buckets/chains.
&nbsp; Here is my constructor definition:
template <typename hashfunc="" typename="">ChainedHashTable<key, hashfunc="">::ChainedHashTable( std::size_t size /*= 37 */, float loadFactor /*= 1 */, const HashFunc &amp;hashFunc = HashFunc() ) : m_tableSize(size), m_loadFactor(loadFactor), hash(hashFunc), m_pTable(new SortedList<hashcontainer<key, data="">&gt;[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 &amp;key, const Data &amp;data ) : m_key(key), m_data(data) {} /** Destructor. */ ~HashContainer(); /** Getter func. * Returns a const reference to the key. */ const Key&amp; GetKey() { return m_key; } /** Getter func. * Returns a reference (const/non-const) to the data object stored inside. */ const Data&amp; GetData() const { return m_data; } Data&amp; GetData() { return m_data; } /** Operator == * Returns true if this key is equal to the other key. */ bool operator==( const HashContainer &amp;hc ) { return this-&gt;GetKey() == hc.GetKey(); } bool operator==( const Key &amp;key ) { return this-&gt;GetKey() == key; } /** Operator != * Returns true if this key is not equal to the other key. */ bool operator!=( const HashContainer &amp;hc ) { return this-&gt;GetKey() != hc.GetKey(); } bool operator!=( const Key &amp;key ) { return this-&gt;GetKey() != key; } /** Operator &lt; * Returns true if this key is less than the other key. */ bool operator&lt;( const HashContainer &amp;hc ) { return this-&gt;GetKey() &lt; hc.GetKey(); } bool operator&lt;( const Key &amp;key ) { return this-&gt;GetKey() &gt; key; } /** Operator &gt; * Returns true if this key is greater than the other key. */ bool operator&gt;( const HashContainer &amp;hc ) { return this-&gt;GetKey() &gt; hc.GetKey(); } bool operator&gt;( const Key &amp;key ) { return this-&gt;GetKey() &gt; key; }private: Key m_key; Data m_data;};
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</typename></hashcontainer<key,></key,></typename></hashcontainer<key,></p>

PARTNERS