@@ -173,14 +173,31 @@ extension RandomAccessCollection {
173
173
public func heatmap( width: Int , interpolator: Interpolator < Element > ) -> Heatmap < [ SubSequence ] > {
174
174
precondition ( width > 0 , " Cannot build a histogram with zero or negative width " )
175
175
let height = Int ( ( Float ( count) / Float( width) ) . rounded ( . up) )
176
- return ( 0 ..< height) . map { row -> SubSequence in
177
- guard let start = index ( startIndex, offsetBy: row * width, limitedBy: endIndex) else {
178
- return self [ startIndex..< startIndex]
179
- }
180
- guard let end = index ( start, offsetBy: width, limitedBy: endIndex) else {
181
- return self [ start..< endIndex]
182
- }
183
- return self [ start..< end]
184
- } . heatmap ( interpolator: interpolator)
176
+ return ( 0 ..< height)
177
+ . map { _sliceForRow ( $0, width: width) }
178
+ . heatmap ( interpolator: interpolator)
179
+ }
180
+
181
+ /// Returns a heatmap of this collection's values, generated by the data in to `height` rows.
182
+ /// - parameters:
183
+ /// - height: The height of the heatmap to generate. Must be greater than 0.
184
+ /// - interpolator: A function or `KeyPath` which maps values to a continuum between 0 and 1.
185
+ /// - returns: A heatmap plot of the collection's values.
186
+ public func heatmap( height: Int , interpolator: Interpolator < Element > ) -> Heatmap < [ SubSequence ] > {
187
+ precondition ( height > 0 , " Cannot build a histogram with zero or negative height " )
188
+ let width = Int ( ( Float ( count) / Float( height) ) . rounded ( . up) )
189
+ return ( 0 ..< height)
190
+ . map { _sliceForRow ( $0, width: width) }
191
+ . heatmap ( interpolator: interpolator)
192
+ }
193
+
194
+ private func _sliceForRow( _ row: Int , width: Int ) -> SubSequence {
195
+ guard let start = index ( startIndex, offsetBy: row * width, limitedBy: endIndex) else {
196
+ return self [ startIndex..< startIndex]
197
+ }
198
+ guard let end = index ( start, offsetBy: width, limitedBy: endIndex) else {
199
+ return self [ start..< endIndex]
200
+ }
201
+ return self [ start..< end]
185
202
}
186
203
}
0 commit comments