|
| 1 | +// |
| 2 | +// BinarySearchExtensions.swift |
| 3 | +// |
| 4 | +// Created by Mark Hamilton on 3/26/16. |
| 5 | +// Copyright © 2016 dryverless. (http://www.dryverless.com) |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | +// |
| 25 | + |
| 26 | +import Foundation |
| 27 | + |
| 28 | +public enum CalcError: ErrorType { |
| 29 | + |
| 30 | + case InvalidAverage |
| 31 | + |
| 32 | + case InvalidMiddleValue |
| 33 | +} |
| 34 | + |
| 35 | +public func binarySearch< T : Comparable >(collection: [T], query: T) throws -> Bool { |
| 36 | + |
| 37 | + var leftCount: Int = 0 |
| 38 | + var rightCount: Int = collection.count - 1 |
| 39 | + |
| 40 | + while( leftCount <= rightCount ) { |
| 41 | + |
| 42 | + guard let middleValue: Int = (( leftCount + rightCount ) / 2) else { |
| 43 | + |
| 44 | + throw CalcError.InvalidAverage |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + guard let estimatedValue: T = collection[middleValue] else { |
| 49 | + |
| 50 | + throw CalcError.InvalidMiddleValue |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + if (estimatedValue == query) { |
| 55 | + |
| 56 | + return true |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + if (estimatedValue < query) { |
| 61 | + |
| 62 | + leftCount = middleValue + 1 |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + if (estimatedValue > query) { |
| 67 | + |
| 68 | + rightCount = middleValue - 1 |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + return false |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +public func binarySearchPrefix(collection: [String], query: String) throws -> Bool { |
| 79 | + |
| 80 | + var leftCount: Int = 0 |
| 81 | + var rightCount: Int = collection.count - 1 |
| 82 | + |
| 83 | + while( leftCount <= rightCount ) { |
| 84 | + |
| 85 | + guard let middleValue: Int = (( leftCount + rightCount ) / 2) else { |
| 86 | + |
| 87 | + throw CalcError.InvalidAverage |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + guard let estimatedValue: String = collection[middleValue] else { |
| 92 | + |
| 93 | + throw CalcError.InvalidMiddleValue |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + if (estimatedValue.hasPrefix(query)) { |
| 98 | + |
| 99 | + return true |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + if (estimatedValue < query) { |
| 104 | + |
| 105 | + leftCount = middleValue + 1 |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + if (estimatedValue > query) { |
| 110 | + |
| 111 | + rightCount = middleValue - 1 |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + return false |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +public func binarySearchFirst(collection: [String], query: String) throws -> Int { |
| 122 | + |
| 123 | + var leftCount: Int = 0 |
| 124 | + var rightCount: Int = collection.count - 1 |
| 125 | + |
| 126 | + while( leftCount <= rightCount ) { |
| 127 | + |
| 128 | + guard let middleValue: Int = (( leftCount + rightCount ) / 2) else { |
| 129 | + |
| 130 | + throw CalcError.InvalidAverage |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + guard let estimatedValue: String = collection[middleValue] else { |
| 135 | + |
| 136 | + throw CalcError.InvalidMiddleValue |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + if (estimatedValue.hasPrefix(query) && leftCount == rightCount) { |
| 141 | + |
| 142 | + return leftCount |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + if estimatedValue.hasPrefix(query) { |
| 147 | + |
| 148 | + if middleValue > 0 && !collection[middleValue - 1].hasPrefix(query) { |
| 149 | + |
| 150 | + return middleValue |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + rightCount = middleValue - 1 |
| 155 | + |
| 156 | + } else if (estimatedValue < query) { |
| 157 | + |
| 158 | + leftCount = middleValue + 1 |
| 159 | + |
| 160 | + } else if (estimatedValue > query) { |
| 161 | + |
| 162 | + rightCount = middleValue - 1 |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + return -1 |
| 169 | + |
| 170 | +} |
| 171 | + |
| 172 | +public func binarySearchLast(collection: [String], query: String) throws -> Int { |
| 173 | + |
| 174 | + var leftCount: Int = 0 |
| 175 | + var rightCount: Int = collection.count - 1 |
| 176 | + |
| 177 | + while( leftCount <= rightCount ) { |
| 178 | + |
| 179 | + guard let middleValue: Int = (( leftCount + rightCount ) / 2) else { |
| 180 | + |
| 181 | + throw CalcError.InvalidAverage |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | + guard let estimatedValue: String = collection[middleValue] else { |
| 186 | + |
| 187 | + throw CalcError.InvalidMiddleValue |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | + if (estimatedValue.hasPrefix(query) && leftCount == rightCount) { |
| 192 | + |
| 193 | + return leftCount |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + if estimatedValue.hasPrefix(query) { |
| 198 | + |
| 199 | + if middleValue < collection.count - 1 && !collection[middleValue + 1].hasPrefix(query) { |
| 200 | + |
| 201 | + return middleValue |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + leftCount = middleValue + 1 |
| 206 | + |
| 207 | + } else if (estimatedValue < query) { |
| 208 | + |
| 209 | + leftCount = middleValue + 1 |
| 210 | + |
| 211 | + } else if (estimatedValue > query) { |
| 212 | + |
| 213 | + rightCount = middleValue - 1 |
| 214 | + |
| 215 | + } |
| 216 | + |
| 217 | + } |
| 218 | + |
| 219 | + return -1 |
| 220 | + |
| 221 | +} |
0 commit comments