alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (SliceInterval.scala)

This example Scala source code file (SliceInterval.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

int, sliceinterval

The SliceInterval.scala Scala example source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2013, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

package scala
package collection
package generic

/** A container for the endpoints of a collection slice.
 *  The constructor is private to enforce the invariants:
 *  from >= 0, until >= 0, from <= until.
 */
private[collection] class SliceInterval private (val from: Int, val until: Int) {
  // The width of this slice from end to end.  This is the
  // maximum size of the collection slice, but the collection
  // need not have this many (or any) elements.  Since
  // from <= until is a constructor invariant, we don't have to
  // check for negative values.
  def width = until - from

  /** Returns a new SliceInterval with endpoints calculated in
   *  terms of the original collection.
   *  Example:
   *  {{{
   *  val coll = (1 to 100).view.slice(10, 30).slice(1, 3)
   *  // the second call to slice causes the interval to
   *  // be recalculated: the result is SliceInterval(11, 13).
   *  }}}
   */
  def recalculate(_from: Int, _until: Int): SliceInterval = {
    val lo    = _from max 0
    val elems = scala.math.min(_until - lo, width)
    val start = from + lo

    if (elems <= 0) new SliceInterval(from, from)
    else new SliceInterval(start, start + elems)
  }
  def recalculate(interval: SliceInterval): SliceInterval =
    recalculate(interval.from, interval.until)
}

object SliceInterval {
  def apply(from: Int, until: Int) = {
    val lo = from max 0
    val hi = until max 0

    if (hi <= lo) new SliceInterval(lo, lo)
    else new SliceInterval(lo, hi)
  }
}

Other Scala source code examples

Here is a short list of links related to this Scala SliceInterval.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.