title: How to implement the Hashable Protocol in Swift for an Int array (a custom string struct)
body: <p>I am making a structure that acts like a <code>String</code>, except that it only deals with Unicode UTF-32 scalar values. Thus, it is an array of <code>UInt32</code>. (See <a href="https://stackoverflow.com/questions/31272561/working-with-unicode-code-points-in-swift">this question</a> for more background.)</p>
  
  <h1>What I want to do</h1>
  
  <p>I want to be able to use my custom <code>ScalarString</code> struct as a key in a dictionary. For example:</p>
  
  <pre><code>var suffixDictionary = [ScalarString: ScalarString]() // Unicode key, rendered glyph value
  
  // populate dictionary
  suffixDictionary[keyScalarString] = valueScalarString
  // ...
  
  // check if dictionary contains Unicode scalar string key
  if let renderedSuffix = suffixDictionary[unicodeScalarString] {
      // do something with value
  }
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>In order to do that, <code>ScalarString</code> needs to implement the <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_Hashable_Protocol/index.html" rel="noreferrer">Hashable Protocol</a>. I thought I would be able to do something like this:</p>
  
  <pre><code>struct ScalarString: Hashable {
  
      private var scalarArray: [UInt32] = []
  
      var hashValue : Int {
          get {
              return self.scalarArray.hashValue // error
          }
      }
  }
  
  func ==(left: ScalarString, right: ScalarString) -&gt; Bool {
      return left.hashValue == right.hashValue
  }
  </code></pre>
  
  <p>but then I discovered that <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_Array_Structure/index.html" rel="noreferrer">Swift arrays</a> don't have a <code>hashValue</code>. </p>
  
  <h1>What I read</h1>
  
  <p>The article <a href="http://letvargo.mooo.com/strategies-for-implementing-the-hashable-protocol-in-swift/" rel="noreferrer">Strategies for Implementing the Hashable Protocol in Swift</a> had a lot of great ideas, but I didn't see any that seemed like they would work well in this case. Specifically,</p>
  
  <ul>
  <li><em>Object property</em> (array is does not have <code>hashValue</code>)</li>
  <li><em>ID property</em> (not sure how this could be implemented well)</li>
  <li><em>Formula</em> (seems like any formula for a string of 32 bit integers would be processor heavy and have lots of integer overflow)</li>
  <li><em>ObjectIdentifier</em> (I'm using a struct, not a class)</li>
  <li><em>Inheriting from NSObject</em> (I'm using a struct, not a class)</li>
  </ul>
  
  <p>Here are some other things I read:</p>
  
  <ul>
  <li><a href="http://samuelmullen.com/2014/10/implementing_swifts_hashable_protocol/" rel="noreferrer">Implementing Swift's Hashable Protocol</a></li>
  <li><a href="http://nshipster.com/swift-comparison-protocols/" rel="noreferrer">Swift Comparison Protocols</a></li>
  <li><a href="https://en.wikipedia.org/wiki/Perfect_hash_function" rel="noreferrer">Perfect hash function</a></li>
  <li><a href="http://digitalleaves.com/blog/2014/11/membership-of-custom-objects-in-swift-arrays-and-dictionaries/" rel="noreferrer">Membership of custom objects in Swift Arrays and Dictionaries</a></li>
  <li><a href="http://www.swiftcoder.info/dev/codefellows/2014/8/2/how-to-implement-hashable-for-your-custom-class" rel="noreferrer">How to implement Hashable for your custom class</a></li>
  <li><a href="https://stackoverflow.com/questions/24239295/writing-a-good-hashable-implementation-in-swift">Writing a good Hashable implementation in Swift</a></li>
  </ul>
  
  <h1>Question</h1>
  
  <p>Swift Strings have a <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_String_Structure/index.html#//apple_ref/swift/structp/String/s:vSS9hashValueSi" rel="noreferrer"><code>hashValue</code></a> property, so I know it is possible to do.</p>
  
  <p>How would I create a <code>hashValue</code> for my custom structure?</p>
  
  <h1>Updates</h1>
  
  <p><strong>Update 1:</strong> I would like to do something that does not involve converting to <code>String</code> and then using <code>String</code>'s <code>hashValue</code>. My whole point for making my own structure was so that I could avoid doing lots of <code>String</code> conversions. <code>String</code> gets it's <code>hashValue</code> from somewhere. It seems like I could get it using the same method.</p>
  
  <p><strong>Update 2:</strong> I've been looking into the implementation of string hash codes algorithms from other contexts. I'm having a little difficulty knowing which is best and expressing them in Swift, though.</p>
  
  <ul>
  <li><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#hashCode()" rel="noreferrer">Java <code>hashCode</code> algorithm</a></li>
  <li><a href="http://www.cse.yorku.ca/~oz/hash.html" rel="noreferrer">C algorithms</a> </li>
  <li><a href="https://stackoverflow.com/questions/7666509/hash-function-for-string">hash function for string</a> (SO question and answers in C)</li>
  <li><a href="http://research.cs.vt.edu/AVresearch/hashing/index.php" rel="noreferrer">Hashing tutorial</a> (Virginia Tech Algorithm Visualization Research Group)</li>
  <li><a href="http://www.partow.net/programming/hashfunctions/" rel="noreferrer">General Purpose Hash Function Algorithms</a></li>
  </ul>
  
  <p><strong>Update 3</strong></p>
  
  <p>I would prefer not to import any external frameworks unless that is the recommended way to go for these things.</p>
  
  <p>I submitted a possible solution using the DJB Hash Function.</p>