Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
enhancement/array-extension (#51)
Browse files Browse the repository at this point in the history
* moved split function into array extension

* fixed label

* fixed code
  • Loading branch information
Dean Eigenmann authored Feb 13, 2019
1 parent d7722da commit d4a3d28
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
11 changes: 1 addition & 10 deletions Sources/BeaconChain/BeaconChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,6 @@ extension BeaconChain {
return index
}

static func split<T>(values: [T], splitCount: Int) -> [[T]] {

let listLength = values.count

return stride(from: 0, to: values.count, by: splitCount).map {
Array(values[(listLength * $0 / splitCount) ..< (listLength * ($0 + 1) / splitCount)])
}
}

static func getShuffling(seed: Bytes32, validators: [Validator], epoch: EpochNumber) -> [[ValidatorIndex]] {
let activeValidatorIndices = getActiveValidatorIndices(validators: validators, epoch: epoch)
let committeesPerEpoch = getEpochCommitteeCount(activeValidatorCount: validators.count)
Expand All @@ -107,7 +98,7 @@ extension BeaconChain {
activeValidatorIndices[getPermutedIndex(index: Int($0), listSize: activeValidatorIndices.count, seed: seed)]
}

return split(values: shuffledActiveValidatorIndices, splitCount: committeesPerEpoch)
return shuffledActiveValidatorIndices.split(count: committeesPerEpoch)
}
}

Expand Down
13 changes: 13 additions & 0 deletions Sources/BeaconChain/Extensions/Array.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

extension Array {

func split(count: Int) -> [[Element]] {
let length = self.count

return (0..<count).map {
Array(self[(length * $0 / count) ..< (length * ($0 + 1) / count)])
}
}

}
11 changes: 11 additions & 0 deletions Tests/BeaconChainTests/Extensions/ArrayTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import XCTest
@testable import BeaconChain

final class ArrayTests: XCTestCase {

func testSplit() {
let array = [0, 1, 2, 3]
XCTAssertEqual([[0, 1], [2, 3]], array.split(count: 2))
}

}
3 changes: 2 additions & 1 deletion Tests/BeaconChainTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import XCTest
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(BeaconChainTests.allTests),
testCase(StateTransitionTests.allTests)
testCase(ArrayTests.allTests),
// testCase(StateTransitionTests.allTests)
]
}
#endif

0 comments on commit d4a3d28

Please sign in to comment.