performance changes
This commit is contained in:
parent
6149f02311
commit
ee6a9283ce
|
@ -8,45 +8,6 @@
|
||||||
|
|
||||||
import Foundation
|
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 {
|
extension Array where Element : Equatable {
|
||||||
func lastIndexOf(_ e: Element) -> Int {
|
func lastIndexOf(_ e: Element) -> Int {
|
||||||
for pos in (0..<self.count).reversed() {
|
for pos in (0..<self.count).reversed() {
|
||||||
|
|
|
@ -119,7 +119,7 @@ open class Attribute {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func isBooleanAttribute() -> Bool {
|
public func isBooleanAttribute() -> Bool {
|
||||||
return (Attribute.booleanAttributes.binarySearch(Attribute.booleanAttributes, key) != -1)
|
return Attribute.booleanAttributes.contains(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func hashCode() -> Int {
|
public func hashCode() -> Int {
|
||||||
|
|
|
@ -164,7 +164,7 @@ public final class CharacterReader {
|
||||||
pos += 1
|
pos += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return pos > start ? cacheString(start, pos-start) : ""
|
return pos > start ? cacheString(start, pos-start) : empty
|
||||||
}
|
}
|
||||||
|
|
||||||
public func consumeToAnySorted(_ chars: UnicodeScalar...) -> String {
|
public func consumeToAnySorted(_ chars: UnicodeScalar...) -> String {
|
||||||
|
@ -176,13 +176,14 @@ public final class CharacterReader {
|
||||||
let val = input
|
let val = input
|
||||||
|
|
||||||
while (pos < remaining) {
|
while (pos < remaining) {
|
||||||
if (chars.binarySearch(chars, val[pos]) >= 0) {
|
|
||||||
|
if chars.contains(val[pos]) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
pos += 1
|
pos += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return pos > start ? cacheString(start, pos-start) : ""
|
return pos > start ? cacheString(start, pos-start) : empty
|
||||||
}
|
}
|
||||||
|
|
||||||
public func consumeData() -> String {
|
public func consumeData() -> String {
|
||||||
|
@ -199,7 +200,7 @@ public final class CharacterReader {
|
||||||
pos += 1
|
pos += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return pos > start ? cacheString(start, pos-start) : ""
|
return pos > start ? cacheString(start, pos-start) : empty
|
||||||
}
|
}
|
||||||
|
|
||||||
public func consumeTagName() -> String {
|
public func consumeTagName() -> String {
|
||||||
|
@ -339,7 +340,7 @@ public final class CharacterReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func matchesAnySorted(_ seq: [UnicodeScalar]) -> Bool {
|
public func matchesAnySorted(_ seq: [UnicodeScalar]) -> Bool {
|
||||||
return !isEmpty() && seq.binarySearch(seq, input[pos]) >= 0
|
return !isEmpty() && seq.contains(input[pos])
|
||||||
}
|
}
|
||||||
|
|
||||||
public func matchesLetter() -> Bool {
|
public func matchesLetter() -> Bool {
|
||||||
|
|
Loading…
Reference in New Issue