diff --git a/ExSwift/Array.swift b/ExSwift/Array.swift index 6fcaefe..ea1bde4 100644 --- a/ExSwift/Array.swift +++ b/ExSwift/Array.swift @@ -792,11 +792,48 @@ internal extension Array { return result } + /** + Returns all of the combinations in the array of the given length, allowing repeats + + :param: length + :returns: Combinations + */ + func repeatedCombination (length: Int) -> [[Element]] { + if length < 0 { + return [] + } + var indexes: [Int] = [] + length.times { + indexes.append(0) + } + var combinations: [[Element]] = [] + var offset = self.count - indexes.count + while true { + var combination: [Element] = [] + for index in indexes { + combination.append(self[index]) + } + combinations.append(combination) + var i = indexes.count - 1 + while i >= 0 && indexes[i] == self.count - 1 { + i-- + } + if i < 0 { + break + } + indexes[i]++ + (i+1).upTo(indexes.count - 1) { j in + indexes[j] = indexes[i] + } + } + return combinations + } + /** Returns all of the combinations in the array of the given length - - :param: length - :returns: Combinations + + :param: length + :returns: Combinations */ func combination (length: Int) -> [[Element]] { if length < 0 || length > self.count { @@ -827,6 +864,33 @@ internal extension Array { return combinations } + /** + Returns all of the permutations of this array of a given length, allowing repeats + + :param: length The length of each permutations + :returns All of the permutations of this array of a given length, allowing repeats + */ + func repeatedPermutation(length: Int) -> [[T]] { + if length < 1 { + return [] + } + var permutationIndexes: [[Int]] = [] + permutationIndexes.repeatedPermutationHelper([], length: length, arrayLength: self.count, permutationIndexes: &permutationIndexes) + return permutationIndexes.map({ $0.map({ i in self[i] }) }) + } + + func repeatedPermutationHelper(seed: [Int], length: Int, arrayLength: Int, inout permutationIndexes: [[Int]]) { + if seed.count == length { + permutationIndexes.append(seed) + return + } + for i in (0.. () { + (0..