-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
218 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// SortedArray.swift | ||
// HandySwift | ||
// | ||
// Created by Cihat Gündüz on 26.12.15. | ||
// Copyright © 2015 Flinesoft. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
public struct SortedArray<Element: Comparable> { | ||
|
||
// MARK: - Stored Instance Properties | ||
|
||
private var internalArray: Array<Element> = [] | ||
|
||
public var array: Array<Element> { | ||
get { | ||
return self.internalArray | ||
} | ||
} | ||
|
||
|
||
// MARK: - Initializers | ||
|
||
public init(array: [Element]) { | ||
self.internalArray = array.sort() | ||
} | ||
|
||
|
||
// MARK: - Instance Methods | ||
|
||
public func firstMatchingIndex(predicate: Element -> Bool) -> Array<Element>.Index? { | ||
|
||
// check if all elements match | ||
if let firstElement = self.array.first { | ||
if predicate(firstElement) { | ||
return self.array.startIndex | ||
} | ||
} | ||
|
||
// check if no element matches | ||
if let lastElement = self.array.last { | ||
if !predicate(lastElement) { | ||
return nil | ||
} | ||
} | ||
|
||
// binary search for first matching element | ||
var predicateMatched = false | ||
|
||
var lowerIndex = self.array.startIndex | ||
var upperIndex = self.array.endIndex | ||
|
||
while lowerIndex != upperIndex { | ||
|
||
let middleIndex = lowerIndex.advancedBy(lowerIndex.distanceTo(upperIndex) / 2) | ||
|
||
if predicate(self.array[middleIndex]) { | ||
upperIndex = middleIndex | ||
predicateMatched = true | ||
} else { | ||
lowerIndex = middleIndex.advancedBy(1) | ||
} | ||
|
||
} | ||
|
||
if !predicateMatched { | ||
return nil | ||
} | ||
|
||
return lowerIndex | ||
} | ||
|
||
public func subArray(toIndex endIndex: Array<Element>.Index) -> SortedArray { | ||
|
||
let range = Range<Int>(start: self.array.startIndex, end: endIndex) | ||
let subArray = Array(self.array[range]) | ||
|
||
return SortedArray(array: subArray) | ||
|
||
} | ||
|
||
public func subArray(fromIndex startIndex: Array<Element>.Index) -> SortedArray { | ||
|
||
let range = Range<Int>(start: startIndex, end: self.array.endIndex) | ||
let subArray = Array(self.array[range]) | ||
|
||
return SortedArray(array: subArray) | ||
|
||
} | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// SortedArrayTests.swift | ||
// HandySwift | ||
// | ||
// Created by Cihat Gündüz on 26.12.15. | ||
// Copyright © 2015 Flinesoft. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import HandySwift | ||
|
||
class SortedArrayTests: XCTestCase { | ||
|
||
func testInitialization() { | ||
|
||
let intArray: [Int] = [9, 1, 3, 2, 5, 4, 6, 0, 8, 7] | ||
let sortedIntArray = SortedArray(array: intArray) | ||
|
||
XCTAssertEqual(sortedIntArray.array, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) | ||
|
||
} | ||
|
||
func testFirstMatchingIndex() { | ||
|
||
let emptyArray: [Int] = [] | ||
let sortedEmptyArray = SortedArray(array: emptyArray) | ||
|
||
XCTAssertNil(sortedEmptyArray.firstMatchingIndex{ _ in true }) | ||
|
||
let intArray: [Int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
let sortedIntArray = SortedArray(array: intArray) | ||
|
||
let expectedIndex = 3 | ||
let resultingIndex = sortedIntArray.firstMatchingIndex{ $0 >= 3 } | ||
|
||
XCTAssertEqual(resultingIndex, expectedIndex) | ||
|
||
} | ||
|
||
func testSubArrayToIndex() { | ||
|
||
let intArray: [Int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
let sortedIntArray = SortedArray(array: intArray) | ||
|
||
let index = sortedIntArray.firstMatchingIndex{ $0 > 5 }! | ||
let sortedSubArray = sortedIntArray.subArray(toIndex: index) | ||
|
||
XCTAssertEqual(sortedSubArray.array, [0, 1, 2, 3, 4, 5]) | ||
|
||
} | ||
|
||
func testSubArrayFromIndex() { | ||
|
||
let intArray: [Int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
let sortedIntArray = SortedArray(array: intArray) | ||
|
||
let index = sortedIntArray.firstMatchingIndex{ $0 > 5 }! | ||
let sortedSubArray = sortedIntArray.subArray(fromIndex: index) | ||
|
||
XCTAssertEqual(sortedSubArray.array, [6, 7, 8, 9]) | ||
|
||
} | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters