Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1- VRange[A, B] has been added as extension of Range such as it hol… #94

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions core/src/main/scala/dogs/Diet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sealed abstract class Diet[A] {
case EmptyDiet() => false
case DietNode(rng, l, r) =>
if(order.lt(v, rng.start)) l.contains(v)
else if(order.gt(v, rng.end)) r.contains(v)
else if (order.gt(v, rng.end)) r.contains(v)
else rng.contains(v)
}

Expand Down Expand Up @@ -103,7 +103,7 @@ sealed abstract class Diet[A] {
/**
* Add new value range [x, y] to the Diet.
*/
def addRange(range: Range[A])(implicit enum: Enum[A], order: Order[A]): Diet[A] =
def addRange(range: dogs.Range[A])(implicit enum: Enum[A], order: Order[A]): Diet[A] =
if (order.lteqv(range.start, range.end))
insertRange(range)
else
Expand Down Expand Up @@ -264,6 +264,13 @@ sealed abstract class Diet[A] {
case DietNode(rng,l,r) =>
l.toStreaming ++ rng.toStreaming ++ r.toStreaming
}

def focus(range: Range[A])(implicit order: Order[A]): Option[Range[A]] = this match {
case EmptyDiet() => None()
case d @ DietNode(Range(x, y), _, _) if (x == range.start && y == range.end) => Some(d.focus)
case DietNode(rng, l, r) =>
if(order.lt(range.start, rng.start)) l.focus(range) else r.focus(range)
}
}

object Diet {
Expand Down
53 changes: 40 additions & 13 deletions core/src/main/scala/dogs/Range.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import cats.Show
import dogs.Predef._
import cats.Order

/**
* Represent an inclusive range [x, y] that can be generated by using discrete operations
*/
final case class Range[A](val start: A, val end: A) {
import Option._
import Option._

/**
* Represent an inclusive range [x, y] that can be generated by using discrete operations
*/
class Range[A](val start: A, val end: A) {
/**
* Subtract a Range from this range.
* The result will be 0, 1 or 2 ranges
*/
* Subtract a Range from this range.
* The result will be 0, 1 or 2 ranges
*/
def -(range: Range[A])(implicit enum: Enum[A], order: Order[A]): Option[(Range[A], Option[Range[A]])] =
if(order.lteqv(range.start, start)) {
if(order.lt(range.end, start))
some((this, none)) // they are completely to the left of us
else if(order.gteqv(range.end, end))
// we are completely removed
// we are completely removed
none
else some((Range(enum.succ(range.end), end), none))
} else {
Expand All @@ -28,12 +28,10 @@ final case class Range[A](val start: A, val end: A) {
else {
val r1 = Range(start, enum.pred(range.start))
val r2: Option[Range[A]] = if(order.lt(range.end, end)) some(Range(enum.succ(range.end), end)) else none
some((r1,r2))
some((r1,r2))
}
}



def +(other: Range[A])(implicit order: Order[A], enum: Enum[A]): (Range[A], Option[Range[A]]) = {
val (l,r) = if(order.lt(this.start,other.start)) (this,other) else (other,this)

Expand All @@ -54,7 +52,7 @@ final case class Range[A](val start: A, val end: A) {
* Verify that the passed range is a sub-range
*/
def contains(range: Range[A])(implicit order: Order[A]): Boolean =
order.lteqv(start, range.start) && order.gteqv(end, range.end)
order.lteqv(start, range.start) && order.gteqv(end, range.end)

/**
* return a stream of the elements in the range
Expand Down Expand Up @@ -101,14 +99,43 @@ final case class Range[A](val start: A, val end: A) {
}
b
}

def withValue[B](value: B): VRange[A, B] = VRange(this, value)

override def equals(obj: scala.Any): Boolean = obj match {
case Range(`start`, `end`) => true
case _ => false
}
}

object Range {

def apply[A](start: A, end: A): Range[A] = new Range(start, end)

def unapply[A](arg: Range[A]): scala.Option[(A, A)] = scala.Some((arg.start, arg.end))

implicit def rangeShowable[A](implicit s: Show[A]): Show[Range[A]] = new Show[Range[A]] {
override def show(f: Range[A]): Predef.String = {
val (a, b) = (s.show(f.start), s.show(f.end))
s"[$a, $b]"
}
}
}


class VRange[A, B] private [dogs] (override val start: A, override val end: A, val value: B) extends Range[A](start, end) {
override def equals(obj: Any): Boolean = obj match {
case VRange(`start`, `end`, `value`) => true
case _ => false
}

override def toString(): String = s"VRange($start, $end, $value)"
}

object VRange {
def apply[A, B](start: A, end: A, value: B): VRange[A, B] = new VRange(start, end, value)

def apply[A, B](range: Range[A], value: B): VRange[A, B] = new VRange(range.start, range.end, value)

def unapply[A, B](vRange: VRange[A, B]): scala.Option[(A, A, B)] = scala.Some((vRange.start, vRange.end, vRange.value))
}
21 changes: 21 additions & 0 deletions tests/src/test/scala/DietSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ class DietSpec extends DogsSuite {
})


test("VRange") {
import Diet._

val diet = Diet.empty[Int] + Range(1, 10).withValue("hello")

diet should be (DietNode(VRange(1, 10, "hello"), EmptyDiet(), EmptyDiet()))
}

test("Focus") {

val diet = Diet.empty[Int] + Range(1, 10).withValue("hello")

val result = diet.focus(Range(1, 10))

println(result)

result should be (Some(VRange(1, 10, "hello")))

diet.focus(Range(1, 5)) should be (None)
}

// property("match") = forAll{ (r: Ranges) =>
// val d = fromRanges(r)
//
Expand Down
11 changes: 11 additions & 0 deletions tests/src/test/scala/RangeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,14 @@ class RangeTest extends DogsSuite {
range.show should be ("[10, 20]")
}
}

class VRangeTest extends DogsSuite {

test("range can store value") {
val range = Range(1, 100).withValue(5)

toRange(range) should be ((Range(1, 100), 5))
}

def toRange[A, B](vRange: VRange[A, B]): (Range[A], B) = (Range(vRange.start, vRange.end), vRange.value)
}