performance changes

This commit is contained in:
Nabil Chatbi 2017-03-26 22:11:48 +02:00
parent 6149f02311
commit ee6a9283ce
3 changed files with 7 additions and 45 deletions

View File

@ -8,45 +8,6 @@
import Foundation
extension Array {
func binarySearch<T: Comparable>(_ collection: [T], _ target: T) -> Int {
let min = 0
let max = collection.count - 1
return binaryMakeGuess(min: min, max: max, target: target, collection: collection)
}
func binaryMakeGuess<T: Comparable>(min: Int, max: Int, target: T, collection: [T]) -> Int {
let guess = (min + max) / 2
if max < min {
// illegal, guess not in array
return -1
} else if collection[guess] == target {
// guess is correct
return guess
} else if collection[guess] > target {
// guess is too high
return binaryMakeGuess(min: min, max: guess - 1, target: target, collection: collection)
} else {
// array[guess] < target
// guess is too low
return binaryMakeGuess(min: guess + 1, max: max, target: target, collection: collection)
}
}
}
extension Array where Element : Equatable {
func lastIndexOf(_ e: Element) -> Int {
for pos in (0..<self.count).reversed() {

View File

@ -119,7 +119,7 @@ open class Attribute {
}
public func isBooleanAttribute() -> Bool {
return (Attribute.booleanAttributes.binarySearch(Attribute.booleanAttributes, key) != -1)
return Attribute.booleanAttributes.contains(key)
}
public func hashCode() -> Int {

View File

@ -164,7 +164,7 @@ public final class CharacterReader {
pos += 1
}
return pos > start ? cacheString(start, pos-start) : ""
return pos > start ? cacheString(start, pos-start) : empty
}
public func consumeToAnySorted(_ chars: UnicodeScalar...) -> String {
@ -176,13 +176,14 @@ public final class CharacterReader {
let val = input
while (pos < remaining) {
if (chars.binarySearch(chars, val[pos]) >= 0) {
if chars.contains(val[pos]) {
break
}
pos += 1
}
return pos > start ? cacheString(start, pos-start) : ""
return pos > start ? cacheString(start, pos-start) : empty
}
public func consumeData() -> String {
@ -199,7 +200,7 @@ public final class CharacterReader {
pos += 1
}
return pos > start ? cacheString(start, pos-start) : ""
return pos > start ? cacheString(start, pos-start) : empty
}
public func consumeTagName() -> String {
@ -339,7 +340,7 @@ public final class CharacterReader {
}
public func matchesAnySorted(_ seq: [UnicodeScalar]) -> Bool {
return !isEmpty() && seq.binarySearch(seq, input[pos]) >= 0
return !isEmpty() && seq.contains(input[pos])
}
public func matchesLetter() -> Bool {