|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 Functional Streams for Scala |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | + * this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to |
| 7 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package fs2 |
| 23 | + |
| 24 | +import org.scalacheck.Shrink |
| 25 | + |
| 26 | +import scala.collection.immutable.{Stream => StdStream} |
| 27 | + |
| 28 | +private[fs2] trait ChunkGeneratorsCompat { |
| 29 | + |
| 30 | + protected implicit def shrinkChunk[A]: Shrink[Chunk[A]] = |
| 31 | + Shrink[Chunk[A]](c => removeChunks(c.size, c)) |
| 32 | + |
| 33 | + // The removeChunks function and the interleave function were ported from Scalacheck, |
| 34 | + // from Shrink.scala, licensed under a Revised BSD license. |
| 35 | + // |
| 36 | + // /*-------------------------------------------------------------------------*\ |
| 37 | + // ** ScalaCheck ** |
| 38 | + // ** Copyright (c) 2007-2016 Rickard Nilsson. All rights reserved. ** |
| 39 | + // ** http://www.scalacheck.org ** |
| 40 | + // ** ** |
| 41 | + // ** This software is released under the terms of the Revised BSD License. ** |
| 42 | + // ** There is NO WARRANTY. See the file LICENSE for the full text. ** |
| 43 | + // \*------------------------------------------------------------------------ */ |
| 44 | + |
| 45 | + private def removeChunks[A](size: Int, xs: Chunk[A]): StdStream[Chunk[A]] = |
| 46 | + if (xs.isEmpty) StdStream.empty |
| 47 | + else if (xs.size == 1) StdStream(Chunk.empty) |
| 48 | + else { |
| 49 | + val n1 = size / 2 |
| 50 | + val n2 = size - n1 |
| 51 | + lazy val xs1 = xs.take(n1) |
| 52 | + lazy val xs2 = xs.drop(n1) |
| 53 | + lazy val xs3 = |
| 54 | + for (ys1 <- removeChunks(n1, xs1) if !ys1.isEmpty) yield Chunk.concat(List(ys1, xs2)) |
| 55 | + lazy val xs4 = |
| 56 | + for (ys2 <- removeChunks(n2, xs2) if !ys2.isEmpty) yield Chunk.concat(List(xs1, ys2)) |
| 57 | + |
| 58 | + StdStream.cons(xs1, StdStream.cons(xs2, interleave(xs3, xs4))) |
| 59 | + } |
| 60 | + |
| 61 | + private def interleave[A](xs: StdStream[A], ys: StdStream[A]): StdStream[A] = |
| 62 | + if (xs.isEmpty) ys else if (ys.isEmpty) xs else xs.head +: ys.head +: (xs.tail ++ ys.tail) |
| 63 | +} |
0 commit comments