More importantly, how do you get a nice hash key?<br><br>You can iterator over contents on a simple array, store the keys there like:<br><br>string hashKeys[];<br><br>That's what you usually use to iterate over when doing hashmaps anyway (if you want to iterate over contents what do you need the hash for?)<br>
<br>Here's a pseudocode attempt at a hashset (I keep forgetting the subtleties of @ and the autogrowing array syntax is missing from the annoying manual):<br><br>class HashElement {<br> fun int hashCode();<br>}<br>
<br>class HashSet {<br> HashElement elements[][];<br><br> fun int contains(HashElement @ element) {<br> elements[element.hashCode()] @=> HashElement @ hashedList[];<br> for (0 => int i; i < hashedList.cap(); i++) {<br>
if (hashedList[i] == element) {<br> return 1;<br> }<br> }<br> return 0;<br> }<br><br> fun void insert(HashElement @ element) {<br> elements[element.hashCode()] @=> HashElement @ hashedList[];<br>
if (hashedList == null) {<br> HashElement[1] @=> hashedList;<br> hashedList @=> elements[element.hashCode()];<br> }<br> element +=> hashedList[]; // keep forgetting the syntax for autogrowing the array<br>
}<br>}<br><br>The elements you want to put in the hash set need to extend HashElement.<br><br>/Stefan<br><br><div class="gmail_quote">2009/9/30 Robert Poor <span dir="ltr"><<a href="mailto:rdpoor@gmail.com">rdpoor@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div style="word-wrap: break-word;">Hans:<div><br></div><div>Of course I'd love to use a hashmap. But how do you get one in Chuck? AFIK, the existing chuckain "hash array" lacks a means to iterate over its contents.</div>
<div><br></div><div>- Rob</div><div class="im"><div><br><div><div>On 30 Sep 2009, at 02:03, Hans Aberg wrote:</div><br><blockquote type="cite"><div>On 30 Sep 2009, at 02:13, Robert Poor wrote:<br><br><blockquote type="cite">
I guess the question should be: what's the fastest way to maintain a *set* of objects (i.e. a collection in which an object may only appear once) with the usual operations for insertion, deletion and iteration?<br></blockquote>
<br>For lookup tables, if you do not need to compare the keys, a hash map is fastest - time complexity O(1), otherwise a balanced tree (like C++ std::map) - complexity O(log n). There might be some C++ hash map classes at <<a href="http://www.boost.org/" target="_blank">http://www.boost.org/</a>>. But if n is small and use not too intense, just about any container will do.<br>
<br> Hans<font color="#000000"><font color="#144fae"><br></font></font></div></blockquote></div><div> <span style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div style="word-wrap: break-word;">
<div><span style="font-size: 12px;"><div style="word-wrap: break-word;"><div><span style="font-size: medium;"><br></span></div></div></span></div></div></span></div></div></div></div><br>_______________________________________________<br>
chuck-users mailing list<br>
<a href="mailto:chuck-users@lists.cs.princeton.edu">chuck-users@lists.cs.princeton.edu</a><br>
<a href="https://lists.cs.princeton.edu/mailman/listinfo/chuck-users" target="_blank">https://lists.cs.princeton.edu/mailman/listinfo/chuck-users</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>Release me, insect, or I will destroy the Cosmos!<br>