Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

A while back I made a custom String struct (see github repo) because of the difficulties in dealing with Mongolian Unicode rendering when using Swift String or NSString (see this questionthis question and the accepted answer for more details). The struct is an array of UInt32 so that it represents a string of Unicode scalar values.

The most difficult part (see herehere and herehere for my StackOverflow questions at the time) was making it conform to the Hashable Protocol so that it could be used as a Dictionary key.

For all that I can tell, everything has been working fine. However, since there was not a lot of documentation and guidance available about implementing the Hashable Protocol, I wanted to make sure that I did it right. Here is the relevant code:

struct ScalarString: SequenceType, Hashable, CustomStringConvertible {
    
    private var scalarArray: [UInt32] = []

    // ...

    // hashValue needed to implement Hashable protocol
    var hashValue: Int {
        get {
            
            // DJB Hash Function
            var hash = 5381
            
            for(var i = 0; i < self.scalarArray.count; i++)
            {
                hash = ((hash << 5) &+ hash) &+ Int(self.scalarArray[i])
            }
            
            return hash
        }
    }

}

// Hashable also needs struct to conform to Equatable protocol
func ==(left: ScalarString, right: ScalarString) -> Bool {
    
    if left.length != right.length {
        return false
    }
    
    for var i = 0; i < left.length; ++i {
        if left.charAt(i) != right.charAt(i) {
            return false
        }
    }
    
    return true
}

A while back I made a custom String struct (see github repo) because of the difficulties in dealing with Mongolian Unicode rendering when using Swift String or NSString (see this question and the accepted answer for more details). The struct is an array of UInt32 so that it represents a string of Unicode scalar values.

The most difficult part (see here and here for my StackOverflow questions at the time) was making it conform to the Hashable Protocol so that it could be used as a Dictionary key.

For all that I can tell, everything has been working fine. However, since there was not a lot of documentation and guidance available about implementing the Hashable Protocol, I wanted to make sure that I did it right. Here is the relevant code:

struct ScalarString: SequenceType, Hashable, CustomStringConvertible {
    
    private var scalarArray: [UInt32] = []

    // ...

    // hashValue needed to implement Hashable protocol
    var hashValue: Int {
        get {
            
            // DJB Hash Function
            var hash = 5381
            
            for(var i = 0; i < self.scalarArray.count; i++)
            {
                hash = ((hash << 5) &+ hash) &+ Int(self.scalarArray[i])
            }
            
            return hash
        }
    }

}

// Hashable also needs struct to conform to Equatable protocol
func ==(left: ScalarString, right: ScalarString) -> Bool {
    
    if left.length != right.length {
        return false
    }
    
    for var i = 0; i < left.length; ++i {
        if left.charAt(i) != right.charAt(i) {
            return false
        }
    }
    
    return true
}

A while back I made a custom String struct (see github repo) because of the difficulties in dealing with Mongolian Unicode rendering when using Swift String or NSString (see this question and the accepted answer for more details). The struct is an array of UInt32 so that it represents a string of Unicode scalar values.

The most difficult part (see here and here for my StackOverflow questions at the time) was making it conform to the Hashable Protocol so that it could be used as a Dictionary key.

For all that I can tell, everything has been working fine. However, since there was not a lot of documentation and guidance available about implementing the Hashable Protocol, I wanted to make sure that I did it right. Here is the relevant code:

struct ScalarString: SequenceType, Hashable, CustomStringConvertible {
    
    private var scalarArray: [UInt32] = []

    // ...

    // hashValue needed to implement Hashable protocol
    var hashValue: Int {
        get {
            
            // DJB Hash Function
            var hash = 5381
            
            for(var i = 0; i < self.scalarArray.count; i++)
            {
                hash = ((hash << 5) &+ hash) &+ Int(self.scalarArray[i])
            }
            
            return hash
        }
    }

}

// Hashable also needs struct to conform to Equatable protocol
func ==(left: ScalarString, right: ScalarString) -> Bool {
    
    if left.length != right.length {
        return false
    }
    
    for var i = 0; i < left.length; ++i {
        if left.charAt(i) != right.charAt(i) {
            return false
        }
    }
    
    return true
}
Source Link
Suragch
  • 405
  • 1
  • 11
  • 20

Implementing the Hashable Protocol in Swift with the DJB hash function

A while back I made a custom String struct (see github repo) because of the difficulties in dealing with Mongolian Unicode rendering when using Swift String or NSString (see this question and the accepted answer for more details). The struct is an array of UInt32 so that it represents a string of Unicode scalar values.

The most difficult part (see here and here for my StackOverflow questions at the time) was making it conform to the Hashable Protocol so that it could be used as a Dictionary key.

For all that I can tell, everything has been working fine. However, since there was not a lot of documentation and guidance available about implementing the Hashable Protocol, I wanted to make sure that I did it right. Here is the relevant code:

struct ScalarString: SequenceType, Hashable, CustomStringConvertible {
    
    private var scalarArray: [UInt32] = []

    // ...

    // hashValue needed to implement Hashable protocol
    var hashValue: Int {
        get {
            
            // DJB Hash Function
            var hash = 5381
            
            for(var i = 0; i < self.scalarArray.count; i++)
            {
                hash = ((hash << 5) &+ hash) &+ Int(self.scalarArray[i])
            }
            
            return hash
        }
    }

}

// Hashable also needs struct to conform to Equatable protocol
func ==(left: ScalarString, right: ScalarString) -> Bool {
    
    if left.length != right.length {
        return false
    }
    
    for var i = 0; i < left.length; ++i {
        if left.charAt(i) != right.charAt(i) {
            return false
        }
    }
    
    return true
}