Skip to content

Commit

Permalink
Merge branch 'release/0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Dec 26, 2015
2 parents f78eb38 + 6cb5383 commit 30599e2
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 6 deletions.
28 changes: 26 additions & 2 deletions HandySwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
823B2B3C1C24AAB7007B3CDD /* HandySwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 823B2B311C24AAB6007B3CDD /* HandySwift.framework */; };
823B2B4D1C24ABA4007B3CDD /* IntExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 823B2B4C1C24ABA4007B3CDD /* IntExtension.swift */; };
823B2B501C24AC00007B3CDD /* IntExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 823B2B4F1C24AC00007B3CDD /* IntExtensionTests.swift */; };
8258E4561C2E0C140031CBFF /* SortedArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8258E4551C2E0C140031CBFF /* SortedArray.swift */; };
8258E4591C2E1ACE0031CBFF /* SortedArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8258E4581C2E1ACE0031CBFF /* SortedArrayTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -29,8 +31,10 @@
823B2B361C24AAB7007B3CDD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = Sources/Info.plist; sourceTree = SOURCE_ROOT; };
823B2B3B1C24AAB7007B3CDD /* HandySwiftTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = HandySwiftTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
823B2B421C24AAB7007B3CDD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = Tests/Info.plist; sourceTree = SOURCE_ROOT; };
823B2B4C1C24ABA4007B3CDD /* IntExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = IntExtension.swift; path = Sources/IntExtension.swift; sourceTree = SOURCE_ROOT; };
823B2B4F1C24AC00007B3CDD /* IntExtensionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = IntExtensionTests.swift; path = Tests/IntExtensionTests.swift; sourceTree = SOURCE_ROOT; };
823B2B4C1C24ABA4007B3CDD /* IntExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = IntExtension.swift; path = Sources/Extensions/IntExtension.swift; sourceTree = SOURCE_ROOT; };
823B2B4F1C24AC00007B3CDD /* IntExtensionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = IntExtensionTests.swift; path = Tests/Extensions/IntExtensionTests.swift; sourceTree = SOURCE_ROOT; };
8258E4551C2E0C140031CBFF /* SortedArray.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SortedArray.swift; path = Sources/Classes/SortedArray.swift; sourceTree = SOURCE_ROOT; };
8258E4581C2E1ACE0031CBFF /* SortedArrayTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SortedArrayTests.swift; path = Tests/Classes/SortedArrayTests.swift; sourceTree = SOURCE_ROOT; };
82F22E551C26434900E784A2 /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -75,6 +79,7 @@
823B2B331C24AAB7007B3CDD /* HandySwift */ = {
isa = PBXGroup;
children = (
8258E4541C2E0BAF0031CBFF /* Classes */,
823B2B4B1C24AB7F007B3CDD /* Extensions */,
823B2B341C24AAB7007B3CDD /* HandySwift.h */,
823B2B361C24AAB7007B3CDD /* Info.plist */,
Expand All @@ -85,6 +90,7 @@
823B2B3F1C24AAB7007B3CDD /* HandySwiftTests */ = {
isa = PBXGroup;
children = (
8258E4571C2E196B0031CBFF /* Classes */,
823B2B4E1C24ABD8007B3CDD /* Extensions */,
823B2B421C24AAB7007B3CDD /* Info.plist */,
);
Expand All @@ -107,6 +113,22 @@
name = Extensions;
sourceTree = "<group>";
};
8258E4541C2E0BAF0031CBFF /* Classes */ = {
isa = PBXGroup;
children = (
8258E4551C2E0C140031CBFF /* SortedArray.swift */,
);
name = Classes;
sourceTree = "<group>";
};
8258E4571C2E196B0031CBFF /* Classes */ = {
isa = PBXGroup;
children = (
8258E4581C2E1ACE0031CBFF /* SortedArrayTests.swift */,
);
name = Classes;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -215,6 +237,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8258E4561C2E0C140031CBFF /* SortedArray.swift in Sources */,
823B2B4D1C24ABA4007B3CDD /* IntExtension.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -223,6 +246,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8258E4591C2E1ACE0031CBFF /* SortedArrayTests.swift in Sources */,
823B2B501C24AC00007B3CDD /* IntExtensionTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
93 changes: 93 additions & 0 deletions Sources/Classes/SortedArray.swift
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.
65 changes: 65 additions & 0 deletions Tests/Classes/SortedArrayTests.swift
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.
38 changes: 34 additions & 4 deletions UsageExamples.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import HandySwift

// MARK: - IntegerTypeExtension

// .times method
// MARK: - IntExtension

//
// `n.times{ someCode }` – Calls someCode n times.
Expand All @@ -17,4 +15,36 @@ var intArray: [Int] = []
let randomInt = Int(arc4random_uniform(1000))
intArray.append(randomInt)
}
intArray
intArray


// MARK: - SortedArray

//
// `SortedArray(array: unsortedArray)` – Initializes with unsorted array.
//
let unsortedArray = [5, 2, 1, 3, 0, 4]
let sortedArray = SortedArray(array: unsortedArray)

//
// `sortedArray.array` – Gives access to internal sorted array.
//
sortedArray.array

//
// `sortedArray.firstMatchingIndex{ predicate }` – Binary search with predicate.
//
let index = sortedArray.firstMatchingIndex{ $0 > 1 }
index

//
// `sortedArray.subArray(toIndex: index) – Returns beginning part as sorted subarray.
//
let nonMatchingSubArray = sortedArray.subArray(toIndex: index!)
nonMatchingSubArray.array

//
// `sortedArray.subArray(fromIndex: index) – Returns ending part as sorted subarray.
//
let matchingSubArray = sortedArray.subArray(fromIndex: index!)
matchingSubArray.arrays

0 comments on commit 30599e2

Please sign in to comment.