     * where they are entered in the consequtive order of the search parameters.
     * This search method should be used for chinese
     */
-/*
-    func search2(bible: Bible, words: [String]) -> [WordRef: WordPositions] {
-        var finalResult = [WordRef: WordPositions]()
-        let results1 = self.search1(bible: bible, words: words)
+    func search2(bible: Bible, words: [String]) -> [WordRef] {
+        var finalResult = [WordRef]()
+        let results1: [WordRef] = self.search1(bible: bible, words: words)
         if results1.count == 0 {
             return finalResult
         }
-        for (wordRef, positions) in results1 {
-            let updatedPostions = self.matchToEachReference(wordPositions: positions)
+        for index in 0..<results1.count {
+            var wordRef = results1[index]
+            let updatedPostions = self.matchToEachReference2(wordPositions: wordRef.wordPositions!)
             if updatedPostions.positions[0].count > 0 {
-                finalResult[wordRef] = updatedPostions
+                wordRef.wordPositions = updatedPostions
+                finalResult.append(wordRef)
             }
         }
         return finalResult
     }
-*/
-    private func matchToEachReference(wordPositions: WordPositions) -> WordPositions {
+    
+    private func matchToEachReference2(wordPositions: WordPositions) -> WordPositions {
         var updatedPositions = WordPositions(numWords: wordPositions.numWords)
         let firstWordPositions: [UInt8] = wordPositions.positions[0]
         for index in 0..<firstWordPositions.count {
-            let matches: [UInt8]? = self.matchToEachWord(wordPositions: wordPositions, index: index)
+            let matches: [UInt8]? = self.matchToEachWord2(wordPositions: wordPositions, index: index)
             if matches != nil {
                 updatedPositions.addReference(positions: matches!)
             }

            self.positions[wordIndex].append(positions[wordIndex])
        }
    }
}


struct ConcordanceModel {
    
    struct TempWordRef : Equatable, Hashable {
        let nodeId: String
        var position: Int  // Should be unsigned byte, or unsigned short
        
        init(reference: String) {
            let parts = reference.components(separatedBy: ";")
            self.nodeId = parts[0]
            self.position = Int(parts[1])!
        }
        
        //var hashValue: Int {
        //    return self.nodeId.hashValue
        //}
        
        func hash(into hasher: inout Hasher) {
            hasher.combine(self.nodeId)
        }
        
        var reference: String {
            get {
                return self.nodeId + ";" + String(self.position)
            }
        }
        
        mutating func next() {
            self.position += 1
        }
        
        static func == (lhs: TempWordRef, rhs: TempWordRef) -> Bool {
            //return lhs.nodeId == rhs.nodeId && lhs.position == rhs.position
            return lhs.nodeId == rhs.nodeId
        }
    }
    
    //let wordsLookAhead = 0
    
    /**
    * This method searches for all verses that contain all of the words entered.

     * This method searches for all verses that contain all of the words entered.
     * It also ensures that the words are in the sequence entered in the search.
     */
    func search3(bible: Bible, words: [String]) -> [WordRef] {
        var finalResult = [WordRef]()
        let results1: [WordRef] = self.search1(bible: bible, words: words)
        if results1.count == 0 {
            return finalResult
        }
        for index in 0..<results1.count {
            var wordRef = results1[index]
            let updatedPostions = self.matchToEachReference3(wordPositions: wordRef.wordPositions!)
            if updatedPostions.positions[0].count > 0 {
                wordRef.wordPositions = updatedPostions
                finalResult.append(wordRef)
            }
        }
        return finalResult
    }
    
    // This is identical to matchToEachReference2, except by calling matchToEachWord3
    private func matchToEachReference3(wordPositions: WordPositions) -> WordPositions {
        var updatedPositions = WordPositions(numWords: wordPositions.numWords)
        let firstWordPositions: [UInt8] = wordPositions.positions[0]
        for index in 0..<firstWordPositions.count {
            let matches: [UInt8]? = self.matchToEachWord3(wordPositions: wordPositions, index: index)
            if matches != nil {
                updatedPositions.addIfNewReference(positions: matches!)
            }
        }
        return updatedPositions
    }
    
    private func matchToEachWord3(wordPositions: WordPositions, index: Int) -> [UInt8]? {
        var updatedPositions: [UInt8] = Array(repeating: 0, count: wordPositions.numWords)
        var nextPosition: UInt8 = wordPositions.positions[0][index]
        updatedPositions[0] = nextPosition
        for wordIndex in 1..<wordPositions.numWords {
            let oneWordPositions: [UInt8] = wordPositions.positions[wordIndex]
            let greater: UInt8? = self.firstGreater3(array: oneWordPositions, num: nextPosition)
            if greater != nil {
                updatedPositions[wordIndex] = greater!
                nextPosition = greater!
            } else {
                return nil
            }
        }
        return updatedPositions
    }
        
    private func firstGreater3(array: [UInt8], num: UInt8) -> UInt8? {
        for val in array {
            if val > num {
                return val
            }
        }
        return nil
    }
}

