<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[hashtable]]></title><description><![CDATA[<p>hi,</p>
<p>macht es sinn den key als array von pointer anzulegen...oder besser std::string?</p>
<p>beim aufrufen von add sollte ich wenn falls der hash index belegt ist, so lange suchen bis ich status_arr == 0 oder status_arr == -1 finde?</p>
<pre><code class="language-cpp">/* Hashtable */
class hashtable
{
private:
	char **key_arr;
	unsigned int *value_arr;
	int *status_arr;
	unsigned int length;

private:
	unsigned int hash(const char *key)
	{
		int sum = 0;

		for(unsigned int i = 0; i &lt; strlen(key); i++)
		{
			sum = sum + static_cast&lt;unsigned int&gt;(key[i]);
		}

		return sum = sum % length;
	}

public:
	hashtable(int n): length(n)
	{
		value_arr = new unsigned int[n];
		status_arr = new int[n];
		key_arr = new char*[n];

		for(unsigned int i = 0; i &lt; n; i++)
		{
			status_arr[i] = 0;
		}
	}

	void add(const char *key, const unsigned int &amp;value)
	{
		unsigned int hash_index = hash(key);

		if(status_arr[hash_index] != 1)
		{
			key_arr[hash_index] = const_cast&lt;char*&gt;(key);
			value_arr[hash_index] = value;
			status_arr[hash_index] = 1;
		}
		else 
		{
			hash_index++;
			key_arr[hash_index] = const_cast&lt;char*&gt;(key);
			value_arr[hash_index] = value;
			status_arr[hash_index] = 1;
		}
	}

	unsigned int item(const char *key)
	{
		unsigned int hash_index = hash(key);
		return value_arr[hash_index];
	}

	unsigned int remove(const char *key)
	{
		unsigned int hash_index = hash(key);
		value_arr[hash_index] = 0;
		status_arr[hash_index] = -1;
	}

	~hashtable()
	{
		delete[] value_arr;
		delete[] key_arr;
	}
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/299477/hashtable</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 22:51:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/299477.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 Feb 2012 22:11:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to hashtable on Sat, 11 Feb 2012 22:11:51 GMT]]></title><description><![CDATA[<p>hi,</p>
<p>macht es sinn den key als array von pointer anzulegen...oder besser std::string?</p>
<p>beim aufrufen von add sollte ich wenn falls der hash index belegt ist, so lange suchen bis ich status_arr == 0 oder status_arr == -1 finde?</p>
<pre><code class="language-cpp">/* Hashtable */
class hashtable
{
private:
	char **key_arr;
	unsigned int *value_arr;
	int *status_arr;
	unsigned int length;

private:
	unsigned int hash(const char *key)
	{
		int sum = 0;

		for(unsigned int i = 0; i &lt; strlen(key); i++)
		{
			sum = sum + static_cast&lt;unsigned int&gt;(key[i]);
		}

		return sum = sum % length;
	}

public:
	hashtable(int n): length(n)
	{
		value_arr = new unsigned int[n];
		status_arr = new int[n];
		key_arr = new char*[n];

		for(unsigned int i = 0; i &lt; n; i++)
		{
			status_arr[i] = 0;
		}
	}

	void add(const char *key, const unsigned int &amp;value)
	{
		unsigned int hash_index = hash(key);

		if(status_arr[hash_index] != 1)
		{
			key_arr[hash_index] = const_cast&lt;char*&gt;(key);
			value_arr[hash_index] = value;
			status_arr[hash_index] = 1;
		}
		else 
		{
			hash_index++;
			key_arr[hash_index] = const_cast&lt;char*&gt;(key);
			value_arr[hash_index] = value;
			status_arr[hash_index] = 1;
		}
	}

	unsigned int item(const char *key)
	{
		unsigned int hash_index = hash(key);
		return value_arr[hash_index];
	}

	unsigned int remove(const char *key)
	{
		unsigned int hash_index = hash(key);
		value_arr[hash_index] = 0;
		status_arr[hash_index] = -1;
	}

	~hashtable()
	{
		delete[] value_arr;
		delete[] key_arr;
	}
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2179963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2179963</guid><dc:creator><![CDATA[waldiwaldi]]></dc:creator><pubDate>Sat, 11 Feb 2012 22:11:51 GMT</pubDate></item><item><title><![CDATA[Reply to hashtable on Tue, 14 Feb 2012 13:54:14 GMT]]></title><description><![CDATA[<p>waldiwaldi schrieb:</p>
<blockquote>
<p>hi,</p>
<p>macht es sinn den key als array von pointer anzulegen...oder besser std::string?</p>
<p>beim aufrufen von add sollte ich wenn falls der hash index belegt ist, so lange suchen bis ich status_arr == 0 oder status_arr == -1 finde?</p>
<pre><code class="language-cpp">/* Hashtable */
class hashtable
{
private:
	char **key_arr;
	unsigned int *value_arr;
	int *status_arr;
	unsigned int length;

private:
	unsigned int hash(const char *key)
	{
		int sum = 0;
		
		for(unsigned int i = 0; i &lt; strlen(key); i++)
		{
			sum = sum + static_cast&lt;unsigned int&gt;(key[i]);
		}

		return sum = sum % length;
	}

public:
	hashtable(int n): length(n)
	{
		value_arr = new unsigned int[n];
		status_arr = new int[n];
		key_arr = new char*[n];

		for(unsigned int i = 0; i &lt; n; i++)
		{
			status_arr[i] = 0;
		}
	}

	void add(const char *key, const unsigned int &amp;value)
	{
		unsigned int hash_index = hash(key);

		if(status_arr[hash_index] != 1)
		{
			key_arr[hash_index] = const_cast&lt;char*&gt;(key);
			value_arr[hash_index] = value;
			status_arr[hash_index] = 1;
		}
		else 
		{
			hash_index++;
			key_arr[hash_index] = const_cast&lt;char*&gt;(key);
			value_arr[hash_index] = value;
			status_arr[hash_index] = 1;
		}
	}

	unsigned int item(const char *key)
	{
		unsigned int hash_index = hash(key);
		return value_arr[hash_index];
	}

	unsigned int remove(const char *key)
	{
		unsigned int hash_index = hash(key);
		value_arr[hash_index] = 0;
		status_arr[hash_index] = -1;
	}

	~hashtable()
	{
		delete[] value_arr;
		delete[] key_arr;
	}
};
</code></pre>
</blockquote>
<p>Ganz schlechte Implementierung:<br />
1. schlechte Hashfunktion, da der Wert den Rahmen sprengen könnte besser ist XOR!<br />
2. XOR ist deswegen auch besser, weil man ähnliche Hashwerte gerne weitgestreut bekommen möchte (nicht wichtig, aber meistens ein Gütekriterium) 'hallo' und 'halol' haben gleiche Werte bei dir!<br />
3. NIEMALS CONST CAST!!!!!!!!!!!!!<br />
4. wenn du ein char übergibst musst du es entweder ganz kopieren aber nicht nur den pointer! Folgender Aufruf erzeugt einen Pointer der dann ins Nirwana zeigt tbl.add(&quot;Hallo&quot;, 5);<br />
5. die item und remove methode muss überprüfen ob der key wirklich matched, oder nur ein verschobenes item wegen überlauf ist!!!!!!!</p>
<p>Ich denke, dass du bevor du mit Hashing arbeiten kannst erstmal noch ein paar Stunden in Pointer, Referenzen, new, malloc, etc investieren solltest! Hier fehlen die Basics noch finde ich!<br />
Deine Variante würde schon gehen, aber wenn du const übergibst, müssen die auch const gespeichert werden! Geht alles, ist halt komisch.</p>
<p>Besser wäre, wenn du Key-Objekte übergibst, die sich selber um die Speicherung kümmern! Vielleicht sogar Objekte mit RefCounter. Dann kannst du deine Hashtable gleich als template definieren!</p>
<pre><code class="language-cpp">/* Hashtable */
template&lt;class T&gt;
class hashtable
{
private:
struct item
{
item():init(false){}
T item;
char *key;
bool init;
};

union hsh
{
unsigned int h;
char bytes[sizeof(unsigned int)];
};

vector&lt;item&gt; m_table;

private:
unsigned int hash(const char *key)
{

  hsh h;
  h.h = 0;

  for(unsigned int i = 0; i &lt; strlen(key); i++)
    h.bytes[i%sizeof(unsigned int)] ^= key[i];

  return hsh.h;
}

};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2181027</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2181027</guid><dc:creator><![CDATA[PhilippHToner]]></dc:creator><pubDate>Tue, 14 Feb 2012 13:54:14 GMT</pubDate></item><item><title><![CDATA[Reply to hashtable on Tue, 14 Feb 2012 14:56:22 GMT]]></title><description><![CDATA[<p>PhilippHToner schrieb:</p>
<blockquote>
<p>3. NIEMALS CONST CAST!!!!!!!!!!!!!</p>
</blockquote>
<p>So n Blödsinn.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2181047</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2181047</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Tue, 14 Feb 2012 14:56:22 GMT</pubDate></item><item><title><![CDATA[Reply to hashtable on Tue, 14 Feb 2012 15:09:53 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>PhilippHToner schrieb:</p>
<blockquote>
<p>3. NIEMALS CONST CAST!!!!!!!!!!!!!</p>
</blockquote>
<p>So n Blödsinn.</p>
</blockquote>
<p>Jetzt weiß ich wieso dich die Leute hier nicht mögen :-D! Keine Ahnung vom Hashing wahrscheinlich, aber const casten <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
<pre><code class="language-cpp">const char str[100]=&quot;stupid&quot;;

strcpy(const_cast&lt;char *&gt;(str), &quot;God&quot;);
cout &lt;&lt; &quot;I'm &quot; &lt;&lt; str;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2181058</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2181058</guid><dc:creator><![CDATA[PhilippHToner]]></dc:creator><pubDate>Tue, 14 Feb 2012 15:09:53 GMT</pubDate></item><item><title><![CDATA[Reply to hashtable on Tue, 14 Feb 2012 15:27:34 GMT]]></title><description><![CDATA[<p>Deine Aussage ist generell einfach Mist, da es Anwendungsfälle für const_cast gibt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2181074</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2181074</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Tue, 14 Feb 2012 15:27:34 GMT</pubDate></item><item><title><![CDATA[Reply to hashtable on Tue, 14 Feb 2012 15:30:26 GMT]]></title><description><![CDATA[<p>Ich gestehe, ich habe auch schon <code>const_cast</code> benutzt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2181076</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2181076</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Tue, 14 Feb 2012 15:30:26 GMT</pubDate></item><item><title><![CDATA[Reply to hashtable on Sun, 26 Feb 2012 03:13:42 GMT]]></title><description><![CDATA[<p>hi,<br />
ich weiss die hash funktion ist nicht gerade geeignet hier...aber was mache ich wenn die hash funktion einen hash liefert und dieser schon in der tabelle vorhanden ist? wie finde ich das richtig element wieder?</p>
<pre><code class="language-cpp">class hashtable
{
private:
	string *key_arr;
	unsigned int *value_arr;
	int *status_arr; // contains the status of the table at index pos: deleted, empty or used
	unsigned int length;

private:
	unsigned int hash(string key)
	{
		int sum = 0;

		for(unsigned int i = 0; i &lt; key.length(); i++)
		{
			sum = sum + static_cast&lt;unsigned int&gt;(key[i]);
		}

		return sum = key.length() % length;
	}

public:
	hashtable(int n): length(n)
	{
		value_arr = new unsigned int[n];
		status_arr = new int[n];
		key_arr = new string[n];

		for(unsigned int i = 0; i &lt; n; i++)
		{
			status_arr[i] = 0;
		}
	}

	void add(const string &amp;key, const unsigned int &amp;value)
	{
		unsigned int hash_index = hash(key);
		unsigned int j = 0;

		if(status_arr[hash_index] != 1)
		{
			key_arr[hash_index] = key;
			value_arr[hash_index] = value;
			status_arr[hash_index] = 1;
		}
		else
		{
			do {
				hash_index ++;
				hash_index = hash_index % length;

				if(status_arr[hash_index] != 1)
				{
					key_arr[hash_index] = key;
					value_arr[hash_index] = value;
					status_arr[hash_index] = 1;

					return;
				}
				else
				{
					j++;
				}
			} while(j &lt; length);

			throw(&quot;Error collision&quot;);
		}
	}

	bool contains(const string &amp;key)
	{
		unsigned int hash_index = hash(key);
		if(status_arr[hash_index] == 1) 
		{
			return true;
		}
		return false;
	}

	unsigned int item(const string &amp;key)
	{
		unsigned int hash_index = hash(key);
		if(status_arr[hash_index] == 1) 
		{
			return value_arr[hash_index];
		}
		throw(&quot;Error calling item&quot;);
	}

	bool remove(const string &amp;key)
	{
		if(contains(key))
		{
			unsigned int hash_index = hash(key);
			value_arr[hash_index] = 0;
			status_arr[hash_index] = -1;

			return true;
		}

		return false;
	}

	~hashtable()
	{
		delete[] value_arr;
		delete[] key_arr;
		delete[] status_arr;
	}
};

int main()
{
	hashtable h(5);
	h.add(&quot;Hugo&quot;, 5);
	h.add(&quot;Gerald&quot;, 10);
	h.add(&quot;Birgit&quot;, 11);
	h.add(&quot;Birgig&quot;, 11);
	h.add(&quot;Birgia&quot;, 11);

	cout &lt;&lt; h.item(&quot;Birgit&quot;) &lt;&lt; endl;
	cout &lt;&lt; h.item(&quot;Birgig&quot;) &lt;&lt; endl;
	cout &lt;&lt; h.item(&quot;Birgia&quot;) &lt;&lt; endl;

	h.remove(&quot;Birgit&quot;);

	cout &lt;&lt; h.item(&quot;Birgig&quot;) &lt;&lt; endl;
	cout &lt;&lt; h.item(&quot;Birgia&quot;) &lt;&lt; endl;

        return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2185860</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185860</guid><dc:creator><![CDATA[waldiwaldi]]></dc:creator><pubDate>Sun, 26 Feb 2012 03:13:42 GMT</pubDate></item><item><title><![CDATA[Reply to hashtable on Sun, 26 Feb 2012 05:34:19 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Deine Aussage ist generell einfach Mist, da es Anwendungsfälle für const_cast gibt.</p>
</blockquote>
<p>knivil schrieb:</p>
<blockquote>
<p>Ich gestehe, ich habe auch schon <code>const_cast</code> benutzt.</p>
</blockquote>
<p>Wozu braucht ihr das, außer für fremde Libs die nicht const correct sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2185863</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185863</guid><dc:creator><![CDATA[const concret]]></dc:creator><pubDate>Sun, 26 Feb 2012 05:34:19 GMT</pubDate></item><item><title><![CDATA[Reply to hashtable on Sun, 26 Feb 2012 08:34:34 GMT]]></title><description><![CDATA[<p>const concret schrieb:</p>
<blockquote>
<p>314159265358979 schrieb:</p>
<blockquote>
<p>Deine Aussage ist generell einfach Mist, da es Anwendungsfälle für const_cast gibt.</p>
</blockquote>
<p>knivil schrieb:</p>
<blockquote>
<p>Ich gestehe, ich habe auch schon <code>const_cast</code> benutzt.</p>
</blockquote>
<p>Wozu braucht ihr das, außer für fremde Libs die nicht const correct sind?</p>
</blockquote>
<p>Das taucht zum Beispiel gerne mal auf, wenn man einen Iterator und einen Const_Iterator selber implementiert, aber nicht alles doppelt schreiben möchte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2185865</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185865</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 26 Feb 2012 08:34:34 GMT</pubDate></item><item><title><![CDATA[Reply to hashtable on Sun, 26 Feb 2012 12:35:15 GMT]]></title><description><![CDATA[<p>waldiwaldi schrieb:</p>
<blockquote>
<p>hi,<br />
ich weiss die hash funktion ist nicht gerade geeignet hier...aber was mache ich wenn die hash funktion einen hash liefert und dieser schon in der tabelle vorhanden ist? wie finde ich das richtig element wieder?</p>
</blockquote>
<p>Alles schreiben nur noch über den const_cast schmarrn, zurück zu dir:</p>
<p>Also es ist ganz normal, dass es zu einer sog. Kollision kommt. Du möchtest N Elemente auf M abbilden. Für N &lt;= M ist das theoretisch möglich, doch nur wenn es eine Hashfunktion gibt, die das in konstanter Laufzeit schafft, also unabhängig von N ist. Es fängt schonmal so an, dass die Elemente vielleicht gar nicht durchnummeriert sind und keine direkt (ideale) Abbildung ohne Kollision möglich ist. Deshalb wählt man ein M relativ größer als N (wobei die Größe davon abhängig ist, wie wahrscheinlich man sich eine nicht konstante Laufzeit doch erlauben kann). Also wenn du so willst ist Hashing zu einem Prozentsatz konstant und zu einem Prozentsatz Laufzeit linear (Überlaufstrategie). Im Grunde genommen instabil.</p>
<p>Deshalb ist die richtige Wahl einer guten Hashfunktion so wichtig. Und nun zurück zu deiner Frage: Bei einer Kollision kommt die Überlaufstrategie zum Einsatz. Es gibt verschiedene Strategien, aber für gute Hashfunktion und großen M kann man sich eine lineare Suche erlauben. Z.B. fügt man ein Element bei einer Kollision in den nächste freie Feld. Es gibt verschiedene Hasingmethoden, du musst hier abwägen, was du brauchst! Offenes Hashing, geschlossenes Hashing, distributed Hashing, uvm. einfach googlen!</p>
<p>Dadurch, dass die Hashfunktion auch ein anderes Element auf den gleichen HAshwert abbilden kann, musst du immer nochmal den direkten Test machen ob es wirklich das Element ist, denn Hashing hilft die genau genommen nur über einen Zugriffspfad, aber sagt dir nie 100% ob es wirklich das ELement ist!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2185922</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185922</guid><dc:creator><![CDATA[PhilippHToner]]></dc:creator><pubDate>Sun, 26 Feb 2012 12:35:15 GMT</pubDate></item></channel></rss>